2

I am trying format \tableofcontents I want to put the chapter's titles Topological spacesat center and Chapter 1, ...like this picture , but I can't get the result.

My code

\documentclass[12pt,a4paper,twoside,openany]{book}
\usepackage[unicode,bookmarksnumbered]{hyperref}
\usepackage{lipsum}

\renewcommand*{\thesection}{\S\arabic{section}}
\renewcommand*{\thesubsection}{\thesection.\arabic{subsection}}

\usepackage{sectsty} 

\allsectionsfont{\centering}

\usepackage{fancyhdr}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\itshape\nouppercase{\rightmark}}
\fancyhead[RE]{\itshape\nouppercase{\leftmark}}


\usepackage{tocloft,calc}
\usepackage{etoolbox}
\renewcommand{\cftchappresnum}{\chaptername\space}
\setlength{\cftchapnumwidth}{\widthof{\textbf{Appendix~999~}}}
\makeatletter
\g@addto@macro\appendix{%
  \addtocontents{toc}{%
    \protect\renewcommand{\protect\cftchappresnum}{\appendixname\space}%
  }%
}
\makeatother
\AtBeginDocument{\renewcommand{\chaptername}{Chapter}}
\begin{document}
\tableofcontents
\chapter{Topological spaces}
\section{Hello again}
    \subsection{Something}
\section{Something}
\lipsum[1-3]
\subsection{Something}
\section{A section}
\lipsum[1-3]

\appendix
\chapter{Hello again}
\section{Hello again}
\lipsum[1-10]
\chapter{Hello again}
    \end{document}

enter image description here

enter image description here

  • Off-topic: You're loading hyperref ways too early – user31729 Jan 17 at 17:29
  • No. I can not accept your answer. Because, the page of tableifcontents does not like my picture. – minhthien_2016 Jan 19 at 10:50
  • You asked for the centering of the title ... that's what my answer did. – user31729 Jan 19 at 10:55
  • I am sorry about my English. Please help me with my picture. – minhthien_2016 Jan 19 at 10:57
2

Use the tocloft package to adjust the style and placing of division titles in the ToC.

\documentclass{book}
\usepackage{tocloft}

\renewcommand{\cftchappresnum}{CHAPTER } % put CHAPTER before the number
\renewcommand{\cftchapfont}{\centering} % center CHAPTER and number
\makeatletter
% start new line after the number, shift title so it is centered
\renewcommand{\cftchapaftersnumb}{%
    \\ \hspace{\@pnumwidth}\hspace{\@tocrmarg} \hfil\bfseries\large}
\makeatother
\renewcommand{\cftchapleader}{\hfill}
\cftpagenumbersoff{chapter} % no page numbers for chapters
\renewcommand{\cftchapafterpnum}{\cftparfillskip}

\renewcommand{\cftsecpresnum}{\S\ } % put section symbol before section numbers

% then remainder of preamble and document

For more information read the manual (> texdoc tocloft).

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.