2

I have some section and subsection titles that span more than one line in my TOC. My TOC is center aligned but my titles that wrap aren't center aligning?

\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   

enter image description here

0

2 Answers 2

3

Again remove a bracing pair and switch to using \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}}

Tip: If you use \ShowCommand\l@section the compilation will show the current code for \l@section in the log.

8
  • 1
    @buttonsrtoys for example the chapter code has {\cftchapterfont {#1}}\nobreak \cftchapterfillnum{#2} Note that {...} around \cftchapterfont {#1}, that is a brace pair. Here it forms a group to keep the changes that \cftchapterfont does local. For what you are doing there, that brace pair makes it hard to optain the design you are after.
    – daleif
    Commented Jan 8 at 11:55
  • daleif, I'm indebted. It worked beautifully. You mentioned bracing pair in your other solution. Can you provide a bit more detail about what that is? I'm clearly not strong at LaTeX Commented Jan 8 at 12:49
  • @buttonsrtoys I already did, why did you delete your comment?
    – daleif
    Commented Jan 8 at 13:20
  • 1
    @daleif my comment had a typo and was more than 5 minutes old so I couldn't edit it, so I reposted it. Unfortunately, it appears below your comment, so is out of chronological order. Yeah, I found your typos when I implemented your fix. I'm amazed your can write this code almost perfectly without testing. Commented Jan 9 at 13:18
  • 1
    @buttonsrtoys I did test (I always do) but the sizes made it look correct, so I did not check further.
    – daleif
    Commented Jan 9 at 13:33
2

This is too complicate. However, to achieve the goal, it is better to redefine the \l@chapapp, \l@section and \l@subsection command.

Another day don't want to work :)

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

enter image description here

You must log in to answer this question.

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