4

I have large document with many chapters and many sections and subsections. (one subsection per problem) and there can be hundreds of problems in each section.

I use \localtableofcontents to make table of content in each section. But the table of content gets jammed after a while and the subsection numbers starts moving over to the right over the name of the subsection.

I show a MWE below. But here is a screen shot from one example.

enter image description here

To reproduce, I made this MWE which generates lots of chapters and sections and for the final section, I added \localtableofcontents. This needs to be compiled at least two times to see the problem in order to build the toc.

\documentclass[12pt]{book}
\usepackage{tocloft}
\usepackage{etoc} 
\usepackage{ifthen}
\newcounter{int}

\begin{document}          
\setcounter{int}{1}
\loop
\chapter{This is my chapter number \theint}
stuff
\addtocounter{int}{1}
\ifnum \value{int}<45
\repeat

\setcounter{int}{1}
\loop
\section{This is my section number \theint}
stuff
\addtocounter{int}{1}
\ifnum \value{int}<100
\repeat

\etocsetnexttocdepth{2}
\etocsettocstyle{\subsection*{Local contents}}{}
\cftsubsecindent 0pt
\localtableofcontents

\setcounter{int}{1}
\loop
\subsection{This is my subsection number \theint}
stuff
\addtocounter{int}{1}
\ifnum \value{int}<1000
\repeat

\end{document}

Compiling the above and going down the pdf for the last section, the toc looks like this

enter image description here

What to change to make the numbers on the left not go over the subsection names?

TL 2021, and using lualatex only.


Update

I found some side effect using the solution given below. It affect the main table of the content of the main document. Here is an example. Without using the macros shown below this is the result, which is what is expected

\documentclass[12pt]{book}
\usepackage{tocloft}
\usepackage{etoc}  
\begin{document}           
\title{my title}
\maketitle
\tableofcontents
 
\chapter{ chapter one}       
\section{ Test file number 34}
Test folder name

\section{ Test file number 35}
Test folder name    
\end{document}

enter image description here

Now after adding the macros given in the answer below, this is the output of the main table of content. Notice the difference

\documentclass[12pt]{book}
\usepackage{tocloft}
\usepackage{etoc}  

\newlength{\npageW}
\settowidth{\npageW}{99999} % width of the page number box
\newlength{\subnumberW}
\settowidth{\subnumberW}{999.999.999} % width of the subsection number box<<<<

\newcommand*{\DotsAndPage}
{\nobreak\leaders\hbox{\normalsize\hbox to 1.75ex {\hss.\hss}}% make dots
\hfill\nobreak\rlap{\makebox[\npageW]{\etocpage}}\par}

\etocsetstyle{subsection}
{}
{\normalfont\addvspace{.5ex}\noindent\setlength{\leftskip}{7em}\setlength{\rightskip}{2em}}
{\llap{\makebox[\subnumberW][l]{\etocnumber}}\etocname\DotsAndPage}
{}    

\begin{document}           
\title{my title}
\maketitle
\tableofcontents
 
\chapter{ chapter one}       
\section{ Test file number 34}
Test folder name

\section{ Test file number 35}
Test folder name    
\end{document}

enter image description here

Why did the main document table of content change? I only wanted the local table of content for the section changed to fix the problem with spacing.

1 Answer 1

1

You can define a style for the subsection (in this case) like

%% ***************************************** added <<<<
\usepackage{showframe} % only to show margins <<<<<<<<<

\newlength{\npageW}
\settowidth{\npageW}{99999} % width of the page number box
\newlength{\subnumberW}
\settowidth{\subnumberW}{999.999.999} % width of the subsection number box<<<<

\newcommand*{\DotsAndPage}
{\nobreak\leaders\hbox{\normalsize\hbox to 1.75ex {\hss.\hss}}% make dots
\hfill\nobreak\rlap{\makebox[\npageW]{\etocpage}}\par}

\etocsetstyle{subsection}
{}
{\normalfont\addvspace{.5ex}\noindent\setlength{\leftskip}{7em}\setlength{\rightskip}{2em}}
{\llap{\makebox[\subnumberW][l]{\textit{\etocnumber}}}\etocname\DotsAndPage}
{}
%**********************************************************

Each \etocsetstyle command has five mandatory arguments:

\etocsetstyle{⟨levelname⟩}{⟨start⟩}{⟨prefix⟩}{⟨contents⟩}{<finish>}

After compiling twice:

e

This is the complete code.

\documentclass[12pt]{book}
\usepackage{tocloft}
\usepackage{etoc} 
\usepackage{ifthen}
\newcounter{int}

