-
Notifications
You must be signed in to change notification settings - Fork 1
/
checktex.pl
executable file
·279 lines (261 loc) · 7.93 KB
/
checktex.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/perl -s
#*************************************************************************
#
# Program: checktex
# File: checktex.pl
#
# Version: V1.2
# Date: 03.07.24
# Function: Check \ref and \label in LaTeX file
#
# Copyright: (c) Univ. Reading and Prof. Andrew C.R. Martin, 2003,2024
# Author: Prof. Andrew C. R. Martin
# EMail: andrew@bioinf.org.uk
#
#*************************************************************************
#
# This program is not in the public domain, but it may be copied
# according to the conditions laid out in the accompanying file
# COPYING.DOC
#
# The code may be modified as required, but any modifications must be
# documented so that the person responsible can be identified. If
# someone else breaks this code, I don't want to be blamed for code
# that does not work!
#
# The code may not be sold commercially or included as part of a
# commercial product except as described in the file COPYING.DOC.
#
#*************************************************************************
#
# Description:
# ============
#
#*************************************************************************
#
# Usage:
# ======
#
#*************************************************************************
#
# Revision History:
# =================
# V1.0 28.07.03 Original
# V1.1 25.05.18 Escaped curly brackets in regexes
# V1.2 03.07.24 Added -o and -sf options. Added use strict!
#
#*************************************************************************
use strict;
UsageDie() if defined($::h);
# First pass through the file to read labels
# ------------------------------------------
open(IN, $ARGV[0]) || die "Can't open file $ARGV[0]\n";
my $linenumber = 1;
my $inSubFigure = 0;
my %labels = ();
my %sfLabels = ();
my @labelarray = ();
my @sfLabelarray = ();
while(<IN>)
{
chomp;
if(/\\begin\{subfigure\}/)
{
$inSubFigure = 1;
}
if(/\\end\{subfigure\}/)
{
$inSubFigure = 0;
}
while(/\\label\{(.*?)\}(.*)/) # If the string contains \label{...}
{
my $label = $1;
my $rest = $2;
if(defined($labels{$label}))
{
print "Label used more than once: $label\n";
printf " Was first used on line %d; reused on line %d\n",
$labels{$label}, $linenumber;
}
elsif(defined($sfLabels{$label}))
{
print "SubFigure label used more than once: $label\n";
printf " Was first used on line %d; reused on line %d\n",
$sfLabels{$label}, $linenumber;
}
else
{
# Store the linenumber of the first (only) label occurrence
# and also push the labels onto an array so that we have
# them in order of occurrence
if(defined($::sf) && $inSubFigure)
{
$sfLabels{$label} = $linenumber;
push @sfLabelarray, $label;
}
else
{
$labels{$label} = $linenumber;
push @labelarray, $label;
}
}
$_ = $rest;
}
$linenumber++;
}
close IN;
# Second pass through the file to read references to labels
# Also reports references to undefined labels
# ---------------------------------------------------------
open(IN, $ARGV[0]) || die "Can't open file $ARGV[0]\n";
$linenumber = 1;
my %refs = ();
while(<IN>)
{
chomp;
while(/\\ref\{(.*?)\}(.*)/) # If the string contains \ref{...}
{
my $label = $1;
my $rest = $2;
if(!defined($labels{$label}) &&
!defined($sfLabels{$label}))
{
print "Label was referenced on line $linenumber, but not defined: $label\n";
}
$refs{$label} = $linenumber if(!defined($refs{$label}));
$_ = $rest;
}
$linenumber++;
}
close IN;
# Now see if any labels were not referenced
# -----------------------------------------
foreach my $label (keys %labels)
{
if(!defined($refs{$label}))
{
printf "Label was defined on line %d, but not referenced: %s\n",
$labels{$label}, $label;
}
}
# Now see if any labels are referenced out of order
# -------------------------------------------------
# First create an array of the first reference to each label in the
# order in which the labels appeared
if(!defined($::o))
{
my @refarray = ();
foreach my $label (@labelarray)
{
if(defined($refs{$label}))
{
push @refarray, $refs{$label};
}
else
{
push @refarray, 0;
}
}
# Now work through the array and check that every following item
# is a larger number
for(my $i=0; $i<$#refarray; $i++)
{
if($refarray[$i])
{
for(my $j=$i+1; $j<=$#refarray; $j++)
{
if($refarray[$j])
{
if($refarray[$j] < $refarray[$i])
{
print "Labels referenced out of order:\n";
printf " Label %s appears before %s, but %s is referenced first\n",
$labelarray[$i], $labelarray[$j], $labelarray[$j];
printf " %s appears on line %d and is first referenced on line %d\n",
$labelarray[$i], $labels{$labelarray[$i]}, $refarray[$i];
printf " %s appears on line %d and is first referenced on line %d\n",
$labelarray[$j], $labels{$labelarray[$j]}, $refarray[$j];
}
}
}
}
}
}
# Third pass checks balanced environments and curly brackets
# ----------------------------------------------------------
open(IN, $ARGV[0]) || die "Can't open file $ARGV[0]\n";
my $line = 0;
my $bra = 0;
my @envs = ();
my @starts = ();
while(<IN>)
{
$line++;
$bra += CountChar($_, '{') if(/\{/);
$bra -= CountChar($_, '}') if(/\}/);
my @words = split(/\\/);
foreach my $word (@words)
{
if($word =~ /begin\{(.*?)\}/)
{
my $env = $1;
push @envs, $env;
push @starts, $line;
}
if($word =~ /end\{(.*?)\}/)
{
my $env = $1;
my $theenv = pop @envs;
my $start = pop @starts;
if($env ne $theenv)
{
print "\\begin{$theenv} at line $start closed by \\end{$env} at line $line\n";
goto EXIT;
}
}
}
}
close IN;
EXIT:
if($bra > 0)
{
printf "Curly brackets were unbalanced - there were $bra too many { brackets\n";
}
elsif($bra < 0)
{
$bra *= (-1);
printf "Curly brackets were unbalanced - there were $bra too many } brackets\n";
}
#*************************************************************************
sub CountChar
{
my($line, $bra) = @_;
my(@chars, $char, $count);
@chars = split(//,$line);
$count = 0;
foreach $char (@chars)
{
$count++ if($char eq $bra);
}
return($count);
}
#*************************************************************************
sub UsageDie
{
print <<__EOF;
CheckTexRef V1.2
(c) The University of Reading / Prof. Andrew C.R. Martin, 2003-2024
Usage: checktexref [-o] file.tex
-o Do not check out of order references
-sf Do not check unreferenced labels for sub-figures
Reads a LaTeX file and checks the use of \\ref and \\label commands.
Reports references to non-existent labels, labels without corresponding
references and out-of-sequence references (i.e. labels which are first
referenced in a different order from which they appear).
Note that the program reads only one LaTeX source file, so \\include
and \\input are not honoured. If you reference a label appearing in
another LaTeX file (or a label appears which is referenced in a
different file), then these will be reported as errors.
__EOF
exit 0;
}