7

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.

3 Answers3

8

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

\documentclass{article}
\usepackage{refcount}% http://ctan.org/pkg/refcount
\begin{document}
\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
\fi
\section{A section}\label{sec:foo}% New label
\end{document}

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.

Werner
  • 603,163
7

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.

David Carlisle
  • 757,742
1

Adapting David Carlisle's answer in another question it's possible to work with normal pageref only.

In this case there's no need for Roman numbering because we only need to push text downwards.

\documentclass{article}
\usepackage{lipsum}
\begin{document}

\lipsum[1-150] \lipsum[1-150] \lipsum[1-150] \lipsum[1-96]

Some interesting text about something in section 1, which starts on page \pageref{x1}.

Some interesting text about something in section 1, which starts on page \pageref{x2}.

Some interesting text about something in section 1, which starts on page \pageref{x3}.

Some interesting text about something in section 1, which starts on page \pageref{x4}.

Some interesting text about something in section 1, which starts on page \pageref{x5}.

The text:

\label{x1}The text of an interesting section.

\label{x2}The text of an interesting section.

\label{x3}The text of an interesting section.

\label{x4}The text of an interesting section.

\label{x5}The text of an interesting section.

\end{document}

Output look like this.

output

This one only takes 6 runs to stabilize (1 more for LaTeX to know that it stabilized), but you can trivially see how to adapt it for larger n... as long as you can fit that many paragraphs into the first 99 pages. For larger text you can switch to the 999/1000 delimiter... but TeX counter only support up to 999999999/1000000000.

user202729
  • 7,143