14

In the script I presented in this question I encountered the following problem. In case of a compilation error, I have to cancel the execution of pdflatex 3 times. This can be very annoying. I would like to add something like

if pdflatex file.tex returned failure break script

However, I could find what values pdflatex returns when it exits. So the question is:

What are the exits/return values/codes of pdflatex?

2 Answers 2

9

The key one for me is that 0 is returned on successful compilation. You don't say what OS you use, so I'll provide a Unix Makefile and a Windows batch file test. On Unix, I use

pdflatex $$NAME
if [ $$? = 0 ] ; then \
  pdflatex $$NAME ; \
  pdflatex $$NAME ; \
else \
 \echo "Compilation failed" ; \
fi

On Windows with a batch file the error level test is slightly different

pdflatex %1
if ERRORLEVEL 1 (
  echo ! Compilation failed
) else (
  pdflatex %1
  pdflatex %1
)

This tests for failure rather than success as the if ERRORLEVEL test works for 'thios number or higher'. You can build quite a lot more complexity on top of this: it depends on your requirements. (For example, my batch file assumes that the name of the file to compile is given as a command-line argument: you might want to use a variable or hard-coded name.)

9
  • Thanks a lot! Worked eventually! Is there some documentation of other return values of pdflatex? This might be helpful...
    – Dror
    Commented Nov 26, 2010 at 10:53
  • Pipelines work more cleanly with Unix exit status codes, I think: compare (pdflatex $$NAME && pdflatex $$NAME && pdflatex $$NAME) || echo Compilation failed Commented Nov 26, 2010 at 10:54
  • @Charles: I'll take your word for it. (My real Makefile is rather more complex than the example above.)
    – Joseph Wright
    Commented Nov 26, 2010 at 11:12
  • @Dror: 0 is a standard value for 'success' from almost all processes.
    – Joseph Wright
    Commented Nov 26, 2010 at 11:17
  • 1
    @Dror: (1) the general rule is zero for success, any non-zero for failure. Commented Nov 26, 2010 at 12:55
4

Converting Joseph's code into shell syntax, it looks like this:

NAME=$1
pdflatex $NAME
if [ $? = 0 ] ; then
  pdflatex $NAME ;
  pdflatex $NAME ;
else 
  echo "Compilation failed" ;
fi

I am keying this in as an answer on because of formatting.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .