TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a (simple) recipe for producing LaTeX code requiring exactly n compilations? (That is, I'm looking for an algorithm/function f(n) with domain {1, 2, 3, ...} and LaTeX code as its range.) If that makes answering easier, I'll be happy about a solution requiring n to be above a certain threshold (say, 2 or 3), since most ordinary code requires not more than 2 or 3 runs of latex. Ideally the solution isn't too contrived and easy to comprehend; direct solutions as well as solutions with recursive characteristics are welcome.

Note: This question grew out of this question about patterns increasing the number of required LaTeX compilation runs.

share|improve this question
Isn't the ideal solution to have exactly one compilation? – morbusg Sep 23 '12 at 6:42
@morbusg Yep (I agree), this is more of a fun/theoretical question. It somehow grew in an obvious way out of another question (the one I linked to); so keeping separate answers/info in separate posts was an additional reason to create this question. – Lover of Structure Sep 23 '12 at 6:46
up vote 6 down vote accepted

Here is a small example that you can modify to give you as many compilations as you want:

\usepackage{refcount}% http://ctan.org/pkg/refcount
\setcounterref{section}{sec:foo}% Read label number from reference
\addtocounter{section}{-1}% Correct for stepping when issuing \section
\ifnum\value{section}<3% Modify this condition for different compiles
  \stepcounter{section}% section = section + 1
\section{A section}\label{sec:foo}% New label

The condition \ifnum\value{section}<3 requires 4 compiles to settle. So, in general, \ifnum\value{section}<N will require N+1 compiles. It would be possible to make the example even simpler.

The generic way LaTeX checks for a possible rerun is to scan the .aux file at \enddocument and see whether an existing label written to the .aux (say mylabel) matches that of what it is in the current document/compile (stored as \r@mylabel). If there is any difference in the macro definitions, a rerun warning is issued.

For completeness, a label can move around on the same page and within the same "unit of reference" and not trigger a rerun.

share|improve this answer

The longtable documentation (section 4) gives an example of a 3 column table that takes 4 passes to stabilise (and 5 passes for latex to know that it has stabilised). It is easy to generalise this to an n-column table taking n+1 runs.

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.