1

I am using \documentclass{book}. There are 6 chapters. The first 4 have numbered sections, created using \section{}. The last two have no numbered sections, but I artificially add several to toc using the command \addcontentsline{toc}{section}{section name}. The problem I am facing is with the table of contents. What I want is the following:

  1. In the table of contents appearing at the beginning of the book, the first 4 chapters should appear with chapters and sections. Chapters 5 and 6 should appear only with chapter.

  2. In chapter 5, there should be a table of contents at the beginning, which prints in two multicolumns all the sections in chapter 5.

  3. In chapter 6, there should be a table of contents at the beginning, which prints in two multicolumns all the sections in chapter 6.

Is this possible? Thanks.

Edit (29th June 2023): I am editing the question a bit to update what I have been able to achieve so far, with help from the post here: different depths for etoc and toc. I am not able to solve the problem completely. By trying different things, I was able to come close to a solution. However, the solution works on some compilations, does not work on other compilations. It would be great to know a complete solution.

\documentclass[12pt,a4paper,fleqn]{book}
\usepackage{array}
\usepackage{tocloft}% always load before etoc

\usepackage{etoc}

\usepackage{amsmath,amsfonts,amsthm,mathrsfs,amssymb,amscd,comment,enumerate}
\usepackage[table]{xcolor}
\usepackage{hyperref}
\usepackage{geometry}
\geometry{bindingoffset=1cm}

\hypersetup{
    colorlinks   = true, %Colours links instead of ugly boxes
    urlcolor     = red, %Colour for external hyperlinks
    linkcolor    = blue, %Colour of internal links
    citecolor   = blue %Colour of citations, could be ``red''
}

\setlength{\textwidth}{20cm}
\setlength{\textheight}{23cm}
\hoffset=-3.0cm



\usepackage{multicol}

\begin{document}
    
    \begin{center}
        {\Large {\bf Title \footnote{Footnote}}}
        
        {\bf \today}
        
        
        {\Large {\bf Title\\}}
    \end{center}
    \etocsetnexttocdepth{1}
    \renewcommand{\contentsname}{Contents}
    \tableofcontents
    

    
    \chapter{Name of chapter 1}
    
    \section{Name of section 1, chapter 1}
    
    
    \section{Name of section 2, chapter 1} (click on the link) 
    
    
    
    
    \chapter{Name of Chapter 2}
    
    \section{Name of section 1, chapter 2}
    
    \section{Name of section 2, chapter 2}
    
    \section{Name of section 3, chapter 2}
    
    \subsection{Subsection}
        
    \section{Name of section 4, chapter 2}
    
    
    
    
    
    
    \chapter{Chapter 3}
    
    \section{Name of section 1, chapter 3}
    
    \section{Name of section 2, chapter 3}
    
    
    \chapter{Name of chapter 4}
    
    \section{Name of section 1, chapter 4}
    
    \section{Name of section 2, chapter 4}
    
    
    \chapter{Name of chapter 5}
    \etocsettocstyle{}{}
    \etocsetnexttocdepth{2}
    \begin{multicols}{2}
        \localtableofcontents
    \end{multicols}
    
    \pagebreak
    
    \addcontentsline{toc}{subsection}{L1}
    \addcontentsline{toc}{subsection}{L2}
    \addcontentsline{toc}{subsection}{L3}
    \addcontentsline{toc}{subsection}{L4}
    \addcontentsline{toc}{subsection}{L5}
    \addcontentsline{toc}{subsection}{L6}
    
    
    \chapter{Name of chapter 6}
    \etocsettocstyle{}{}
    \etocsetnexttocdepth{2}
    \begin{multicols}{2}
        \localtableofcontents
    \end{multicols}
    
    \pagebreak
    
    \addcontentsline{toc}{subsection}{M1}
    \addcontentsline{toc}{subsection}{M2}
    \addcontentsline{toc}{subsection}{M3}
    \addcontentsline{toc}{subsection}{M4}
    \addcontentsline{toc}{subsection}{M5}
    \addcontentsline{toc}{subsection}{M6} 

\end{document}

This works partially and in ways which I do not understand. If I comment out the line "\etocsettocstyle{}{}" after Chapter 5, compile twice, then add this line, then compile twice, then I get the table of contents in Chapter 5 as desired.

enter image description here

In this file the table of contents in Chapter 6 never appears. Although it does seems to work in other files, where the local table of contents in more than one chapter appears.

