1

Is there a way to center the ToC around the space between the chapter titles and the page number as in this pictures (not aligned to the right of the page, but the whole thing relatively centered, or positionable at least, relative to the white space between the chapter and page number)? Many thanks for any help!

\documentclass{book}
\usepackage{titlesec}
\pagestyle{plain}

%no chapter numbers in ToC
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@chapter}{\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}{%
                      \addcontentsline{toc}{chapter}{\protect\numberline{}#1}}{\typeout{Success}}{\typeout{Failed!}}
                      
%page number next to chapter name in ToC
\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}

\chapter{chapter1}

\chapter{chapter2}

\chapter{chapter3}

\end{document}

enter image description here

6
  • It would be useful not only to tell us that you've found the code somewhere but to add a link to the source of the code. Also please don't show code snippets only. Please try always to show a minimal working example. For example, because of not doing so, currently I do not know, which class you are using. This possibly made my answer more complicated than needed. (And IMHO the close votes are also because of the missing information because of the missing MWE – and my missing upvote has the same reason).
    – cabohah
    Sep 15, 2023 at 8:13
  • Sorry about that; I didn't know really what to use as a MWE since it was just the table of contents, and hence including the snippets to at least show something (I'm not too savvy with this; someone else would have known how to provide a useful MWE). I'm just using the standard book class. I will edit the question to include more.
    – jejwood
    Sep 15, 2023 at 11:05
  • See my answer for examples of working examples with table of contents. Using packages like mwe, lipsum, blindtext etc. is very handy for such examples.
    – cabohah
    Sep 15, 2023 at 11:10
  • Are you really using titlesec (not only loading it)? If not, you should not load it. If it could be relevant for the answer (for example, if you use it to configure \part), you should show the relevant code. BTW: If you don't use \part, the part in the \clist_map_inline:nn argument does not make much sense.
    – cabohah
    Sep 15, 2023 at 11:12
  • I am using it. To edit the style of my chapter headings within the text. I didn't think it would effect anything else (indeed, your answer below works well), but I am having a vertical spacing issue with one entry being out of line slightly, so perhaps I was wrong in assuming the other packages having no effect. I will post a new question with absolutely everything included. Thanks for your help!
    – jejwood
    Sep 15, 2023 at 11:49

1 Answer 1

2

It seems you also want all entries to be bold. So I've added \textbf to the entryformat. If you don't want this, just remove it.

IMHO you don't need \clist_map_inline:nn, because KOMA-Script provides \DeclareTOCStyleEntries to configure several entries at once.

IMHO you also don't need to patch \@chapter, because the number can be removed using entrynumberformat.

The variable indent for the centering of the entries is somehow tricky. Unfortunately something like indent=\fill does not work. But you can add \hfill to entryformat to move the entries to the right. To not move the completely to the right, but somehow to the middle, I suggest to use a box if defined width for the page numbers. You can use the width of this box for the manipulation of the centering axis of entries. A lower width will move all entries to right. A larger width will move all entries to the left:

\documentclass{scrbook}

\usepackage{mwe}

\newcommand*{\NoNumber}[1]{}
\DeclareTOCStyleEntries[
  indent=0pt,% don't use an extra indent of the entries
  numwidth=0pt,% no numbers so don't reserve space for the entry numbers
  linefill=\enskip,% space between text and page number
  raggedpagenumber,% don't use right aligned page number column (not really needed)
  entryformat=\hfill\textbf,% extra fill space at the left of the entries +
                            % bold entries
  pagenumberbox={\makebox[.333\linewidth][l]},% widths used for the left aligned page numbers
  entrynumberformat=\NoNumber,% don't print entry numbers (see definition above)
  pagenumberformat=\textbf,% bold page numbers
]
{tocline}{part,chapter,section,subsection,paragraph,subparagraph}

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\end{document}

somehow centered ToC entries

I should state, that this approach would not work, if there would be to long entries to the table of contents. But currently you've also not specified if multi-line entries have to be handled and how.

The suggestion also works with standard classes like book, but in this case not for the part entries, because book does not use \numberline when writing part entries. In this case you first would have to additionally load package tocbasic to make \DeclareTOCStyleEntries available. To remove the number from part entries of the standard classes, you would have to patch \@part:

