Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Quoting from an answer to Typesetting the entire Song That Never Ends

I ran this code once, and aborted the process after it reported producing something like 47,000 pages (in a very short amount of time). Unfortunately, my computer complained that the resulting pdf file was "damaged" and could not be opened. A better (perhaps impossible?) answer would produce a file such that the output could actually be viewed once the process was killed (or died).

This got me curious (and it is merely a curiosity). Can TeX die gracefully? Since its output is a PDF, presumably it needs to do some non-trivial stuff at the end to ensure that the PDF is valid so merely killing TeX is too abrupt.

A more graceful method would be for TeX to ask at each shipout "Shall I continue?", but that suffers from the other extreme: it is too intrusive as the user needs to keep giving commands to keep TeX going.

Off the top of my head, the only way I can think of to communicate with a running process in a non-blocking way is via a file. At each shipout (or other), TeX can check for the existence of a file and continue if it doesn't exist, and if it does then read the file and carry out whatever actions said file contains (thus allowing for more refined communication).

Is there a better way?

share|improve this question
As per usual, I have absolutely no idea how to tag this one. –  Loop Space Jan 30 '13 at 10:56
Suggested tag: {to-TeX-or-not-to-TeX}. ;) –  Count Zero Jan 30 '13 at 11:01
I think 'DVI mode' is the plan here, as each shipped out page can normally be viewed even if the run terminates –  Joseph Wright Jan 30 '13 at 11:11
JFTR: If you hit ctrl-c and then x then also pdftex will produce a valid PDF. –  Stephan Lehmke Jan 30 '13 at 16:37
@StephanLehmke Oddly enough, I haven't tested it up to that many pages either! (Hopefully my question expands sufficiently on the original "die nicely" to survive this astonishing revelation.) –  Loop Space Jan 30 '13 at 17:07

3 Answers 3

up vote 8 down vote accepted

enter image description here

Run this file


\def\foo{\advance\count0 1 \foo}


hello I reached \the\count0 before you stopped me


For a bit then stop it at the terminal (control-c in most shells) and enter i\resume and it will finish cleanly:

 pdftex oops
This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012)
 restricted \write18 enabled.
entering extended mode
! Interruption.
\foo ->\advance \count 0 1
l.7 \foo
? i\resume
] )</usr/local/texlive/2012/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb
Output written on oops.pdf (1 page, 14709 bytes).
Transcript written on oops.log.
share|improve this answer
I like it! Quick question: why all the \relaxing? –  Loop Space Jan 30 '13 at 15:29
the \relax aren't needed in this version plan a was to stop the loop with a gobble command but depending on when exactly you hit ^C you needed to eat a different number of tokens so I stuck in some more so gobblefour was always safe but \let is better –  David Carlisle Jan 30 '13 at 15:33

If you set \pausing=1, TeX will stop after each line of input read from the file.

share|improve this answer
What does "stop" mean in this context? Will it halt completely, or will it wait for some "please continue" signal? Either way, it seems to suffer from one of the issues I don't want as I basically want to be able to asynchronously send a signal to TeX. –  Loop Space Jan 30 '13 at 15:09
@AndrewStacey: It will wait for user input. :-) –  Martin Schröder Jan 30 '13 at 15:28
Right, so this is an implementation of my "graceful" method. The problem with this one is that I have to keep telling it to continue. What it would enable is having a controlling script which actually handled the signals and told TeX to keep going unless the script received the right signal. So, useful, but not quite what I was looking for. –  Loop Space Jan 30 '13 at 15:31

Maybe setting a timer in \everypar? I used \pdfelapsedtime as shown in my post at Benchmarking various operations of TeX. In the MWE that illustrates the technique, I have used a simple

\ifdim\result sp <100000sp  \fibo \else Finish\fi

to do the time check. Code is rough and just a concept.


       \advance\numone  by  \numtwo  
       \savenumone=\the\numone  \numone=\numtwo  \numtwo=\savenumone 
       \advance\numbertimes  by 1 \ifnum \numbertimes<#1
    \ifnum\numbertimes=#1 \advance\numone  by   \numtwo\fi 




\ifdim\result sp <100000sp  \fibo \else Finish\fi



Change the "Finish" to \enddocument to terminate.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.