6
  • Welcome to TeX.SE! Please show us the code you have tried so far, then we do not have to guess, what you are doing ...
    – Mensch
    Jun 13, 2023 at 18:41
  • I was able to figure out with help from this answer here:tex.stackexchange.com/questions/686154/…
    – RSe
    Jun 13, 2023 at 19:04
  • @Mensch: Should I post my solution as an answer? My concern is if my question is a duplicate one.
    – RSe
    Jun 13, 2023 at 19:07
  • @RSe as your question has not been closed, it is useful to passers-by if you would be so kind as to post your solution and accept it...
    – user691586
    Jun 28, 2023 at 12:41
  • Actually I was not able to solve the problem completely. By trying different things, I was able to come close to a solution. However, the solution works on some compilations, does not work on other compilations. It would be great to know a complete solution.
    – RSe
    Jun 29, 2023 at 14:13

1 Answer 1

0

There a number of subtle things at works in your MWE.

  1. You should send a bug report to etoc author, because the tocloft detection fails with my TL2023 installation. As a result the command \etocsetnexttocdepth remains without effect for \tableofcontents because \tableofcontents is redefined by tocloft. Simplest work-around here is to use \etoctableofcontents for the main TOC.

  2. The various \addcontentsline of last chapter from your MWE are ignored because there is no typeset material and they get discarded; in real life this will not happen; to fix this an \mbox{} right before \end{document} suffices.

  3. There is something very weird with interaction with multicols environment: code such as

  \chapter{Name of chapter 5}
  \etocsettocstyle{}{}
  \begin{multicols}{2}
      \localtableofcontents
  \end{multicols}

appears to confuse etoc. On looking at the .toc file, the order of the line added by \chapter command there and some seemingly etoc internal is permuted, and as a result the \localtableofcontents prints nothing. The etoc manual mention multicols and has devoted commands so I think the code above should be replaced by

  \chapter{Name of chapter 5}
  \etocmulticolstyle{}
  \etocsetnexttocdepth{2}
  \localtableofcontents

or by

  \chapter{Name of chapter 5}
  \etocsetnexttocdepth{2}
  \etoclocalmulticol{}

I got very confused by this when experimenting because the .toc file gets updated and its effect is only on next compilation so always compile sufficiently many times or suppress all auxiliary files when updating. About multicols maybe check etoc documentation, Part II.

Here is the updated MWE which incorporates above conclusions. I commented out various irrelevant things. But make sure to read next proposal which is better below next.

\documentclass[12pt,a4paper,fleqn]{book}
\usepackage{array}
\usepackage{tocloft}% always load before etoc

\usepackage{etoc}

%\usepackage{amsmath,amsfonts,amsthm,mathrsfs,amssymb,amscd,comment,enumerate}
%\usepackage[table]{xcolor}
\usepackage{hyperref}
\usepackage{geometry}
%\geometry{bindingoffset=1cm}

\hypersetup{
    colorlinks   = true, %Colours links instead of ugly boxes
    urlcolor     = red, %Colour for external hyperlinks
    linkcolor    = blue, %Colour of internal links
    citecolor   = blue %Colour of citations, could be ``red''
}

% \setlength{\textwidth}{20cm}
% \setlength{\textheight}{23cm}
% \hoffset=-3.0cm

\usepackage{multicol}

\begin{document}
    
    \begin{center}
        {\Large {\bf Title \footnote{Footnote}}}
        
        {\bf \today}
        
        
        {\Large {\bf Title\\}}
    \end{center}
    \etocsetnexttocdepth{1}
    \renewcommand{\contentsname}{Contents}

\etoctableofcontents

\chapter{Name of chapter 1}
    
    \section{Name of section 1, chapter 1}
    
    \section{Name of section 2, chapter 1} (click on the link) 

\chapter{Name of Chapter 2}
    
    \section{Name of section 1, chapter 2}
    
    \section{Name of section 2, chapter 2}
    
    \section{Name of section 3, chapter 2}
    
    \subsection{Subsection}
        
    \section{Name of section 4, chapter 2}
    
\chapter{Chapter 3}
    
    \section{Name of section 1, chapter 3}
    
    \section{Name of section 2, chapter 3}
    
\chapter{Name of chapter 4}
    
    \section{Name of section 1, chapter 4}
    
    \section{Name of section 2, chapter 4}
    
\chapter{Name of chapter 5}
    \etocsetnexttocdepth{2}
    \etoclocalmulticol{}

    \clearpage
    
    \addcontentsline{toc}{subsection}{L1}
    \addcontentsline{toc}{subsection}{L2}
    \addcontentsline{toc}{subsection}{L3}
    \addcontentsline{toc}{subsection}{L4}
    \addcontentsline{toc}{subsection}{L5}
    \addcontentsline{toc}{subsection}{L6}

\chapter{Name of chapter 6}
    \etocsetnexttocdepth{2}
    \etoclocalmulticol{}

    \clearpage
    
    \addcontentsline{toc}{subsection}{M1}
    \addcontentsline{toc}{subsection}{M2}
    \addcontentsline{toc}{subsection}{M3}
    \addcontentsline{toc}{subsection}{M4}
    \addcontentsline{toc}{subsection}{M5}
    \addcontentsline{toc}{subsection}{M6} 

    \mbox{}
