This is a well known problem with \addcontentsline (or \addtocontents) and \include; simply move the lines
\phantomsection
\addcontentsline{toc}{part}{Topic 1}
to the file intro.tex.
Let's take a look at the cause of the problem; for simplicity's sake let us consider the following sample file main.tex
\documentclass{memoir}
\begin{document}
\tableofcontents*
\mainmatter
\addcontentsline{toc}{part}{Topic 1}
\include{intro}
\chapter{Chapter 2}
\end{document}
and file intro.tex is something like
\chapter{Introduction}
Text
Of course, in a real document "Chapter 2" should also be written in its own .tex file and \included in main.tex, but this is not relevant for the explanation below.
Two things are important here:
- Each
\included file produces its own .aux file.
- The information about the table of contents is not written directly to the
.toc file; the \@writes go first to the .aux file(s) and only at the execution of \enddocument this information is read form the .aux file(s) and written to the .toc file.
Now, let's follow some of the steps of the processing of main.tex. Just before the \end{document} is reached, the files main.aux and main.toc are still empty, but, and this is crucial, the file intro.aux has already been written and contains (amongst other things) the line.
\@writefile{toc}{\contentsline {chapter}{\chapternumberline {1}Introduction}{1}}
When \enddocument is being executed, file main.aux will be processed and will contain the lines
\@writefile{toc}{\contentsline {part}{Topic 1}{1}}
\@writefile{lof}{\addvspace {10pt}}
\@writefile{lot}{\addvspace {10pt}}
\@writefile{toc}{\contentsline {chapter}{\chapternumberline {2}Chapter 2}{3}}
Also, the writes to the .toc file are done, but by the time this is done, the order is not the expected (remember that intro.aux was written first): the write associated to "Introduction" will be done first, then the one for "Topic 1" and finally the one for "Chapter2". In fact, the file main.toc will be
\contentsline {chapter}{\chapternumberline {1}Introduction}{1}
\contentsline {part}{Topic 1}{1}
\contentsline {chapter}{\chapternumberline {2}Chapter 2}{3}
The nex processing of the document will find the lines in that order and the lines will be typeset in the document in the wrong order.
Possible Solutions
Move the \addtocontents line to the beginning of intro.tex. Doing this, when intro.aux is written it contains the lines
\@writefile{toc}{\contentsline {part}{Topic 1}{1}}
\@writefile{lof}{\addvspace {10pt}}
\@writefile{lot}{\addvspace {10pt}}
\@writefile{toc}{\contentsline {chapter}{\chapternumberline {1}Introduction}{1}}
so now the \@writes to the .toc file will be in the right order:
\contentsline {part}{Topic 1}{1}
\contentsline {chapter}{\chapternumberline {1}Introduction}{1}
\contentsline {chapter}{\chapternumberline {2}Chapter 2}{3}
I understand that this might sound "hackish", but other alternative solutions (see below) are also more or less of the same nature.
If you don't need the \includeonly feature available for \include, you can use \input instead (notice however that \input doesn't issue \cleardoublepage or \clearpage internally). \input doesn't produce .aux files for the subsidiary documents and this will prevent the problem.
If you need to use \include and all your included files will take care of issuing \cleardoublepage or \clearpage when necessary (for example, if all the included files contain \chapter), then you can process your document as it is; this will produce the wrong order in the table of contents) but will let you profit from the \includeonly functionality and then, for the final version, replace all the \include commands for \input commands and perform a final complete processing (performing as many runs as the document requires).
Use a subsidiary file for the \addtocontents line and \include it in your main document. In the example, main.tex would look now like this:
\mainmatter
\include{topic1}
\include{intro}
\chapter{Chapter 2}
with topic1.tex containing the line
\addcontentsline{toc}{part}{Topic 1}
\included files)? – jamaicanworm Mar 30 '12 at 15:02