1

I am trying to center-align chapter titles in the table of contents. I tried using \centering in the preamble but it did not work:

\makeatletter
\let\stdl@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{%
    \stdl@chapter{\centering\textcolor{blue}{#1}}{\centering{#2}}}

Here is a complete example:

\documentclass[11pt]{book}
\usepackage[svgnames]{xcolor}
\usepackage{tocloft}
\makeatletter
\let\stdl@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{%
    \stdl@chapter{\centering{#1}}{\centering{#2}}}
\let\stdl@section\l@section
\renewcommand*{\l@section}[2]{%
  \stdl@section{{#1}}{{#2}}}
\let\stdl@subsection\l@subsection
\renewcommand*{\l@subsection}[2]{%
  \stdl@subsection{{#1}}{{#2}}}
\makeatother
\setcounter{tocdepth}{2}
\begin{document}
\tableofcontents
\chapter{Chapter one}
\section{Section One}
\subsection{Subsection One}
\subsection{Subsection Two}
\section{Section Two}
\subsection{Subsection One}
\subsection{Subsection Two}
\end{document}
2

If you still want the chapter number left aligned and only the title centered, then renew \cftchapaftersnumb.

\documentclass[11pt]{book}
\usepackage[svgnames]{xcolor}
\usepackage{tocloft}
\renewcommand\cftchapaftersnumb{\hfill}
\setcounter{tocdepth}{2}
\begin{document}
\tableofcontents
\chapter{Chapter one}
\section{Section One}
\subsection{Subsection One}
\subsection{Subsection Two}
\section{Section Two}
\subsection{Subsection One}
\subsection{Subsection Two}
\end{document}

enter image description here

Modifying \l@chapter, instead, as the OP was attempting, will have an effect on the chapter number, as well:

\documentclass[11pt]{book}
\usepackage[svgnames]{xcolor}
\usepackage{tocloft}
%\renewcommand\cftchapaftersnumb{\hfill}
\makeatletter
\let\stdl@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{\stdl@chapter{\hfill#1}{#2}}
\makeatother
\setcounter{tocdepth}{2}
\begin{document}
\tableofcontents
\chapter{Chapter one}
\section{Section One}
\subsection{Subsection One}
\subsection{Subsection Two}
\section{Section Two}
\subsection{Subsection One}
\subsection{Subsection Two}
\end{document}

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.