# Why do I have to typeset multiple times in TeXShop?

I'm very new to TeX, writing a book (using the book document class), and using TeXShop. I understand that TeX needs to make multiple passes in order to generate all the intermediate files it needs (indices, toc, glossaries, etc.), but why does it not know which intermediate files are out of date when I 'typeset' the document?

A Simple example:

% --- FRONT MATTER ---
\frontmatter
% Title
\input{./pre/title.tex}
% Preface
\chapter*{Preface}
\input{./pre/preface.tex}
% Contents
\tableofcontents
% --- MAIN MATTER ---
\mainmatter
% Introduction
\chapter*{Introduction}
\input{./pre/introduction.tex}
.
.
.
% --- BACK MATTER ---
\backmatter
% Glossary
\printglossaries


In the above example, if I remove the line \addcontentsline{toc}{chapter}{test} and press +t to typeset the document, the 'test' chapter is still in the contents, I have to typeset it again to correct it.

This is a very mature project that is written by programmers who intimately understand makefiles and source dependencies, these are all solved problems and it shouldn't be difficult to know when an intermediate file such as the .toc file is out of date. If this is a bug, or just a rough-edge in the workflow that hasn't been improved yet then fine, I can accept that, or maybe it's just not seen as important to the users and developers. However, it seems more likely to me that I'm just doing it wrong, or that there's a better/different workflow, or a command-line option I can add somewhere, that prevents these issues.

The brute-force solution would be to keep typesetting over and over again until none of the intermediate/output files change, but that's obviously not a very efficient process and it should really be done internally by TeX in a more intelligent way. How do I fix it?

• You don't have to typeset multiple times, you have to compile multiple times ;-) And (La)TeX works by writting information into external files. As soon as the information change the file has to be changed too. (La)TeX is different to a programming language which can pick up code from a shared library and it does not need to have it in linear order but has jumpers! A book/text is linear (usually), so the information has to be in order and as such recompilation is necessary. And it's actually not limited to TeXShop or whatever of those editors you use – user31729 Nov 30 '15 at 18:05
• So compiling multiple times is a 'normal' part of the workflow then? – jhabbott Nov 30 '15 at 18:11
• Yes, but normally compiling twice is sufficient. If this is too tedious, have a look on the \includeonly{...} and \include feature --> this will keep the references etc. and will only compile the relevant parts. – user31729 Nov 30 '15 at 18:15
• How would you propose do write a document in a single pass if it contains forward references? – David Carlisle Nov 30 '15 at 18:26
• @jhabbott no the main difference is that you are using a system written mostly around 1980 where doing two passes in memory simply was not an option. Tex doesn't usually have more than a page in memory: it tries to output the typeset result of one page before reading further into the file. Doing multiple passes just as required depending on which data changed is exactly what make or latexmk or arara do, latex itself just implements one pass. – David Carlisle Nov 30 '15 at 19:12

Contrary to a programming language such as C or Pascal etc. LaTeX is basically a linear compilation process, i.e. it can't look too far ahead in order to see what is coming in next chapters.

In fact, most compilers are actually multi-pass-compilers, they just do the compilation more than once and pretend it would be only one run.

The general rule is

pdflatex foo

bibtex   foo  %(or biber foo)
makeindex foo %( if there is an index (exception: imakeidx can do this automatically
makeglossaries foo %If needed
pdflatex foo %again
pdflatex foo %3rd run --> now all should be ok!


If too much compilation time is to be expected, use the \includeonly{foo1, foo2, foo100} macro and

\include{foo1}
\include{foo2}
.
.
.
\include{foo100}


for inclusion of sub files. (There's also a excludeonly package)

I've answered (amongst other users) the deeper details what is going on behind \addcontentsline and ToC in this question: Mimicking LaTeX's "table of contents" functionality