[Tex/LaTex] Custom table of contents

etoctable of contentstocloft

I think Sakurai's Modern Quantum Mechanics table of contents is really beautiful. How can I create it in LaTeX?

Sakurai's table of contents

I tried to use etoc and tocloft but I'm new to this stuff and couldn't make any real progress.

Best Answer

Special setups require special means ;-)

The spacings etc. can be done with tocloft features, however, this changes the \tableofcontents command too much.

The section page numbers in the section title line require some change to the \@sect command.

\documentclass{book}

\makeatletter
\let\latextableofcontents\tableofcontents % Save the original version
\makeatother


\usepackage{amssymb}
\usepackage{tocloft}


\newlength{\commonindentshift}

\setlength{\commonindentshift}{15pt}

\renewcommand{\cftchapaftersnum}{\hfill$\blacksquare$\hfill}
\addtolength{\cftchapnumwidth}{\commonindentshift} % increase the space
\renewcommand{\cftchapdotsep}{\cftnodots}
\renewcommand{\cftsecdotsep}{\cftnodots}
\renewcommand{\cftsubsecdotsep}{\cftnodots}
\renewcommand{\cftsubsubsecdotsep}{\cftnodots}
\renewcommand{\cftchappagefont}{\Large\bfseries}
\addtolength{\cftsecindent}{\commonindentshift}
\addtolength{\cftsubsecindent}{\commonindentshift}
\addtolength{\cftsubsubsecindent}{\commonindentshift}


\renewcommand{\cftchapfont}{\Large\bfseries}

\newcommand{\starredchapter}[1]{%
  \chapter*{#1}%
  \addtocontents{toc}{\protect\contentsline{chapter}{\hskip\cftchapnumwidth #1}{\thepage}}
}  

\usepackage{xpatch}


\makeatletter
\AtBeginDocument{%
\xpatchcmd{\latextableofcontents}{%
  \chapter*{\contentsname
    \@mkboth{%
      \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
}{%
  \chapter*{%
    \centering \contentsname
    \@mkboth{%
      \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
}{\typeout{Patched!!!}}{\typeout{Patch failure}}
\let\tableofcontents\latextableofcontents

}


\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addtocontents{toc}{\protect\contentsline{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7 \textit{\thepage}}{}}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
    \csname #1mark\endcsname{#7}%
    \addtocontents{toc}{\protect\contentsline{#1}{%
        \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7 \textit{\thepage}}{}}}%
    \fi
  \@xsect{#5}
}
\makeatother


\begin{document}

\makeatletter
\makeatother
\tableofcontents


\starredchapter{Preface}
\starredchapter{In memoriam}


\chapter{First}
\section{First of first chapter}
\section{Second of first chapter}
\subsection{First sub section of 2nd section}
\chapter{Second}

\end{document}

enter image description here