21

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?

6 Answers 6

14

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}
\newcommand\immediateaddcontentsline[3]{%
        \begingroup
        \let\origwrite\write
        \def\write{\immediate\origwrite}%
        \addcontentsline{#1}{#2}{#3}%
        \endgroup
}
\begin{document}

\tableofcontents
\newpage

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

\end{document}
1
  • 1
    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, 2011 at 2:07
12

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!

1
  • 2
    This "works" but breaks \includeonly, which is the main point of using \include (if one uses it correctly). EDIT: thanks for explaining why the bug arises. The reason for the delay is explained here: tex.stackexchange.com/a/10297/1340. Jul 25, 2017 at 2:07
3

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.

2
  • 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. Aug 1, 2009 at 18:01
  • 3
    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 :) Aug 1, 2009 at 18:08
2

What if you replace

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

with

\part{A Part of My Document}
2
  • 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. Aug 2, 2009 at 4:59
  • 1
    @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. Aug 2, 2009 at 23:58
2

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.
2
  • Except that I really want many different parts after the table of contents, with multiple included files for each. So that won't work. Aug 1, 2009 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, 2009 at 19:22
0

If you try the file

\documentclass{article}

\begin{document}

\tableofcontents
\newpage
\part{A Part of My Document}
\addcontentsline{toc}{part}{About 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}
\addcontentsline{toc}{part}{About A Part of My Document}
\include{includedfile}

\end{document}

Does this produce what you want?

Section 2.3 of Mittelbach and Goosens (2004), The LaTeX Companion (2nd edn) has lots on this, if you want more details.

You must log in to answer this question.

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