1

In the sequence of my question (here), this is the third part. I want to centralize the title of "Content" and add a short text on top of it like the following figure, but I don't know how to do it. Thanks. Here is the minimal code:

\documentclass{book}

\usepackage[a5paper]{geometry} \usepackage{lipsum} \linespread{1.5}

\begin{document}

\tableofcontents \addtocontents{toc}{ \textbf{Title} \hfill \textbf{Page}\par}

\chapter{Spring}\lipsum[1-30] \section{Sp1}\lipsum[1-30] \section{Sp2}\lipsum[1-30] \section{Sp3}\lipsum[1-30] \section{Sp4}\lipsum[1-30] \section{Sp5}\lipsum[1-30] \section{Sp6}\lipsum[1-30] \section{Sp7}\lipsum[1-30] \section{Sp8}\lipsum[1-30] \section{Sp9}\lipsum[1-30]

\chapter{Summer} \section{Su1}\lipsum[1-30] \section{Su2}\lipsum[1-30] \section{Su3}\lipsum[1-30] \section{Su4}\lipsum[1-30] \section{Su5}\lipsum[1-30] \section{Su6}\lipsum[1-30] \section{Su7}\lipsum[1-30] \section{Su8}\lipsum[1-30] \section{Su9}\lipsum[1-30]

\chapter{Fall} \section{F1}\lipsum[1-30] \section{F2}\lipsum[1-30] \section{F3}\lipsum[1-30] \section{F4}\lipsum[1-30] \section{F5}\lipsum[1-30] \section{F6}\lipsum[1-30] \section{F7}\lipsum[1-30] \section{F8}\lipsum[1-30] \section{F9}\lipsum[1-30]

\chapter{Winter} \section{W1}\lipsum[1-30] \section{W2}\lipsum[1-30] \section{W3}\lipsum[1-30] \section{W4}\lipsum[1-30] \section{W5}\lipsum[1-30] \section{W6}\lipsum[1-30] \section{W7}\lipsum[1-30] \section{W8}\lipsum[1-30] \section{W9}\lipsum[1-30]

\end{document}

enter image description here

AYBRXQD
  • 737
  • 2
    Please make all your question self-sustained. They should be understandable without having to read your whole series of questions. In particular, add a MWE which is adapted to the current problem, to all your questions, not only to the first one. – samcarter_is_at_topanswers.xyz Mar 20 '23 at 16:01
  • I have modified the question, thanks. – AYBRXQD Mar 20 '23 at 16:03
  • 1
    Why does a MWE about adding a header require sooooooooooooooooooooo many sections? – samcarter_is_at_topanswers.xyz Mar 20 '23 at 16:11
  • 1
    Please also edit your questions titles to reflect what they are actually about. In their current form, future users with the same problem are unlikely to find your question. – samcarter_is_at_topanswers.xyz Mar 20 '23 at 16:13
  • 1
    The table of contents is a chapter so: https://tex.stackexchange.com/questions/13496/center-aligning-chapters for the centering. But preambles on top or below a chapter headings would be provided, e.g., by the KOMA-Script classes, that also provide \raggedchapter, that could be redefined to be \centering. – cabohah Mar 20 '23 at 16:13
  • Dear @cabohah, thanks for you help but I only need to change the content title not all chapters. – AYBRXQD Mar 20 '23 at 16:22
  • You can use \titleformat more than once and also the KOMA-Script suggestion wouldn't be possible to change only the heading of the table of contents. – cabohah Mar 20 '23 at 16:27
  • Dear @cabohah, if you provide the right MWE, it would be kind of you. – AYBRXQD Mar 20 '23 at 16:29

1 Answers1

2

As already told in a comment, KOMA-Script classes provide \raggedchapter to change the alignment of the chapter headings and \setchapterpreamble to place a preamble text on top of below a chapter headings. So you can change the alignment before (and after) the table of contents and add a preamble. As an extra feature it provides \BeforeTOCHead. This can be used to do all the relevant changes from within the document preamble, without changing the headings of other chapters:

\documentclass[emulatestandardclasses,a5paper]{scrbook}

\usepackage{xcolor} \usepackage{blindtext} \BeforeTOCHead[toc]{% \let\raggedchapter\centering \setchapterpreamble[o]{\centering\color{red} a short text} }

\begin{document}

\tableofcontents

\blinddocument

\end{document}

Table of contents with centered heading an preamble text above the heading

With the standard classes also the \BeforeTOC feature of KOMA-Script could be used. To do so, you can load package tocbasic. However the preamble feature would still not be available, so you need a ugly hack to add the text on top of the heading, e.g., a local redefinition of \contentsname. But in this case you would also need to change the marks for the table of contents.

\documentclass[a5paper]{book}

\usepackage{xcolor} \usepackage{tocbasic}

\usepackage{blindtext}

\BeforeTOCHead[toc]{% \let\raggedchapter\centering \markboth{\MakeUppercase{\contentsname}}{\MakeUppercase{\contentsname}}% \renewcommand*{\contentsname}{% Ugly hack to add preamble and to center \centering \textcolor{red}{\normalsize a short text}\par Contents }% } \AfterTOCHead[toc]{% \markboth{CONTENTS}{CONTENTS}% }

\begin{document}

\listoftoc[\contentsname]{toc}

\blinddocument

\end{document}

cabohah
  • 11,455
  • Dear @cabohah, thanks for your answer, but what shall I do for the book document class? – AYBRXQD Mar 20 '23 at 16:43
  • 2
    @AYBRXQD Why using a class, that does not provide what you want? Using a class, which does provide most of the wanted features, is more comfortable and smarter. – cabohah Mar 20 '23 at 16:51