summaryrefslogtreecommitdiff
path: root/support/latexn/latexn
blob: 9a105737e062e73bdc15ddf22417aed65aeaddcd (plain)
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
#! /bin/csh -f
# File: latexn

# This is a csh script for UNIX systems.  It runs Latex enough times to 
# resolve cross references, and to get the table of contents (if any) correct.
# It also runs makeindex if needed.  See the help text below for more details.

# INSTALLATION: Make this an executable file: chmod a+x latexn
# and put it in an appropriate directory for executable files.
# If your system has non-standard commands to run bibtex, latex and makeindex, 
# then change the appropriate lines.

# CUSTOMIZATION
# Similarly, if you have personal preferences as to how these programs should
# be used, change the appropriate lines.  For example, Andrei Klot points out
# that if you would like latex to stop automatically after the first error it
# finds, you can replace the line
#
#       latex $thisfile
# by
#       echo X | latex $thisfile

# FILES GENERATED: In addition to the standard LaTeX output files, 
# back-up copies of the previous .aux and .idx files are generated. These
# have names like file.aux.bak and file.idx.bak.  They are rm'd at the end.


# J.C. Collins.  collins@phys.psu.edu
# 29 Oct 97 JCC.  Try again with BiBTeX

# A.S. Beach.  asb4@psu.edu
# 23 Oct 97 ASB.  Attempt at integrating BiBTeX.

# J.C. Collins.  collins@phys.psu.edu
# 21 Mar 97 JCC.  Clean-up
# 17 Mar 97 JCC.  Try latex command line to be echo X | latex ...
#                 (Suggestion from klot@priam.serma.cea.fr, so that
#                 LaTeX just stops on an error, without user intervention.)
#                 But leave that as suggestion for customization.
# 20 Jan 97 JCC.  Clean up from previous versions.  

@ maxcount = 5

if ( "$1" == "" ) then
# No command line arguments => help message.
cat << EOT
 Usage 

     latexn file
 
 LaTeXs file.tex.  If there are no errors then LaTeX is repeated as necessary
 to fix cross references, and to ensure that the table of contents and the 
 index (if any) are correct.  Makeindex and/or bibtex are run if necessary.
 If the file has an explicit extension, then LaTeX is applied to that file
 rather to file.tex.
 The maximum number of attempts at LaTeX is $maxcount.  If LaTeX gives an
 error (as indicated by its exit code), then no repeated runs of LaTeX are
 made.  
EOT
exit
endif


# Variables
# thisfile = source file.
# thisbase= base of filename.
# thisext = extension of filename.
# resulttex = exitcode

set thisfile=$1
set thisbase=$thisfile:r
set thisext=$thisfile:e
set resulttex=0

if ( "$thisext" == "" ) then
   # No extension supplied.  
   set thisfile=$thisbase.tex
endif

if ( ! -e $thisfile ) then
   echo File $thisfile does not exist.
   exit
endif

