如何使目录中的长标题居中对齐?

如何使目录中的长标题居中对齐?

我的目录中有一些章节和子章节标题跨越多行。我的目录居中对齐,但换行的标题却没有居中对齐?

\documentclass{memoir}
\let\ordinal\relax

% TOC title
\makeatletter
\renewcommand{\printtoctitle}[1]{\centering\HUGE\textit{Contents}}

% TOC
\usepackage{fmtcount}
\setcounter{tocdepth}{2}

\makeatletter
\patchcmd{\l@chapapp}{{\cftchapterfont {#1}}\nobreak}{\cftchapterfont {#1}\nobreak}{\typeout{ok}}{\typeout{bad}}

% Chapter
\setlength\cftchapternumwidth{0pt}
\renewcommand{\thechapter}{\Numberstring{chapter}}
\renewcommand{\cftchapterleader}{}
\renewcommand{\cftchapterfillnum}[1]{\hspace*{10pt}\Huge#1\hfil\strut\par}
\renewcommand*{\cftchapterfont}{}
\renewcommand\chapternumberline[1]{\hfil\LARGE\emph{#1}\hfil\strut\Huge\par\nopagebreak\hfil}

% Section
\renewcommand{\cftsectionleader}{}
\renewcommand{\thesection}{}
\renewcommand{\cftsectionfillnum}[1]{\itshape\hspace*{10pt}\LARGE#1\cftparfillskip\par}
\renewcommand*{\cftsectionfont}{\itshape\LARGE\hfil}

% Subsection
\renewcommand{\cftsubsectionleader}{}
\renewcommand{\cftsubsectionfillnum}[1]{\hspace*{10pt}\LARGE#1\cftparfillskip\par}
\renewcommand*{\cftsubsectionfont}{\itshape\LARGE\hfil}

% Spacing
\setlength{\cftbeforechapterskip}{0pt}

% Document
\begin{document}
\tableofcontents*
\chapter{Chapter One}
\chapter{Chapter Two}
\chapter{Chapter Three}
\chapter{Chapter Four}
\section{Section One}
\subsection{Subsection One}
\section{Section Two}
\subsection{Subsection Two}
\subsection{Subsection Three, Which Has a\\Really Long Title}
\chapter{Chapter Five}
\end{document}

\makeatother   

在此处输入图片描述

答案1

再次移除支撑对并切换到使用\centering

% Section
\setlength\cftsectionnumwidth{0pt}
\renewcommand{\cftsectionleader}{}
\renewcommand{\thesection}{}
\renewcommand{\cftsectionfillnum}[1]{\itshape\hspace*{10pt}\LARGE#1\par}
\renewcommand*{\cftsectionfont}{\centering\itshape\LARGE}

\patchcmd{\l@section}{{\@nameuse{cftsectionfont}{#1}}\nobreak}{\@nameuse{cftsectionfont}{#1}\nobreak}{\typeout{ok}}{\typeout{bad}}


% Subsection
\setlength\cftsubsectionnumwidth{0pt}
\renewcommand{\cftsubsectionleader}{}
\renewcommand{\cftsubsectionfillnum}[1]{\hspace*{10pt}\LARGE#1\par}
\renewcommand*{\cftsubsectionfont}{\centering\itshape\LARGE}

\patchcmd{\l@subsection}{{\@nameuse{cftsubsectionfont}{#1}}\nobreak}{\@nameuse{cftsubsectionfont}{#1}\nobreak}{\typeout{ok}}{\typeout{bad}}

提示:如果您使用\ShowCommand\l@section编译,将\l@section在日志中显示当前代码。

答案2

这太复杂了。不过,为了达到目的,最好重新定义\l@chapapp\l@section\l@subsection命令。

又是不想工作的一天:)

\documentclass{memoir}
\let\ordinal\relax

% TOC title

\renewcommand{\printtoctitle}[1]{\centering\HUGE\textit{Contents}}

% TOC
\usepackage{fmtcount}
\setcounter{tocdepth}{2}
\setcounter{secnumdepth}{0}

\usepackage{tikz}

% Chapter
\renewcommand{\thechapter}{\Numberstring{chapter}}
\renewcommand{\cftchapternumwidth}{0pt}
\renewcommand{\cftbeforechapterskip}{20pt}
\renewcommand{\cftchapterfillnum}[1]{}
\renewcommand{\cftchapterfont}{\itshape\Huge}
\renewcommand\chapternumberline[1]{{\Large#1\\[5pt]}}
\makeatletter
\def\l@chapapp#1#2#3{%
\ifnum \c@tocdepth >\m@ne \cftchapterbreak \vskip
\cftbeforechapterskip \centering\begin{minipage}[b]{0.5\textwidth}
\centering\parindent \cftchapterindent
\relax \@afterindenttrue \interlinepenalty \@M \leavevmode \let \@cftbsnum
\cftchapterpresnum \let \@cftasnum \cftchapteraftersnum \let \@cftasnumb
\cftchapteraftersnumb \def \@chapapp@head {#3}\settowidth {\@tempdima
}{\cftchapterfont \@chapapp@head }\addtolength {\@tempdima }{\cftchapternumwidth
}\advance \memRTLleftskip \@tempdima \null \nobreak {\cftchapterfont {#1}}\nobreak \cftchapterfillnum {#2}\end{minipage}\tikz[remember picture, overlay]{\node [anchor=text,font=\cftchapterfont] at (0.5,0) {#2};}\par\fi%
}
\makeatother

% Section
\renewcommand{\cftsectionindent}{0pt}
\renewcommand{\cftsectionnumwidth}{0pt}
\renewcommand{\cftbeforesectionskip}{15pt}
\renewcommand{\cftsectionfillnum}[1]{}
\renewcommand{\cftsectionfont}{\itshape\LARGE}

\makeatletter
\def\l@section#1#2{%
\ifnum \@nameuse {c@tocdepth} > 0\relax \vskip \@nameuse
{cftbeforesectionskip}\centering\begin{minipage}[b]{0.5\textwidth}
\centering\newcommand *\cftwhatismyname {section}\parindent \@nameuse {cftsectionindent}\relax \@afterindenttrue
\interlinepenalty \@M \leavevmode \settowidth {\@tempdima }{\@nameuse
{cftsectionfont}{\@nameuse {cftsectionname}}}\addtolength {\@tempdima
}{\@nameuse {cftsectionnumwidth}}\expandafter \let \expandafter \@cftbsnum
\csname cftsectionpresnum\endcsname \expandafter \let \expandafter
\@cftasnum \csname cftsectionaftersnum\endcsname \expandafter \let
\expandafter \@cftasnumb \csname cftsectionaftersnumb\endcsname \expandafter
\let \expandafter \@cftn@me \csname cftsectionname\endcsname \advance
\memRTLleftskip \@tempdima \null \nobreak {\@nameuse {cftsectionfont}{#1}}\nobreak \@nameuse {cftsectionfillnum}{#2}\end{minipage}\tikz[remember picture, overlay]{\node [anchor=text,font=\cftsectionfont] at (0.5,0) {#2};}\par\fi%
}
\makeatother

% Subsection
\renewcommand{\cftsubsectionindent}{0pt}
\renewcommand{\cftsubsectionnumwidth}{0pt}
\renewcommand{\cftbeforesubsectionskip}{10pt}
\renewcommand{\cftsubsectionfillnum}[1]{}
\renewcommand{\cftsubsectionfont}{\itshape\Large}

\makeatletter
\def\l@subsection#1#2{%
\ifnum \@nameuse {c@tocdepth} > 1\relax \vskip \@nameuse
{cftbeforesubsectionskip}\centering\begin{minipage}[b]{0.5\textwidth}
\centering\newcommand *\cftwhatismyname
{subsection}  \parindent
\@nameuse {cftsubsectionindent}\relax\@afterindenttrue \interlinepenalty
\@M \leavevmode \settowidth {\@tempdima }{\@nameuse {cftsubsectionfont}{\@nameuse
{cftsubsectionname}}}\addtolength {\@tempdima }{\@nameuse {cftsubsectionnumwidth}}\expandafter
\let \expandafter \@cftbsnum \csname cftsubsectionpresnum\endcsname
\expandafter \let \expandafter \@cftasnum \csname cftsubsectionaftersnum\endcsname
\expandafter \let \expandafter \@cftasnumb \csname cftsubsectionaftersnumb\endcsname
\expandafter \let \expandafter \@cftn@me \csname cftsubsectionname\endcsname
\advance \memRTLleftskip \@tempdima \null \nobreak 
{\@nameuse {cftsubsectionfont}{#1}}\nobreak \@nameuse {cftsubsectionfillnum}{#2}
\end{minipage}\tikz[remember picture, overlay]{\node [anchor=text,font=\cftsubsectionfont] at (0.5,0) {#2};}\par\fi%
}
\makeatother

% Document
\begin{document}
\tableofcontents*
\chapter{Chapter One Which Has a Really Long Long Long Title}
\chapter{Chapter Two}
\chapter{Chapter Three}
\chapter{Chapter Four}
\section{Section One}
\subsection{Subsection One}
\section{Section Two}
\subsection{Subsection Two}
\subsection{Subsection Three, Which Has a Really Long Long Long Long Long Long Long Long Long Title}
\chapter{Chapter Five}
\end{document}

在此处输入图片描述

相关内容