%% ***************************************** added <<<<
\usepackage{showframe} % only to show margins <<<<<<<<<

\newlength{\npageW}
\settowidth{\npageW}{99999} % width of the page number box
\newlength{\subnumberW}
\settowidth{\subnumberW}{999.999.999} % width of the subsection number box<<<<

\newcommand*{\DotsAndPage}
{\nobreak\leaders\hbox{\normalsize\hbox to 1.75ex {\hss.\hss}}% make dots
\hfill\nobreak\rlap{\makebox[\npageW]{\etocpage}}\par}

\etocsetstyle{subsection}
{}
{\normalfont\addvspace{.5ex}\noindent\setlength{\leftskip}{7em}\setlength{\rightskip}{2em}}
{\llap{\makebox[\subnumberW][l]{\textit{\etocnumber}}}\etocname\DotsAndPage}
{}
%**********************************************************

\begin{document}    
    
              
    \setcounter{int}{1}
    \loop
    \chapter{This is my chapter number \theint}
    stuff
    \addtocounter{int}{1}
    \ifnum \value{int}<45
    \repeat
    
    \setcounter{int}{1}
    \loop
    \section{This is my section number \theint}
    stuff
    \addtocounter{int}{1}
    \ifnum \value{int}<100
    \repeat
    
    \etocsetnexttocdepth{2}
    \etocsettocstyle{\subsection*{Local contents}}{}
    \cftsubsecindent 0pt
    \localtableofcontents
    \setcounter{int}{1}
    \loop
    \subsection{This is my subsection number \theint}
    stuff
    \addtocounter{int}{1}
    \ifnum \value{int}<1000
    \repeat
    
\end{document}

UPDATE after a follow-up question.

In practice, it is necessary to define the upper levels: section and chapter to obtain a coherent table of contents, taking into account the maximum number of digits of sections and subsections.

For example

%% ***************************************** added <<<<
\usepackage{showframe} % only to show margins <<<<<<<<< 
    
\newlength{\chapterW}
\settowidth{\chapterW}{999} % width of the chapter number box   
\newlength{\pageW}
\settowidth{\pageW}{99999} % width of the page number box
\newlength{\snumberW}
\settowidth{\snumberW}{999.999} % width of the section number box<<<<
\newlength{\subnumberW}
\settowidth{\subnumberW}{999.999.999} % width of the subsection number box<<<<

\newcommand*{\DotsAndPage}
{\nobreak\leaders\hbox{\normalsize\hbox to 1.75ex {\hss.\hss}}% make dots
    \hfill\nobreak\rlap{\makebox[\pageW]{\etocpage}}\par}

\etocsetstyle{subsection}
{}
{\normalfont\addvspace{.5ex}\noindent\setlength{\leftskip}{\dimexpr \chapterW + \snumberW + 5.5em}\setlength{\rightskip}{2em}}
{\llap{\makebox[\subnumberW][l]{\textit{\etocnumber}}}\etocname\DotsAndPage}
{}

\etocsetstyle{section}
{}
{\normalfont\addvspace{.5ex}\noindent\setlength{\leftskip}{\dimexpr \chapterW + 3.5em}\setlength{\rightskip}{2em}}
{\llap{\makebox[\snumberW][l]{\textit{\etocnumber}}}\etocname\DotsAndPage}
{}

\etocsetstyle{chapter} 
{}
{\bfseries\addvspace{.8ex}\setlength{\leftskip}{1.5em}\setlength{\rightskip}{2em}\noindent}
{\llap{\makebox[\chapterW][l]{\etocnumber}}\etocname \hfill\nobreak\rlap{\makebox[\pageW]{\etocpage}}\par}
{}  
%   **********************************************************

x

y

Remark

You can modify the formatting of subsection only for a local table of contents (for example to set a smaller indent)

      ...
    \etocsetnexttocdepth{2}     
    \etocsetstyle{subsection}
    {}
    {\normalfont\addvspace{.5ex}\noindent\setlength{\leftskip}{7em}\setlength{\rightskip}{2em}}
    {\llap{\makebox[\subnumberW][l]{\textit{\etocnumber}}}\etocname\DotsAndPage}
    {}      
    \etocsettocstyle{\subsection*{Local contents}}{}
    ....
2
  • Thanks for the effort. I found some side effect to the above. Now the main table of content do not display correctly as before. I will post a MWE example now at bottom of my question.
    – Nasser
    Feb 14, 2022 at 2:53
  • 1
    @Nasser Please see the updated answer. Thank you for your feedback! Feb 14, 2022 at 14:22

You must log in to answer this question.

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