\end{document}

This all being said, the whole approach appears to be flawed. Using subsection only for some reason of tocdepth is not the good way. Besides there are warnings of hyperref which does not like going directly from chapter to subsection.

Package hyperref Warning: Difference (2) between bookmark levels is greater 
(hyperref)                than one, level fixed on input line 105.

So here is another approach which does use sections. It relies on the mechanism of depth tags, which I found in etoc documentation in part II, Control of contents.

We add

\etocdepthtag.toc{lastchapters}% add a depth tag to toc file
\etocignoredepthtags % ignore etoc depth tags for the local tocs, it will
                     % obeyed only by main TOC

in document body right before chapter 4 and insert

\etocsettagdepth{lastchapters}{chapter}

in preamble or at least before the main TOC in document body.

\documentclass[12pt,a4paper,fleqn]{book}
\usepackage{array}
\usepackage{tocloft}% always load before etoc

\usepackage{etoc}

%\usepackage{amsmath,amsfonts,amsthm,mathrsfs,amssymb,amscd,comment,enumerate}
%\usepackage[table]{xcolor}
\usepackage{hyperref}
\usepackage{geometry}
%\geometry{bindingoffset=1cm}

\hypersetup{
    colorlinks   = true, %Colours links instead of ugly boxes
    urlcolor     = red, %Colour for external hyperlinks
    linkcolor    = blue, %Colour of internal links
    citecolor   = blue %Colour of citations, could be ``red''
}

% \setlength{\textwidth}{20cm}
% \setlength{\textheight}{23cm}
% \hoffset=-3.0cm

\usepackage{multicol}
\etocsettagdepth{lastchapters}{chapter}
\begin{document}
    
    \begin{center}
        {\Large {\bf Title \footnote{Footnote}}}
        
        {\bf \today}
        
        
        {\Large {\bf Title\\}}
    \end{center}
    \renewcommand{\contentsname}{Contents}

\etoctableofcontents

\chapter{Name of chapter 1}
    
    \section{Name of section 1, chapter 1}
    
    
    \section{Name of section 2, chapter 1} (click on the link) 
    
\chapter{Name of Chapter 2}
    
    \section{Name of section 1, chapter 2}
    
    \section{Name of section 2, chapter 2}
    
    \section{Name of section 3, chapter 2}
    
    \subsection{Subsection}
        
    \section{Name of section 4, chapter 2}

\chapter{Chapter 3}
    
    \section{Name of section 1, chapter 3}
    
    \section{Name of section 2, chapter 3}
    
    
\chapter{Name of chapter 4}
    
    \section{Name of section 1, chapter 4}
    
    \section{Name of section 2, chapter 4}
    
    
\etocdepthtag.toc{lastchapters}% add a depth tag to toc file
\etocignoredepthtags % ignore etoc depth tags for the local tocs, it will
                     % obeyed only by main TOC
    

\chapter{Name of chapter 5}
    \etocmulticolstyle{}
    \localtableofcontents

%    \clearpage
    
    \section{L1}
    \section{L2}
    \section{L3}
    \section{L4}
    \section{L5}
    \section{L6}
    
    
\chapter{Name of chapter 6}
    % \etocmulticolstyle{} % already in effect
    \localtableofcontents

%    \clearpage
    
    \section{M1}
    \section{M2}
    \section{M3}
    \section{M4}
    \section{M5}
    \section{M6} 

    \mbox{}
\end{document}

This gives a main TOC

main TOC

and local TOCs as desired (maybe some extra control of vertical space should be added between chapter title and local TOC).

local TOC of chapter 5

A bug report regarding detection of tocloft should be addressed to etoc author: it fails with up-to-date TL2023.

4
  • I looked at etoc from TL2021 and regarding tocloft I see some piece of code there had \AtBeginDocument{\let\tableofcontents\etoctableofcontents} but in the current etoc from TL2023 (1.2a) the matching piece of the code does not have the \AtBeginDocument. This looks like a bug (as tocloft has not changed since 2017). So it is not so much the detection of tocloft which is problematic but the missing \AtBeginDocument, because tocloft itself waits for \begin{document} to modify \tableofcontents.
    – user691586
    Jul 1, 2023 at 18:11
  • Your solution works very well for me. Thank you very much for your great effort and explanations. I do not want sections, and so instead I used the command \addcontentsline{toc}{section}{L1}, etc. I do not know how to file a bug report. Is it possible to bring to the attention of the author of etoc this post? Thank you very much.
    – RSe
    Jul 2, 2023 at 17:54
  • @RSe I will write an email to etoc author (contact information is in the package pdf documentation). Glad it helped.
    – user691586
    Jul 2, 2023 at 19:34
  • Thank you very much.
    – RSe
    Jul 3, 2023 at 9:46

You must log in to answer this question.

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