@ count = 0
# We'll repeat LaTeX until the variable dorepeat is undefined.  
# We'll use LaTeX at least once.
set dorepeat
while ( $?dorepeat ) 
   if ( -e $thisbase.idx) then
      echo Making backup of old .idx file: $thisbase.idx.bak.  Then makeindex...
      cp -p $thisbase.idx $thisbase.idx.bak
      makeindex $thisbase.idx
      echo ' '
   endif

   if ( -e $thisbase.aux) then
      echo Making backup of old .aux file: $thisbase.aux.bak
      cp -p $thisbase.aux $thisbase.aux.bak

      grep \\\\bibdata $thisbase.aux > /dev/null
      if ( ( $status == 0 ) && ( $count == 0 ) )  then 
          echo Need bibtex run before first pass...
          bibtex $thisbase
          echo ' '
      endif
   else if ( ! -e $thisbase.aux.bak) then
      # Make dummy file.  This will save a pass if there are no
      # cross-references generated
      echo "\relax " >! $thisbase.aux.bak
   endif

   if ( -e $thisbase.bbl) then
      echo Making backup of old .bbl file: $thisbase.bbl.bak
      cp -p $thisbase.bbl $thisbase.bbl.bak
   endif




   #  Normal latex:
   latex $thisfile
   #  Commented out alternative to standard latex command to make
   #  latex stop after it encounters an error.
   #  echo X | latex $thisfile

   set resulttex=$status
   @ count = $count + 1
   echo Exit code $resulttex.  Number of iterations $count.
   # status = 0 => no error or mild error (overfull and underfull etc)

      
   # Now run bibtex, if needed, i.e., 
   # an .aux file exists, it contains the string \bibdata, and
   # this is the first pass.
   # The information that bibtex reads from the .aux file is
   # pass-independent. If we find (below) that the bbl file is unchanged,
   # then the last latex saw a correct bibliography.
   if ( -e $thisbase.aux ) then
       # I think I have put the correct number of \'s:
       grep \\\\bibdata $thisbase.aux
       if ( ( $status == 0 ) && ( $count == 1 ) )  then 
           bibtex $thisbase
           echo ' '
       endif
   endif

   if ( $count >= $maxcount ) then
       unset dorepeat
       echo "I've tried $count times.  That's enough."
   else if ( $resulttex >= 1 ) then
      echo "Bad error.  Therefore I will not run latex a second time"
      echo Return code $resulttex
      unset dorepeat
   else
      # Default to not needing to do anything more:
      unset dorepeat
      # Now find any conditions that require a rerun of LaTeX:

      # new .aux file
      if ( ! -e $thisbase.aux ) then
         echo latex produced no .aux file.
         if ( -e $thisbase.aux.bak ) then
            echo The .aux.bak file is of no use.  I will delete it.
            rm  $thisbase.aux.bak
         endif
      else if ( -e $thisbase.aux.bak ) then
         # "Has the .aux file changed?"
         diff $thisbase.aux $thisbase.aux.bak > /dev/null
         if ( $status == 0) then
            echo The .aux file has not changed.
         else
            echo The .aux file has changed.  I must run latex again.
            set dorepeat
         endif
      else
         echo I have a .aux file, but there was no previous one, so I must run latex again.
         set dorepeat
      endif

      # new .idx file
      if ( ! -e $thisbase.idx ) then
         if ( -e $thisbase.idx.bak ) then
            echo latex produced no .idx file.
            echo The .idx.bak file is of no use.  I will delete it.
            rm  $thisbase.idx.bak
         endif
      else if ( -e $thisbase.idx.bak ) then
         # Has the .idx file changed?
         diff $thisbase.idx $thisbase.idx.bak > /dev/null
         if ( $status == 0) then
            echo The .idx file has not changed.
         else
            echo The .idx file has changed.  I must run latex again.
            set dorepeat
         endif
      else
         echo I have a .idx file, but there was no previous one, so I must run latex again.
         set dorepeat
      endif

      # new .bbl file
      if ( ! -e $thisbase.bbl ) then
         if ( -e $thisbase.bbl.bak ) then
            echo latex produced no .bbl file.
            echo The .bbl.bak file is of no use.  I will delete it.
            rm  $thisbase.bbl.bak
         endif
      else if ( -e $thisbase.bbl.bak ) then
         # Has the .bbl file changed?
         diff $thisbase.bbl $thisbase.bbl.bak > /dev/null
         if ( $status == 0) then
            echo The .bbl file has not changed.
         else
            echo The .bbl file has changed.  I must run latex again.
            set dorepeat
         endif
      else
         echo I have a .bbl file, but there was no previous one, so I must run latex again.
         set dorepeat
      endif
   endif
   echo ===============================================
end

# now delete the backup files.
if ( -e $thisbase.aux.bak ) rm $thisbase.aux.bak
if ( -e $thisbase.bbl.bak ) rm $thisbase.bbl.bak
if ( -e $thisbase.idx.bak ) rm $thisbase.idx.bak

exit $resulttex