10

Example:

\documentclass{article}

\begin{document}


\section{Power series}\label{ps}

\begin{equation} \label{eq}
\sum_{i=0}^\infty a_i x^i.
\end{equation}

The equation \ref{eq} is a typical power series.

\section{Conclusion}
Look at Section~\ref{ps}.


\end{document}

In order to get the references, latex must be run twice, and an auxiliary file is created. Whereas, look at the following analogous (less verbose) OpTeX example:

\sec[ps] Power series 

$$
\sum_{i=0}^\infty a_i x^i. \eqmark[eq]
$$

The equation \ref[eq] is a typical power series.

\sec Conclusion
Look at Section~\ref[ps].

\bye

We need to run optex only once to get the references. Moreover, no auxiliary (.ref) file is created, because in this example references can be already resolved in one pass.

So, I don't understand why LaTeX always forces a two pass approach even when a single pass can work as well, and can deal with the most common kinds of references (future references are rarer).

14
  • 4
    You provide the answer to your own question in the final sentence: forward (or future) references. What is the basis for your claim "future references are rarer"? What would you do about citation call-outs, which are nothing but forward references to items in the bibliography, which (in the vast majority of real-world documents) occurs toward the end of the document?
    – Mico
    Commented Mar 20, 2021 at 7:20
  • 1
    Your hypothetical, "if I have no future references", pretty much provides the best answer to your question of why there's no variant of TeX or LaTeX that is designed not to work with forward references. When given a choice between two imperfect systems -- say, the current system, which requires two passes but gets by with a single executable; and a system that you might prefer but which requires users to actively switch to a different executable if and when their document takes on a more real-world flavor -- most users will gladly choose the one that's requires less active work of them.
    – Mico
    Commented Mar 20, 2021 at 8:00
  • 2
    latex exists since 30 years and I have lots of trust in the creativity of its package authors. So either someone already wrote an extension that does what you want or the need is not there. Check ctan.org/topic/label-ref. While it is certainly possible to implement this, I personally never had a document where I thought it would be nice to have this, With the exception of short tests, all my documents are compiled more than once anyway so that it doesn't matter. Commented Mar 20, 2021 at 9:27
  • 2
    Note, that when bib-reference list is after all \cite commands (which is very common) then you need only two passes: optex, optex without calling external program. LaTeX user needs four calls: latex, biber, latex, latex. Or, when creating Index, you need optex, optex (without calling any external program), but LaTeX user needs latex, makeindex, latex. :)
    – wipet
    Commented Mar 20, 2021 at 13:06
  • 1
    Actually, sometimes more than two passes are needed. It is even possible to write documents that never converge. Commented Mar 21, 2021 at 11:46

2 Answers 2

26

"Why" questions are not easy to answer, especially as the original system was designed for machines with a tiny fraction of the memory that current machines have, but..

It is true, as you point out, that it would be possible in some cases for backward counter references to be resolved in one pass, but this would not be possible for page references and not possible for forward references.

Firstly it is worth noting that this makes absolutely no difference in practice, so it's just a theoretical question:

  • If you are writing the file, very few people write the entire document in one session and just run latex once and use the pdf, so resolving cross references does not require any extra runs of latex. They are resolved by the time they need to be resolved.

  • If you are processing a complete document that you have been given as source then usually it contains a table of contents or list of figures or references to a bibliography, so would take multiple passes to resolve all references even if \label was changed to allow backward \ref in a single pass.

Technically \label causes an ordered pair (or quadruple with hyperref) of data for the current counter and page reference to be saved. By saving them to the aux file and reading them back at the start, both values are either known or not and usable for forward or backward references. This simplifies the implementation and especially it simplifies documenting the behaviour.

Note that optex was designed in an era when machines had much more available memory and by a different person, so it is not that surprising that it takes different design decisions in some cases, neither is right or wrong.

The fact that latex is documented as a multipass system with data stored to the filesystem between passes rather than in memory is just an optimisation to give the user the possibility of just using a single pass after each edit. You could just as easily advertise it as a system that resolves all cross references (as latexmk or context do for example) by doing multiple runs as an "internal implementation detail" and resolving all cross references before returning a PDF to the user.

2
  • I was reading pwt by Kees van der Laan, where he talks about his blue format that extends manmac.tex. He there emphases the one pass approach for references, hence my question :)
    – User
    Commented Mar 20, 2021 at 12:09
  • 2
    +1: Close to 600k! Commented Mar 20, 2021 at 18:00
13

You start from a wrong point of view.

With OpTeX you do get a .ref file written, in case one run is insufficient to solve cross references, which can happen in two cases:

  1. a future reference, or
  2. a label has been set in the vicinity of a page break, and the page number at the time the label is seen is not the same as what is at output time.

The first case is clear. For the second case, consider

Some text to artificially push the section too near the page break.

\vskip\dimexpr\vsize-4\baselineskip\relax

\sec[ps] Power series 

$$
\sum_{i=0}^\infty a_i x^i. \eqmark[eq]
$$

The equation \ref[eq] is a typical power series.

\sec Conclusion
Look at Section~\ref[ps] at page~\pgref[ps].

\bye

and you'll see that a .ref file will be created. The log will contain

WARNING l.18: Try to rerun to get references right.

Due to the asynchronous mechanism of page breaking, there is no way to resolve cross references, even past ones, in one pass. As you see, even past references may need two passes.

If you just want back references to numbers and not to pages, then it should work even with hyperlinks, but it seems quite a big limitation.

7
  • 1
    I really don't understand your example. The eq and ps labels are resolved in one pass, whereas the xx label is never resolved, because it is nowhere defined. The .ref file is created just because optex reasonably thinks that xx will appear later in the document. So what? What were you trying to show?
    – User
    Commented Mar 20, 2021 at 10:56
  • @User Sorry, used wrong version of the example.
    – egreg
    Commented Mar 20, 2021 at 11:09
  • Ok, thank you for the example. In any case, even without the \vskip\dimexpr\vsize-4\baselineskip\relax, optex writes to the .ref file, because it is aware of this eventuality. So, the pgref references are always handled by default with a two-pass approach.
    – User
    Commented Mar 20, 2021 at 11:44
  • @User Also per-page footnotes would need two passes. I see no real problem in running twice over the document: TeX syntax errors are almost unavoidable, not to mention typos.
    – egreg
    Commented Mar 20, 2021 at 11:47
  • 2
    It seems the @egreg's example not to be surprising. When \pgref is needed then two passes are needed because of asynchronous processing typessetting material by TeX. This is one of principle of TeX itself.
    – wipet
    Commented Mar 20, 2021 at 14:32

You must log in to answer this question.

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