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.

I have a table of contents in my memoir book. I want there to be one line for each chapter, in addition to headings for each set of similar chapters.

For some reason, the line for the first chapter appears before the first phantom line.

\documentclass{memoir}

\begin{document}

\frontmatter
\tableofcontents*

\mainmatter

\phantomsection
\addcontentsline{toc}{part}{Topic 1}

\include{intro}

\chapter{Chapter 2}

This is the second chapter.

\phantomsection
\addcontentsline{toc}{part}{Topic 2}

\chapter{Chapter 3}

This is the third chapter.

\chapter{Chapter 4}

This is the fourth chapter.

\end{document}

The file intro.tex in the same directory contains the following:

\chapter{Introduction} \label{chap:intro}

\noindent This is the introductory chapter.

I've noticed that if I paste the contents of intro.tex into the main file (instead of using \include), the problem is fixed--the "Introduction" line in the table of contents appears before "Topic 1"! However, this isn't a good solution, since I have large chapter files which I want to include separately...

What might be causing this problem? And is there a way to fix it?

share|improve this question
    
Gonzalo's solution works, but I'm still wondering if there might be something more elegant (which doesn't require changing anything in the \included files)? –  jamaicanworm Mar 30 '12 at 15:02
1  
As I said, it's a well known problem and the "standard solution" is the one I mentioned in my answer. I can give you one or two possible alternatives, but I am not sure they will be "more elegant"; anyway, the change to be done is only adding two lines, so I don't see any big problem. –  Gonzalo Medina Mar 31 '12 at 0:17
1  
I've now updated my answer with an explanation of the cause of the problem and a description of several possible solutions. –  Gonzalo Medina Mar 31 '12 at 16:06

1 Answer 1

up vote 7 down vote accepted

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:

  1. Each \included file produces its own .aux file.
  2. 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

  1. 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.

  2. 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.

  3. 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).

  4. 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}
    
share|improve this answer
    
Excellent explanation! Thank you. –  jamaicanworm Mar 31 '12 at 16:25

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.