1

I would like to futher customize my TOC, but it seems that I cannot include \centering or \par - e.g., for something like \@chapapp\ \HeadingNumber\par. Otherwise, color/size/font changes work fine.

\documentclass{mwbk}
\usepackage[utf8]{inputenc}
\usepackage{mathptmx}
\usepackage{graphicx}

\usepackage[dvipsnames]{xcolor}


\usepackage{blindtext}

\makeatletter



\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries \LARGE % added for large font for chapters
      \advance\leftskip\@tempdima
      \hskip -\leftskip %\@chapapp~ \par % added \@chapapp~ \par 
      #1\nobreak \raggedright % originally \hfil
%added for leaders (dots) in chapter in toc
%\xleaders\hbox{$\m@th
 %      \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
 %       mu$}\hfill%
%%%
\nobreak\hb@xt@\@pnumwidth{\hss \itshape \fbox{#2}}\par\vspace{12pt} % ADDED VSPACE
      \penalty\@highpenalty
    \endgroup
  \fi}  




%for mwbk
\def\mw@seccntformat#1{#1\enspace} % para tirar o ponto após número do capítulo

\renewcommand*\chapter@toc{%
  \ifHeadingNumbered\typeout{\@chapapp\space\thechapter.}\fi
  \addcontentsline{toc}{chapter}{%
        \ifHeadingNumbered
\mw@seccntformat{\color{ForestGreen}\@chapapp\ \HeadingNumber}%
        \fi
        \color{yellow}\HeadingTOCText}%
  \addtocontents{lof}{\protect\addvspace{10\p@}}%
  \addtocontents{lot}{\protect\addvspace{10\p@}}%
  }




\makeatother





\begin{document}


\tableofcontents

\chapter{Some chapter}

\blinddocument

\blinddocument

\end{document}
  • I would suggest using a \parbox, but for more than one line you might have trouble aligning the top to the left and the bottom to the right (although it can be done). – John Kormylo Jun 14 '17 at 01:03

1 Answers1

2

It is not exactly clear what is desired, from the question, so I will make an assumption that it is the yellow text the OP wants centered in the TOC, but not in the document. Also, how to handle multi-line entries of the chapter name in such a fashion? For the general approach, I surrounded the heading TOC text with \hfills, as in

  \addcontentsline{toc}{chapter}{%
        \ifHeadingNumbered
\mw@seccntformat{\color{ForestGreen}\@chapapp\ \HeadingNumber}%
        \fi
        \color{yellow}\hfill\HeadingTOCText\hfill}%

However, that does not handle the multi-line chapter names. For those, I require manual breaks using \manbreak, as in \chapter{This is a really extra long \manbreak chapter name}. This macro is defined one way for the TOC construction, and \relaxed for the rest of the document by way of

\def\manbreak{\hfill\hfill\\\mbox{}\hfill\hfill}
\tableofcontents
\let\manbreak\relax

Here is the MWE.

\documentclass{mwbk}
\usepackage[utf8]{inputenc}
\usepackage{mathptmx}
\usepackage{graphicx}

\usepackage[dvipsnames]{xcolor}


\usepackage{blindtext}

\makeatletter



\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries \LARGE % added for large font for chapters
      \advance\leftskip\@tempdima
      \hskip -\leftskip %\@chapapp~ \par % added \@chapapp~ \par 
      #1\nobreak \raggedright % originally \hfil
%added for leaders (dots) in chapter in toc
%\xleaders\hbox{$\m@th
 %      \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
 %       mu$}\hfill%
%%%
\nobreak\hb@xt@\@pnumwidth{\hss \itshape \fbox{#2}}\par\vspace{12pt} % ADDED VSPACE
      \penalty\@highpenalty
    \endgroup
  \fi}  




%for mwbk
\def\mw@seccntformat#1{#1\enspace} % para tirar o ponto após número do capítulo

\renewcommand*\chapter@toc{%
  \ifHeadingNumbered\typeout{\@chapapp\space\thechapter.}\fi
  \addcontentsline{toc}{chapter}{%
        \ifHeadingNumbered
\mw@seccntformat{\color{ForestGreen}\@chapapp\ \HeadingNumber}%
        \fi
        \color{yellow}\hfill\HeadingTOCText\hfill}%
  \addtocontents{lof}{\protect\addvspace{10\p@}}%
  \addtocontents{lot}{\protect\addvspace{10\p@}}%
  }




\makeatother





\begin{document}

\def\manbreak{\hfill\hfill\\\mbox{}\hfill\hfill}
\tableofcontents
\let\manbreak\relax

\chapter{Some chapter}

\blinddocument

\blinddocument

\chapter{This is a really extra long \manbreak chapter name}

\end{document}

enter image description here

enter image description here


While I don't know how to allow for \pars in the definition of \l@chapter or \mw@seccntformat, a similar approach with \manbreak can be used, here redefining \def\mw@seccntformat#1{#1\Manbreak}, where \Manbreak likewise has one definition in the TOC, and is \relaxed afterwards.

\documentclass{mwbk}
\usepackage[utf8]{inputenc}
\usepackage{mathptmx}
\usepackage{graphicx}

\usepackage[dvipsnames]{xcolor}


\usepackage{blindtext}

\makeatletter



\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries \LARGE % added for large font for chapters
      \advance\leftskip\@tempdima
      \hskip -\leftskip %\@chapapp~ \par % added \@chapapp~ \par 
      #1\nobreak \raggedright % originally \hfil
%added for leaders (dots) in chapter in toc
%\xleaders\hbox{$\m@th
 %      \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
 %       mu$}\hfill%
%%%
\nobreak\hb@xt@\@pnumwidth{\hss \itshape \fbox{#2}}\par\vspace{12pt} % ADDED VSPACE
      \penalty\@highpenalty
    \endgroup
  \fi}  


%for mwbk
\def\mw@seccntformat#1{#1\Manbreak} % para tirar o ponto após número do capítulo

\renewcommand*\chapter@toc{%
  \ifHeadingNumbered\typeout{\@chapapp\space\thechapter.}\fi
  \addcontentsline{toc}{chapter}{%
        \ifHeadingNumbered
\mw@seccntformat{\color{ForestGreen}\@chapapp\ \HeadingNumber}%
        \fi
        \color{yellow}\hfill\HeadingTOCText\hfill}%
  \addtocontents{lof}{\protect\addvspace{10\p@}}%
  \addtocontents{lot}{\protect\addvspace{10\p@}}%
  }

\makeatother

\begin{document}

\def\Manbreak{\\\mbox{}}
\def\manbreak{\hfill\hfill\\\mbox{}\hfill}
\tableofcontents
\let\manbreak\relax
\let\Manbreak\relax

\chapter{Some chapter}

\blinddocument

\blinddocument

\chapter{This is a really extra long \manbreak chapter name}

\end{document}

enter image description here

  • Steven, that's great as far as it goes -- \hfill works, \centering doesn't, but actually I'd like to know how to have a paragraph brake in \mw@seccntformat or l@chapter, so that I can have "Chapter 4.\par etc... –  Jun 14 '17 at 10:27
  • @Joseph It is still not clear to me which element of the TOC you want to possess a paragraph break...the chapter title?? – Steven B. Segletes Jun 14 '17 at 10:35
  • @Joseph Do I understand that you want the yellow text on a line below the green text, for example? – Steven B. Segletes Jun 14 '17 at 10:39
  • @Joseph Please see my edit. – Steven B. Segletes Jun 14 '17 at 10:51
  • Steven, yes, that's exactly what I was thinking about -- I'm still wondering why \\\mbox{} works, but a simple \par doesn't. I'm still working to center the green (chapter) also, but that's fine. –  Jun 14 '17 at 12:20
  • @Joseph See https://tex.stackexchange.com/questions/82664/when-to-use-par-and-when-or-blank-lines. \par requires \long definitions, which will break a lot of things at that level, if shoehorned in. – Steven B. Segletes Jun 14 '17 at 12:22
  • @Joseph As to \\\mbox{}, an \hfill needs something to push against, and that is what the \mbox{} provides – Steven B. Segletes Jun 14 '17 at 12:24
  • 1
    Indeed, when I changed to: \mw@seccntformat{\hfill\color{ForestGreen}\@chapapp\ \HeadingNumber\hfill\mbox{}}%, it got centered. I never imagined an \mbox{} would make such a difference. Many thanks. –  Jun 14 '17 at 12:26