\documentclass{book}

\usepackage{mwe}
\usepackage{tocbasic}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@part}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
  \addcontentsline{toc}{part}{#1}%
}{}{\PatchFailure}
\makeatother

\newcommand*{\NoNumber}[1]{}
\DeclareTOCStyleEntries[
  indent=0pt,% don't use an extra indent of the entries
  numwidth=0pt,% no numbers so don't reserve space for the entry numbers
  linefill=\enskip,% space between text and page number
  pagenumberbox={\makebox[.333\linewidth][l]},% widths used for the left aligned page numbers
  entryformat=\hfill\textbf,% extra fill space at the left of the entries +
                            % bold entries
  entrynumberformat=\NoNumber,% don't print entry numbers (see definition above)
  pagenumberformat=\textbf,% bold page numbers
]
{tocline}{part,chapter,section,subsection,paragraph,subparagraph}

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\end{document}

same for the standard classes

BTW: Another suggestion could be to define your own ToC entry style:

\documentclass{book}

\usepackage{mwe}
\usepackage{tocbasic}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@part}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
  \addcontentsline{toc}{part}{#1}%
}{}{\PatchFailure}

\newcommand*{\NoNumber}[1]{}
\DeclareTOCEntryStyle{centered}[{%
  % Following definition by cases is only needed for automatic setup of the
  % level option with the standard classes. With KOMA-Script classes, the
  % levels are already initialized and the cases are not needed. These case definition has been copied from tocbasic.sty.
  \@ifundefined{#1tocdepth}{%
    \Ifstr{#1}{part}{\@namedef{#1tocdepth}{-1}}{%
      \Ifstr{#1}{chapter}{\@namedef{#1tocdepth}{0}}{%
        \Ifstr{#1}{section}{\@namedef{#1tocdepth}{1}}{%
          \Ifstr{#1}{subsection}{\@namedef{#1tocdepth}{2}}{%
            \Ifstr{#1}{subsubsection}{\@namedef{#1tocdepth}{3}}{%
              \Ifstr{#1}{paragraph}{\@namedef{#1tocdepth}{4}}{%
                \Ifstr{#1}{subparagraph}{\@namedef{#1tocdepth}{5}}{%
                  \Ifstr{#1}{figure}{\@namedef{#1tocdepth}{1}}{%
                    \Ifstr{#1}{table}{\@namedef{#1tocdepth}{1}}{%
                      \Ifstr{#1}{lstlisting}{\@namedef{#1tocdepth}{1}}{%
                        \expandafter\let\csname #1tocdepth\expandafter\endcsname
                        \csname #1numdepth\endcsname
                      }%
                    }%
                  }%
                }%
              }%
            }%
          }%
        }%
      }%
    }%
  }{}%
}]{%
  \renewcommand*\numberline[1]{}% no numbers
  % Use a \parbox for the text (and number). The width of the \parbox specifies the horizontal position of the page number
  \noindent\bfseries\parbox[b]{.667\linewidth}{\raggedleft\strut ##1\strut}\enskip ##2\par
}
\makeatother

\DeclareTOCStyleEntries
{centered}{part,chapter,section,subsection,paragraph,subparagraph}

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\section{A section with a much to long entry to fit into one line}
\blindtext
\end{document}

enter image description here

Once again, this would be somehow easier if you are using a KOMA-Script class:

\documentclass{scrbook}

\usepackage{mwe}

\newcommand*{\NoNumber}[1]{}
\DeclareTOCEntryStyle{centered}{%
  \renewcommand*\numberline[1]{}% no numbers
  \noindent\bfseries\parbox[b]{.667\linewidth}{\raggedleft\strut ##1\strut}\enskip ##2\par
}

\DeclareTOCStyleEntries
{centered}{part,chapter,section,subsection,paragraph,subparagraph}

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\section{A section with a much to long entry to fit into one line}
\end{document}

enter image description here

See \DeclareTOCEntryStyle in the KOMA-Script manual for more information about this, i.e., if you need additional per entry level formatting.

You must log in to answer this question.

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