-1

This question already has an answer here:

enter image description here

I'm compiling my thesis.

The middle is LaTeX code, the right panel is the PDF file and the left panel is the navigation panel seen in Adobe Reader.

The order is Chapter1-AppendixQ-Chapter2 in the TeX code and the order is preserved in the left panel. But Chapter 2 and AppendixQ are reversed in the Table of Contents in the PDF file. (2.5 Data analysis and 2.6 Conclusion are sections in the chapter 2, A1 and A2 are sections in 'appendicex.' 'AppendixQ' is inserted using \addcontentsline command because the appendix title is not shown in the Table of Contents.

I don't understand why \include{chapter2} comes earlier than \addcontentsline{toc}{chapter}{AppendixQ}

marked as duplicate by egreg table-of-contents Jan 2 '17 at 12:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Hi, can you add a MWE, please? – Troy Jan 2 '17 at 4:39
  • @Troy It's too long to copy and paste here and it consists of multiple files (main tex file and each chapter). Could you tell me how I can post them? – user67275 Jan 2 '17 at 4:45
  • 1
    Do take some time to read the link that I posted in the previous comment. In essence, a MWE should contain the least amount of code and packages which causes the problem. How I usually go about doing this is by creating a copy of the entire thesis file, and start removing code and packages that I think are irrelevant, recompiling each time to see if the problem is still there. Once you narrow down your latex code, then post it here. – Troy Jan 2 '17 at 4:49
  • A tip: You can use backticks ` to mark your inline code as I did in my edit. – Heiko Oberdiek Jan 2 '17 at 8:08
2

The problem is based on a mix-up of deferred writing and immediate writing into the .aux file. Entries in the table of contents are first collected in the .aux files. At the next parsing of the .aux files, the entries are written to the .toc file with the same order as in the .aux files.

\addcontentsline or \addtocontents are deferred and written at the shipout time of the page, when the page number of the page is known. In opposite, \include immediately writes into the .aux file that now the .aux file for the included TeX file is included. Example:

\include{chapter_foobar}

writes

\@input{chapter_foobar.aux}

in the main .aux file.

Analysis of the problem in the question

Main .tex file:

\documentclass{report}
\usepackage[hidelinks]{hyperref}
\usepackage{bookmark}

\begin{document}
\tableofcontents
\clearpage % \include{chap1} calls \clearpage at the begin and end
\addcontentsline{toc}{chapter}{AppendixQ}
\include{chap2}
\end{document}

chap2.tex:

\chapter{My 2nd paper}

\addcontentsline for "AppendixQ" is called at the start of a new page. However, there is no material for a new page, when \clearpage is invoked by the following \include. Therefore, \@input{chap2} is immediately written by \include{chap2}. Then, later, when the next page is output, the entry for "AppendixQ" is written into the .aux file.

Main .aux file:

\@input{chap2.aux}
...
\@writefile{toc}{\contentsline {chapter}{AppendixQ}{2}{chapter*.1}}

chap2.aux contains:

\@writefile{toc}{\contentsline {chapter}{\numberline {1}My 2nd paper}{2}{chapter.1}}

Solution

The solution is to move \addcontentsline into the following file chap2.tex:

\addcontentsline{toc}{chapter}{AppendixQ}

\chapter{My 2nd paper}

The main .tex file:

\documentclass{report}
\usepackage[hidelinks]{hyperref}
\usepackage{bookmark}

\begin{document}
\tableofcontents
\clearpage % by \include{chap1}
\include{chap2}
\end{document}

Now, the main .aux file contains

\@input{chap2.aux}

and chap2.aux contains the entries for the table of contents in the correct order:

\@writefile{toc}{\contentsline {chapter}{AppendixQ}{2}{chapter*.1}}
\@writefile{toc}{\contentsline {chapter}{\numberline {1}My 2nd paper}{2}{chapter.1}}
  • Heiko, what a pitty, that I can only press once on the +1-button. :-/ Very well answered! – Jan Jan 2 '17 at 9:19
1

As I understand, appendix between chapters is required.

But since there is no MWE to work with,

the following answer: Appendix after each chapter

should help you out.

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