1

can someone help me adjust my TOC with the MWE code which I am going to provide.

I am trying to add dots between the titles and the page number.

The title for the section 2.1, chapter 3 and subsection 3.1.1 is so long that the page number is shown on the second line. What do i need to do in order for the page number to be right aligned on the second line and also be connected with dots.

The last thing I am trying to change is the following, i want the space between 3.1.2 and 3.2 to tbe the same as 3.1 and 3.1.1

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

\etocsetlevel{section}{1}
\etocsetlevel{subsection}{2}

\etocsettocdepth{subsection}

\newlength{\tocleft}
\setlength{\tocleft}{2.5cm} % adjust to fit the inner margin defined in geometry

\newlength{\tocsep}
\setlength{\tocsep}{2em}

\etocsetstyle{chapter}
   {}
   {}
   {\etocifnumbered
     {\makebox[0pt][r]{\textbf{\etocnumber}\hspace{\tocsep}}%
      \textbf{\etocname}\hfill\etocpage\par}
    {\textbf{\etocname}\hfill\etocpage}%
    \par\vspace{3ex}}
   {}

\etocsetstyle{section}
   {\vspace{-2ex}}
   {}
   {\makebox[0pt][r]{\etocnumber\hspace{\tocsep}}%
    \etocname\hfill\etocpage\par}
   {\addvspace{3ex}}

\etocsetstyle{subsection}
   {\vspace{0ex}}
   {}
   {\makebox[0pt][r]{\etocnumber\hspace{\tocsep}}%
    \etocname\hfill\etocpage\par}
   {\addvspace{2ex}}

\etocsettocstyle{\chapter*{\contentsname}
                \thispagestyle{plain}%
                \leftskip\tocleft\parindent0pt}

\begin{document}
\tableofcontents 
\chapter{test}
\section{test}
\subsection{test}
\chapter{test}
\section{very long titleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee}
\subsection{test}
\chapter{very long titleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee}
\section{test}
\subsection{very long titleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee}
\subsection{test}
\section{test}
\subsection{test}


\end{document}

Thank you very much.

1 Answer 1

1

I used \allowbreak\mbox{}\nobreak\dotfill in place of \hfill.

As per vertical spaces I replaced original code by something different altogether which add spaces for the first section (only) and for the not-first chapters only.

(I also modified a bit the titles to be more realistic)

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

% this is already default so is unneeded
%\etocsetlevel{section}{1}
%\etocsetlevel{subsection}{2}

\etocsettocdepth{subsection}

\newlength{\tocleft}
\setlength{\tocleft}{2.5cm} % adjust to fit the inner margin defined in geometry

\newlength{\tocsep}
\setlength{\tocsep}{2em}

\etocsetstyle{chapter}
   {}
   {\etociffirst{}{\vspace{3ex}}}
   {\etocifnumbered
     {\makebox[0pt][r]{\textbf{\etocnumber}\hspace{\tocsep}}%
      \textbf{\etocname}\allowbreak\mbox{}\nobreak\dotfill\etocpage\par}
    {\textbf{\etocname}\allowbreak\mbox{}\nobreak\dotfill\etocpage}%
    \par
    }
   {}

\etocsetstyle{section}
   {}
   {\etociffirst{\vspace{1ex}}{}}
   {\makebox[0pt][r]{\etocnumber\hspace{\tocsep}}%
    \etocname\allowbreak\mbox{}\nobreak\dotfill\etocpage\par}
   {}

\etocsetstyle{subsection}
   {}
   {}
   {\makebox[0pt][r]{\etocnumber\hspace{\tocsep}}%
    \etocname\allowbreak\mbox{}\nobreak\dotfill\etocpage\par}
   {}

\etocsettocstyle{\chapter*{\contentsname}
                \thispagestyle{plain}%
                \leftskip\tocleft\parindent0pt}

\begin{document}
\tableofcontents 
\chapter{test}
\section{test}
\subsection{test}
\chapter{test}
\section{very long titl eee eeee eeeee eee eeee eeeee eee eeee eeeeeeeeeeeeeeee}
\subsection{test}
\chapter{very long titl eee eeee eeeee eee eeee eeeee eee eeee eeeeeeeeeee}
\section{test}
\subsection{very long titl eee eeee eeeee eee eeee eeeee eee eeee eeeee eee eeee eeeee}
\subsection{test}
\section{test}
\subsection{test}


\end{document}

TOC with dots and linebreaks

You must log in to answer this question.

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