2 # Author: Dimitri Antoniou <dimitri@risc2.aecom.yu.edu>
3 # usage: ltags filename
4 # handles: \label and \cite{ } with one or more arguments
5 # fails if arguments of cite spread over more than one line
6 # also searches in files that are \include or \input in the main file
8 # get main LaTeX source file from command line:
11 # get names of included files and store them in an array
12 open MAIN, $mainfile or die "$!" ;
14 @allsrcfiles = map{ /^\\(?:input|include){(.*?)}/ } @mainfile;
15 unshift @allsrcfiles, $mainfile;
17 # loop over all source files
18 for $srcfile (@allsrcfiles) {
19 # if \input{fname} append .tex to fname
20 unless ( $srcfile =~ m/\.tex/ ) { $srcfile = $srcfile . "\.tex" }
21 open SRC, $srcfile or die "$!" ;
22 # store contents of source file in array @texfile
25 # store lines with \label and \cite (or \citeonline) in arrays
26 @labelList = grep{ /\\label{/ } @texfile;
27 @citeList = grep{ /\\(cite|citeonline){/ } @texfile;
29 # see if we use an external database; if yes, store its name in $bibfile
30 ($dbase) = grep{ /^\\bibliography{/ } @texfile;
32 $dbase =~ m/\\bibliography{(.*?)}/;
36 # write \bibitem in tags file
41 while ( m/\\(?:cite|citeonline){(.*?)}/g ) {
43 # if \cite has more than one argument, split them:
45 @mrefs = split /,/, $refs;
46 # there might be more than one \cite in a line:
47 push (@multirefs, @mrefs);
51 push (@multirefs, @refs);
54 # in BibTeX, format is @ARTICLE{Name, }; in source file, \bibitem{Name}
55 for $ref (@multirefs) {
57 push @unsorttag, "$ref\t$bibfile\t/{$ref,/\n"
60 push @unsorttag, "$ref\t$srcfile\t/bibitem{$ref}/\n"
65 # write \label in tag file
66 foreach (@labelList) {
68 push @unsorttag, "$1\t$srcfile\t/label{$1}/\n";
72 # sort tag file; then, eliminate duplicates
73 @sortedtag = sort @unsorttag;
75 @uniqtag = grep { ! $seen{$_} ++ } @sortedtag;