tags:

views:

1703

answers:

5

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?

+2  A: 

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.

Damien Pollet
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
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
A: 

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.
indy
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
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
+1  A: 

What if you replace

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

with

\part{A Part of My Document}
Selinap
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
@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
A: 

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.

mas
+5  A: 

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!

Anton Geraschenko
This is what I ended up using. Thanks!
Ben Alpert