Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Mar 20 '11 at 5:51

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

add comment

6 Answers 6

up vote 5 down vote accepted

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}
share|improve this answer
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 '11 at 2:07
add comment

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!

share|improve this answer
    
This is what I ended up using. Thanks! –  Ben Alpert Aug 3 '09 at 0:37
add comment

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.

share|improve this answer
    
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
2  
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
add comment

What if you replace

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

with

\part{A Part of My Document}
share|improve this answer
    
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
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. –  Anton Geraschenko Aug 2 '09 at 23:58
add comment

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.
share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.