# ToC numbering problem

My LaTeX document is acting strangely. Here is a simplified version of it:

\documentclass{article}

\begin{document}

\tableofcontents
\newpage

\addcontentsline{toc}{part}{A Part of My Document}
\include{includedfile}

\end{document}


And in includedfile.tex:

\section{My Section Title}
Quack.


Clearly, in the table of contents, the heading for the part should precede the one for the section, but it doesn't! What's wrong?

-

## migrated from stackoverflow.comMar 20 '11 at 5:51

This question came from our site for professional and enthusiast programmers.

The delaying issue several people have mentioned is that TeX delays all \write commands until \shipout time. If for some reason, you need an immediate \write, you can use \immediate\write. To that end, here's a simple new macro that acts like \addcontentsline, but writes to the aux file immediately.

\documentclass{article}
\begingroup
\let\origwrite\write
\def\write{\immediate\origwrite}%
\endgroup
}
\begin{document}

\tableofcontents
\newpage

\immediateaddcontentsline{toc}{part}{A Part of My Document}
\include{includedfile}

\end{document}

-
This also solved a problem where \addcontentsline was not adding bookmark entries in the resulting PDF, unless there was additional text after the command. Bizarre. –  jevon Oct 13 '11 at 2:07

This is a tricky little issue. It turns out that \include differs from \input in an important way; it doesn't just add a couple of \clearpages. I think the right solution is to make a custom \include command which functions almost like the usual one:

\newcommand{\myinclude}[1]{\clearpage\input{#1}\clearpage}


When you use \addcontentsline, directly or indirectly, it writes a line on the aux file saying "write this and that to the toc file". Then it reads the aux file and follows that instruction. When you run latex again, the toc file has the right stuff in it and you get a nice table of contents.

But the tex \write command has some sort of delay to it (that I don't understand). When you use \addcontentsline several times in a row, it doesn't matter because they all go on the write stack in the right order. But here's the tricky part: when you use \include, it makes a separate aux file for the file you're including and immediately writes a command in the main aux file saying "go look at that other aux file for instructions" (with no weird delay). So if you use \include immediately after an \addcontentsline, the "go look at the other aux file" command gets written before the "write some stuff in the toc file" command. So all the contents entries from the included file get written first!

-
This is what I ended up using. Thanks! –  Ben Alpert Aug 3 '09 at 0:37

It works for me when I replace \include by \input.

I think \include is for chapters (it forces a \clearpage or something like that), so I never use it in practice.

-
I do actually want to have the included file on a separate page, though, and I'd also like to figure out why it doesn't work as written. –  Ben Alpert Aug 1 '09 at 18:01
You can add an explicit \clearpage or \cleardoublepage. The only thing you lose is that \include is for partial compilation (i.e. with \includeonly). As for why it does not work as written, I have no idea… experience with LaTeX shows that sometimes you probably don't want to understand :) –  Damien Pollet Aug 1 '09 at 18:08

What if you replace

\addcontentsline{toc}{part}{A Part of My Document}


with

\part{A Part of My Document}

-
Agreed. The usual sectioning commands call \addcontentsline, so it is generally not necessary to call it explicitly yourself. @Ben Alpert: If you have a special reason for making the explicit call, it might help to describe it. –  dmckee Aug 2 '09 at 4:59
@dmckee: The usual sectioning commands also produce other output. He probably doesn't want to have a separate page saying "Part I" that you flip through when you're reading. If you use \addcontentsline, the table of contents is the only place you'll get any output. –  Anton Geraschenko Aug 2 '09 at 23:58

Try moving the addcontentsline above the tableofcontents:

Updated: incorrect ordering occurs if \addcontentsline is on the same level as \include. A workaround is to have the \addcontentsline in the included file:

\documentclass{article}
\begin{document}
\tableofcontents
\newpage
\include{includedfile}
\include{some-other-file}
\end{document}


contents of includedfile.tex

\addcontentsline{toc}{part}{First Part of My Document}
\section{My Section Title}
Quack.

-
Except that I really want many different parts after the table of contents, with multiple included files for each. So that won't work. –  Ben Alpert Aug 1 '09 at 18:05
Reading through some latex documentation there seems to be a problem using \addcontentsline at the same level as an \include statement. The solution is to move the \addcontentsline into the file loaded by \include. Not the cleanest solution but it will allow you to have multiple parts correctly ordered in the table of contents. –  indy Aug 1 '09 at 19:22

If you try the file

\documentclass{article}

\begin{document}

\tableofcontents
\newpage
\part{A Part of My Document}
\include{includedfile}

\end{document}


You may get a clue as to what is happening.

The /addcontentsline instruction is normally invoked automatically by the document sectioning commands... If you do not want a heading number (starred form) but you do want an entry in the .toc file, you can use \addcontentsline with or without \numberline ... (Mittelbach and Goosens (2004), see below)

Hence, for example,

\documentclass{article}

\begin{document}

\tableofcontents
\newpage
\part*{A Part of My Document}