63

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.

15
  • 4
    We will need a minimal example.
    – Joseph Wright
    Commented Oct 6, 2011 at 11:37
  • 14
    @JosephWright That's the question! :)
    – egreg
    Commented Oct 6, 2011 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
    Commented Oct 6, 2011 at 12:01
  • 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
    Commented Jan 22, 2012 at 20:47
  • 1
    and of course, there's the answer in the tex faq ... which just says "tweak it". a real answer would be welcome. Commented Nov 8, 2012 at 15:25

6 Answers 6

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
1
  • 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. Commented Jul 15, 2022 at 13:21
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}
9
  • 2
    Cool, that's exactly what I was looking for. I hope @RoelofSpijker won't mind if I accept this answer instead. Commented Oct 30, 2012 at 14:01
  • 18
    For those who wonder and are too lazy to compile: this crucially relies on the fact that in roman numerals, the page number IV takes more space than the page number V. This wouldn't work with Arabic page numbers, since increasing page numbers take increasing space. Commented Aug 7, 2014 at 22:48
  • 1
    @yo' are you sure? that seems unlikely, unless you have a funky font where 9 is wider than 10. Commented Sep 17, 2019 at 9:21
  • 1
    @DavidCarlisle Ouch. Right, sorry that's my bad.
    – yo'
    Commented Sep 17, 2019 at 9:32
  • 3
    @yo' there are certainly fonts with 0 wider than 1, which could be used.... Commented Sep 17, 2019 at 9:52
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.

7
  • 1
    The output is different each time, the A might have the same size, but it is not on the same page. The output switches between having 0 and 1 blank pages prior to the Huge A. I would not call that stable at all. Commented Oct 6, 2011 at 11:56
  • 1
    As a matter of fact, thats incorrect. The A also switches between Huge and small size. Change the capital A to a lowercase a in the else branch to see this a little more clearly. Commented Oct 6, 2011 at 11:59
  • 6
    Now someone should put this into latexmk and watch it run forever.
    – Canageek
    Commented Jan 22, 2012 at 21:23
  • 3
    @Canageek: I got 'pdflatex' needed too many passes after latexmk compiled it five times. Commented May 1, 2012 at 18:48
  • 1
    @MartinScharrer Good to know that there is a built in safety for that.
    – Canageek
    Commented May 2, 2012 at 5:33
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?

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
  • 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.
    – user4686
    Commented Jan 22, 2016 at 16:57
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...

5
  • +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
    Commented Apr 15, 2021 at 9: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
    Commented Apr 15, 2021 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 Commented May 31, 2021 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
    Commented May 31, 2021 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. Commented May 31, 2021 at 11:46

You must log in to answer this question.

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