[Tex/LaTex] Changing the order of TOC entries

table of contents

I am typesetting lecture notes in which I want to keep in chronological order and I've got the problem that there is something which belongs to chapter 2 after chapter 3.

I would like the table of contents entry of chapter 2 to be cohesive, but right now it looks like

TOC-old

while I intend it to look like

TOC-new

The inline code is as follows:

\documentclass{book}
\renewcommand{\thesection}{\arabic{section}}
\begin{document}
\tableofcontents

\chapter{Lorem 1}         % first chapter

\null\clearpage
\setcounter{page}{38}
\chapter{Lorem 2}         % second chapter
\section{Dolor sit}
\section{Amet consetetur}
\section{Adipisci elit}
\section{Sed eiusmod}     % fourth section

\null\clearpage
\setcounter{page}{52}
\chapter{Lorem 3}         % third chapter
\section{Dolor sit}
\section{Amet consetetur}
\section{Adipisci elit}
\section{Sed eiusmod}     
\section{Tempor incidunt} 
\section{Ut labore}

\null\clearpage
\setcounter{page}{74}
\setcounter{chapter}{1}
\chapter{Lorem 2 Encore}  % second chapter again
\setcounter{section}{4}
\section{Tempor incidunt} % fifth section

\null\clearpage
\setcounter{chapter}{3}
\chapter{Lorem 4}         % fourth chapter
\section{Dolor sit}
\end{document}

Any ideas on how to change the order of entries would be greatly appreciated.

Best Answer

The following is perhaps a bit more manageable in the long run:

enter image description here

\documentclass{book}
\usepackage{refcount}% http://ctan.org/pkg/refcount
\renewcommand{\thesection}{\arabic{section}}
\begin{document}
\tableofcontents

\chapter{Lorem 1}         % first chapter

\null\clearpage
\setcounter{page}{38}
\chapter{Lorem 2}         % second chapter
\section{Dolor sit}
\section{Amet consetetur}
\section{Adipisci elit}
\section{Sed eiusmod}     % fourth section
\addtocontents{toc}{%
  \protect\contentsline {section}{%
    \protect\numberline {\getrefnumber{sec:tempor-incidunt}}\protect\ignorespaces Tempor incidunt}{\getpagerefnumber{sec:tempor-incidunt}}}

\null\clearpage
\setcounter{page}{52}
\chapter{Lorem 3}         % third chapter
\section{Dolor sit}
\section{Amet consetetur}
\section{Adipisci elit}
\section{Sed eiusmod}
\section{Tempor incidunt}
\section{Ut labore}

\null\clearpage
\setcounter{page}{74}
\setcounter{chapter}{1}

\begingroup
\renewcommand{\addcontentsline}[3]{}
\chapter{Lorem 2 Encore}  % second chapter again
\setcounter{section}{4}
\section{Tempor incidunt} % fifth section
\label{sec:tempor-incidunt}
\endgroup

\null\clearpage
\setcounter{chapter}{3}
\chapter{Lorem 4}         % fourth chapter
\section{Dolor sit}
\end{document}

The idea is to write the content-entry in your document at the place similar to where you want it to appear in the ToC. It uses refcount to extract both the section and page number associated with a reference to the section that is placed later in the document. The adjustment to \addcontentsline is meant to locally remove the ToC-writing capability for the oddly-placed chapter/section.

Related Question