62

It is an unfortunate reality that one must compile a LaTeX file several times in order to get all labels and citations correct. As far as I know, one should go on compiling as long as TeX warns that that "labels may have changed", and two or three passes may not be enough.

Is it possible to write a LaTeX document that never stabilizes, i.e., no matter how many times I compile it, it will go on complaining that labels have changed?

I am thinking about something like a \pageref{something} written in a large font that moves from page 9 to page 10, which requires longer to typeset and thus makes the corresponding label change page and so on ad infinitum.

lockstep
  • 250,273
  • 4
    We will need a minimal example. – Joseph Wright Oct 06 '11 at 11:37
  • 14
    @JosephWright That's the question! :) – egreg Oct 06 '11 at 11:50
  • 1
    Due to the halting problem, it isn't even possible to find out, if the document will eventually compile without label-changes. – FUZxxl Oct 06 '11 at 12:01
  • it's possible that with some add-on packages, this warning becomes "pathological". we know it's true with the package amsbooktoc (which is sent out to ams authors only on request, so it's not in general distribution), but haven't had time to track down the source of the problem yet. however, after the number of runs normally required to get a good table of contents, the document is stable, so we just warn our authors about what they should check for. but i believe this is a different situation than you are describing. – barbara beeton Oct 06 '11 at 12:17
  • @FUZxxl, I'm not 100% sure, but I don't think that is relevant here. The halting problem is deciding whether or not a certain program ever halts given a certain input. If you provide latex with the tex file from my answer as input, we can observe that it halts. This gives rise to new input, the same tex file + the aux file produced. Which leads to a new aux file as output. This again becomes the input, etc. The aux file only has two states though, so we are only working with 2 distinct inputs (disregarding the initial run with no aux) which lead to eachother as output. – Roelof Spijker Oct 06 '11 at 12:26
  • (cont.) This leads to a circular program, which won't halt as long as we assume latex is consistent, i.e. given the same input, the output is also the same. – Roelof Spijker Oct 06 '11 at 12:27
  • 7
    @FUZxxl That isn't true: That just states that there exists a class of problems exists for which you cannot prove if they will halt or not. Not all problems belong to that class. For example, you can mathematically prove that while(n>0){n++;} will go forever, and that n=10;while(n<10){n--}; will halt. – Canageek Jan 22 '12 at 20:47
  • @FUZxxl All we have to do is describe a simple system as a state machine and show that it will move between these two states forever. – Canageek Jan 22 '12 at 20:49
  • @Canageek Well, since TeX is turing-complete, it is impossible to tell whether a document will eventually compile without label changes in general. Of course, you can make proofs for an infinite number of documents. – FUZxxl Jan 23 '12 at 17:43
  • @FUZxxl Well no, there should be a class of TeX documents that you can't prove it for. Most ones you could follow the algorithm through and see if it will end, much like most C programs. It is just POSSIBLE to answer a question you can't predict. – Canageek Jan 24 '12 at 03:13
  • @Canageek Isn't that exactly what I say? The number of provable (non)terminating documents is of course infinite; so is the number not-provable documents. – FUZxxl Jan 24 '12 at 13:47
  • @FUZxxl Ah, I was referring to the average usage case, not the theoretical number of documents. – Canageek Jan 24 '12 at 19:54
  • @JosephWright i have several such documents squirreled away; one was created when debugging footmisc, and contains pathologically many footnotes, which oscillate position (i know where this one is). i've also come across the problem with a couple of "real" documents, but would find it hard to retrieve them from archive. – wasteofspace Nov 08 '12 at 15:20
  • 1
    and of course, there's the answer in the tex faq ... which just says "tweak it". a real answer would be welcome. – wasteofspace Nov 08 '12 at 15:25
  • 1
    This question seems relevant: https://tex.stackexchange.com/questions/122116/how-to-prolong-compilation-time-while-engaging-in-leisure-activities – Steven B. Segletes Apr 15 '21 at 10:39

6 Answers6

49

Having an explicit \ifthenelse test makes the document look like the looping is intentional. It is possible for the cross referencing not to converge even if there is no explicit conditional switching within the file. This document for example.

\documentclass{article}

\pagenumbering{Roman} \begin{document}

a\clearpage b\clearpage c\clearpage

\begin{figure}[!t] \framebox(200,430){} \caption{a figure to take up space} \end{figure}

Some interesting text about something in Section \ref{x}, which starts on page \pageref{x}.

\section{zzz\label{x}} The text of an interesting section. \end{document}


Or this version using arabic proportional numbers

\documentclass[a5paper]{article}
\usepackage{cfr-lm}
\renewcommand\shapedefault{it}
\begin{document}

\section{Introduction}

\title{test}

\tableofcontents

\clearpage

\section{Zzz} more text\ more text

\subsection{Zzzz} more text\ more text\ more text

\subsection{Zzzzz} more text\ more text\ more text\ more text\ more text\ more text

\subsection{Zzzzzz} more text\ more text

There is some text, in Section~\ref{z} on page~\pageref{z}. That has a, b, c.

\section{ZZZ\label{z}} A, B, C.

\end{document}

David Carlisle
  • 757,742
37

Quickly testing something, this seems to give the warning each time it is compiled.

\documentclass{article}
\usepackage{ifthen}
\begin{document}
\newcommand{\tes}[1]{\ifthenelse{\equal{#1}{1}}{\Huge A}{\small A}}
\vspace*{.95\textheight}
\tes{\pageref{test}}
\label{test}
\end{document}

So it seems that it is possible. Basically, \tes puts a huge A in front of the label if the label is on page 1, moving its location to page 2. If it's on page 2, \tes puts a small A in front of it instead, moving it back to page 1. Changed the code a little bit, no longer needs lipsum, just uses a vspace now. If you just run latex file.tex you will see the output switches between a 1 page dvi and a 2 page dvi after each run.

Roelof Spijker
  • 17,663
  • 5
  • 55
  • 63
20

I accidentally created such a document for real. I used biblatex-icomp. Two consecutive citations of the same source happened to be in the vicinity of a page break. Then apparently the following happened (citing Paul Stanley's accurate description):

  1. On the first run the citation seems to fit on the first page, so the aux file records "this citation was on page 1".

  2. Next time round, biblatex looks at this, and says "OK, that fit on page 1, so I'll use ibid". Unfortunately, doing that forces the citation onto the next page, so this time the aux file records: "citation was on page 2". And biblatex (which is using "old information" about where the citation appears) wrongly prints the citation as an "ibid".

  3. All would be well if the citation stayed on page 2, because biblatex would put things right next time it ran and it would get back in sync. But in fact next time, because a full citation is used, it moves back to page 1! So this time you end up with a full citation on page 1.

  4. And next time ... you get the picture.

A working example and Paul Stanley's explanation can be found at the question Avoid infinite compiler passes with biblatex ibid citation styles?

Faheem Mitha
  • 7,778
mach
  • 923
8

For the fun of it. See also The LaTeX runs (with varioref) never settle down to a stable final state.

\documentclass[a4paper]{article}
\usepackage{geometry}

\begin{document}

\pagenumbering{Roman}
I\clearpage
II\clearpage
III\clearpage
IV\clearpage
V\clearpage
VI\clearpage
VII\clearpage

\section{foo}

xxxx xxxx 
xxxx xxxx 
xxxx xxxx 
xxxx xxxx 
xxxx xxxx 
xxxx xxxx xxxx xx
See section on page~\pageref{bar}

\vspace*{17.4cm}

\section{bar}\label{bar}

Where am I ?

\end{document}
  • 1
    ah sorry, I had not seen that Roman numerals were also in use in @DavidCarlisle answer from 2012. Here VIII has two more characters than IX. With monotype font, would be easier yet. –  Jan 22 '16 at 16:57
6

For the sake of completeness, this is an example that uses neither proportional Arabic number nor bibtex/Roman page numbering/conditional.

%! TEX program = pdflatex
\documentclass{article}
\usepackage{lipsum}
\usepackage{color}
\begin{document}
\lipsum[1-48]

Embarking on a journey, humanity delves into the mysteries of existence. In the expansive realm of human understanding, the intricacies of quantum mechanics and neurobiology converge, offering glimpses into the mysteries of \label{a}\textcolor{red}{the} universe. Amidst the pages of exploration, on page \textcolor{red}{\pageref{a}}, fundamental concepts intertwine, inviting curiosity and sparking intellectual endeavors. Within this captivating tapestry, the relentless pursuit of understanding continues, fueled by the perpetual quest for enlightenment and innovation. \end{document}

(Some nonsense text that looks more interesting than lipsum was generated with ChatGPT.)

Output: (dynamic GIF image)

output

Remaining challenges:

  • something that makes the cycle length not 2
user202729
  • 7,143
  • 1
    Thanks! I'm switching to this answer as accepted to denote that this is the current "state-of-the-art" example that improves on all previous ones and is more natural-looking. It's good to see that this question attracts interest even 10 years after it was posed. – Federico Poloni Jul 15 '22 at 13:21
3

Based on @DavidCarlisle 's answer (which relies on roman page numbers being used) and his comment, I have created a document with arabic page numbers:

\documentclass[11pt]{article}
\usepackage[proportional]{libertine}

\begin{document}

There is nothing of interest on this page.\clearpage

There is nothing of interest on this page.\clearpage

There is nothing of interest on this page.\clearpage

There is nothing of interest on this page.\clearpage

There is nothing of interest on this page.\clearpage

There is nothing of interest on this page.\clearpage

There is nothing of interest on this page.\clearpage

There is nothing of interest on this page.\clearpage

There is nothing of interest on this page.\clearpage

\begin{figure}[!t] \centering \framebox(260,420){} \caption{A figure to take up space} \end{figure}

Here is some text referring to something in section \ref{mylabel} (which starts on page \pageref{mylabel}).

\section{A very interesting section\label{mylabel}}

The text of an interesting section.

\end{document}

The font used in this document is Linux Libertine with proportional figures. "proportional figures" means that the characters 0123456789 do not have the same width. For example, 0 is (slightly) wider than 1.

The sentence Here is some text referring to something in section \ref{mylabel} (which starts on page \pageref{mylabel}). on page 10 just fits in one line if \pageref{mylabel} is 11, but it occupies two lines if \pageref{mylabel} is 10. (This is because 0 is slightly wider than 1.)

Now the following happens: If the sentence refers to page 11, it fits in one line and the section fits on page 10. If the sentence refers to page 10, it occupies two lines, the section does not fit on page 10 and is moved to page 11...

  • +1 Nice example! Just one suggestion: to make the code shorter you can just replace the whole one-liners by a single \setcounter{page}{10} just before \begin{figure}. – campa Apr 15 '21 at 09:46
  • 1
    Well, I thought it was funnier this way :). Of course \setcounter{page}{10} is shorter. Another option would be to fill nine pages with an appropriate amount of dummy text from the lipsum or blindtext package. – user227621 Apr 15 '21 at 10:27
  • using 0/1 you need to go to 10 I have somewhere an example using proportional figues with 3 wider than 4 so takes less filler – David Carlisle May 31 '21 at 11:31
  • @DavidCarlisle That would be very funny :). Creating such an example is of course even more demanding, since the difference in width between "3" and "4" is even smaller than between "0" and "1. – user227621 May 31 '21 at 11:38
  • 1
    @user227621 a few grep later I found it (actually it is 2 wider than 3) I added it to my answer. – David Carlisle May 31 '21 at 11:46