4

I would like to put page numbers in the standard table of contents right after the chapter/section/subsection... names and not right-justified. I tried tocstyle package but it changes other parameters of the table of contents, e.g. indentation, etc.

UPD: I use our institute's thesis class, which is based on report class and these are the definitions they made which are related to tables of contents:

%%% Table of contents

\setcounter{tocdepth}{2}
\renewcommand\@pnumwidth{1.85em}
\def\numberline#1{\ifdim\@tempdima>\z@\hb@xt@\@tempdima{\hfil#1.\ }%
    \else{#1.\ }\fi}
\renewcommand*\l@part[2]{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{1.0em \@plus\p@}%
    \setlength\@tempdima{0em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      {\leavevmode
       \large \bfseries #1\hfil \hb@xt@\@pnumwidth{\hss #2}}\par
       \nobreak
     \global\@nobreaktrue
     \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}
\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 0.8em \@plus\p@
    \setlength\@tempdima{0em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}
\renewcommand*\l@section{\@dottedtocline{1}{0.6em}{2.4em}}
\renewcommand*\l@subsection{\@dottedtocline{2}{2.8em}{3.0em}}
\renewcommand*\l@subsubsection{\@dottedtocline{3}{4.0em}{1.8em}}
\renewcommand*\l@paragraph{\@dottedtocline{4}{5.4em}{1.9em}}
\renewcommand*\l@subparagraph{\@dottedtocline{5}{7.1em}{2.8em}}

So I would like to change/update these definitions in order to keep all other parameters the same.

4
  • 1
    The layout of the toc depends not insignificantly on the document class and possibly other packages. It would greatly help me understand your question if you could provide an MWE that shows the toc you have so far.
    – moewe
    Commented Jun 19, 2018 at 18:24
  • It's our institute's template. But ToC looks rather standard do perhaps they didn't change it much. I'll update with MWE later. Commented Jun 19, 2018 at 18:51
  • 1
    Still there are great differences between what one should do to modify the TOC between say KOMA classes and the standard classes. So we need too know what class your institute template is based on.
    – moewe
    Commented Jun 19, 2018 at 18:56
  • @moewe added more details Commented Jun 20, 2018 at 8:54

2 Answers 2

8

The standard ToC style is easily adjustable using tocloft. It breaks up the regular sectional unit X into \cftX<type> components. Here is a pseudo-code representation for how numbered sectional units are typeset:

{\cftXfont {\cftXpresnum SNUM\cftXaftersnum\hfil} \cftXaftersnumb TITLE}%
  {\cftXleader}{\cftXpagefont PAGE}\cftXafterpnum\par

where X is

  • part for \parts;
  • chap for \chapters (if using a class that has chapters);
  • sec for \sections;
  • subsec for \subsections;
  • subsubsec for \subsubsections;
  • para for \paragraphs;
  • subpara for \subparagraphs;
  • ...

Below is a minimal example based on article that adjusts the ToC to suit your needs for \sections and \subsections. The style is fairly self-explanatory.

enter image description here

\documentclass{article}

\usepackage{lipsum}
\usepackage{tocloft}

% Update \sections in ToC
\renewcommand{\cftsecleader}{~}% Content between section title and page number
\renewcommand{\cftsecafterpnum}{\hfill\mbox{}}% Content after section page number
% Update \subsections in ToC
\renewcommand{\cftsubsecleader}{~}% Content between subsection title and page number
\renewcommand{\cftsubsecafterpnum}{\hfill\mbox{}}% Content after subsection page number


\begin{document}

\tableofcontents

\sloppy% Just for this example

\section{First section}\lipsum[1-10]

\subsection{First subsection}\lipsum[6-15]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Second subsection}\lipsum[16-25]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Third subsection}\lipsum[26-35]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Last subsection}\lipsum[36-45]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\section{Second section}\lipsum[11-20]

\subsection{First subsection}\lipsum[6-15]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Second subsection}\lipsum[16-25]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Third subsection}\lipsum[26-35]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Last subsection}\lipsum[36-45]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\section{Third section}\lipsum[21-30]

\subsection{First subsection}\lipsum[6-15]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Second subsection}\lipsum[16-25]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Third subsection}\lipsum[26-35]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Last subsection}\lipsum[36-45]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\section{Final section}\lipsum[31-40]

\subsection{First subsection}\lipsum[6-15]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Second subsection}\lipsum[16-25]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Third subsection}\lipsum[26-35]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\subsection{Last subsection}\lipsum[36-45]
\subsubsection{First subsubsection}\lipsum[11-20]
\subsubsection{Second subsubection}\lipsum[21-30]
\subsubsection{Third subsubsection}\lipsum[31-40]
\subsubsection{Last subsubsection}\lipsum[41-50]

\end{document}
6

Here is a suggestion using package tocbasic.

Standard class:

\documentclass{book}
\usepackage{blindtext}

\usepackage{tocbasic}
\DeclareTOCStyleEntry[numwidth=2em,indent=0pt]{tocline}{part}
\usepackage{expl3}
\ExplSyntaxOn
  \clist_map_inline:nn
  {part,chapter,section,subsection,subsubsection,paragraph,subparagraph}
  {\DeclareTOCStyleEntry[
    linefill=\enskip,
    raggedpagenumber,
    pagenumberbox=\mbox
  ]{tocline}{#1}}
\ExplSyntaxOff

\begin{document}
\tableofcontents
\part{Part One}
\Blinddocument
\Blinddocument
\setcounter{page}{4000}% only for the example
\part{Part Two}
\Blinddocument
\Blinddocument
\end{document}

Result:

enter image description here

KOMA-Script class:

\documentclass{scrbook}% loads package tocbasic
\usepackage{blindtext}% only for dummy text

\RedeclareSectionCommands[%
  toclinefill=\enskip,%
  tocraggedpagenumber,%
  tocpagenumberbox=\mbox
]{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}

\begin{document}
\tableofcontents
\part{Part One}
\Blinddocument
\Blinddocument
\setcounter{page}{4000}% only for the example
\part{Part Two}
\Blinddocument
\Blinddocument
\end{document}

Result:

enter image description here

3
  • Is there a reason for choosing toclinefill={~} over toclinefill={}?
    – moewe
    Commented Jun 19, 2018 at 20:03
  • 1
    @moewe There should be a minimum space between text and page number even if there are page numbers with 3 or more digits. But I have changed it now to toclinefill=\enskip and added pagenumberbox=\mbox. So the space between the text and the page number is equal for the entries.
    – esdd
    Commented Jun 19, 2018 at 20:27
  • I see. I didn't think of three-digit page numbers. Thank you for the explanation.
    – moewe
    Commented Jun 19, 2018 at 20:29

You must log in to answer this question.

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