latex:如何在目录中将章节标题居中对齐?

latex:如何在目录中将章节标题居中对齐?

我正在尝试将目录中的章节标题居中对齐。我尝试在序言中使用 \centering,但没有成功:

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

这是一个完整的例子:

\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}

答案1

如果您仍希望章节号左对齐并且仅标题居中,请更新\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

\l@chapter相反,正如 OP 尝试的那样,修改也会对章节编号产生影响:

\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

相关内容