3

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.

4
  • 1
    Hard to say without looking at your code. Please provide a minimal example.
    – daleif
    Jul 27, 2014 at 13:19
  • @Sora. - I've taken the liberty of augmenting your code snippets into a compilable example. Feel free to modify it further to suit your objectives.
    – Mico
    Jul 27, 2014 at 14:24
  • 1
    You could use \addcontentsline earlier to add the contents for the out-of-order sections and then prevent TeX writing a toc entry when you actually typeset them e.g \chapter[]{Chapter title}, I think. (Or does this add a numbered entry with no content?)
    – cfr
    Jul 27, 2014 at 22:33
  • I don't know but I just solved the problem myself, see below, but thanks anyway!
    – Sora.
    Jul 27, 2014 at 22:35

2 Answers 2

4

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.

1
  • Thanks a lot, this is awesome. However I should have noted I am using the hyperref package which redefines \addtocontents to use one more parameter so it wouldn't compile at first and when I added an empty parameter, the hyperlink was broken. But I was able to make it work by adding \label{ch:tempor-incidunt} after \chapter and putting {section.\getrefnumber{ch:tempor-incidunt}.\getrefnumber{sec:tempor-incidunt}} into the \addcontents parameters. Now it works like a charm.
    – Sora.
    Jul 28, 2014 at 22:33
0

Got it but gonna post it anyway in case someone might need this one day.

I looked up the definition of \tableofcontents in the document class file scrreprt.cls

\newcommand*\tableofcontents{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \chapter*{\contentsname
        \@mkboth{\contentsname}{\contentsname}}%
    \@starttoc{toc}%
    \if@restonecol\twocolumn\fi
    }

and figured out that \@starttoc{toc} is the TeX macro which generates the ToC content.

So I ran \tableofcontents as usual, manually edited the corresponding .toc file and included it this way:

\makeatletter
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \chapter*{\contentsname
        \@mkboth{\contentsname}{\contentsname}}%
%    \@starttoc{toc}%
    \if@restonecol\twocolumn\fi
\makeatother
\input{script.toc}

Now it looks exactly as I intended:

Got it

2
  • 1
    I think you should (in case you haven't already done so) upvote and accept @Werner's answer. His solution is more flexible and doesn't require you to hand-edit the .toc file or have to remember to disable \@starttoc.
    – Mico
    Jul 28, 2014 at 18:11
  • @Mico Now that I saw his answer, I did upvote and accept it. It didn't work right away and caused the document to fail to compile but that was my fault since I should've noted my document uses the hyperref package. However, I was able to tweak it and make it work, as I noted in the comment under his answer.
    – Sora.
    Jul 28, 2014 at 22:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .