0

Is it possible to customize the table of contents as per the attached screen shot ? The following are required.

  1. Chapter Name and Page Numbers in Red Color
  2. Section Name and Page Numbers in Blue Color
  3. Dots between chapter / section title and page numbers

Here is the code that needs to be customized.

\documentclass{amsbook}
    \usepackage{xpatch,fmtcount}
    \usepackage[colorlinks=true]{hyperref}
    \renewcommand{\sectionname}{Unit}
    \renewcommand{\thechapter}{\ifnum\value{chapter}<10 0\fi\arabic{chapter}}
    \makeatletter
    % remove the period from the toc
    \renewcommand{\tocsection}[3]{%
    \indentlabel{\@ifnotempty{#2}{\ignorespaces#1 #2\quad}}#3} % was #2.\quad
    \let\tocchapter\tocsection
    \let\tocsubsection\tocsection
    \let\tocparagraph\tocsection
    \let\tocsubparagraph\tocsection
    % remove the period from the section numbers
    \renewcommand\@seccntformat[1]{%
    \protect\textup{\protect\@secnumfont
    \csname the#1\endcsname
    \protect\enspace % was \@secnumpoint
    }%
    }
    % remove the period after the titles
    \patchcmd{\@sect}{\@addpunct.}{}{}{}
    % fix the page style
    \def\ps@headings{\ps@empty
    \def\@evenhead{%
    \setTrue{runhead}%
    \normalfont\scriptsize
    \hfil % <---- was \rlap{\thepage}\hfil
    \def\thanks{\protect\thanks@warning}%
    \leftmark{}{}\hfil}%
    \def\@oddhead{%
    \setTrue{runhead}%
    \normalfont\scriptsize \hfil
    \def\thanks{\protect\thanks@warning}%
    \rightmark{}{}\hfil}% <--- was \hfil\llap{\thepage}
    % taken from \ps@plain
    \def\@oddfoot{\normalfont\scriptsize \hfil\thepage\hfil}%
    \let\@evenfoot\@oddfoot
    %
    \let\@mkboth\markboth
    \def\partmark{\@secmark\markboth\partrunhead\partname}%
    \def\chaptermark{%
    \@secmark\markboth\chapterrunhead{}}%
    \def\sectionmark{%
    \@secmark\markright\sectionrunhead\sectionname}%
    }
    \makeatother
    \renewcommand{\thesection}{\padzeroes[2]{\decimal{chapter}}-\padzeroes[2]{\decimal{section}: }}
    \begin{document}
    \tableofcontents
    \chapter{First}
    \section{one}
    \chapter{Second}
    \section{two}
    \section{three}
    \chapter{Third}
    \end{document}

enter image description here

1 Answer 1

1

b

This is the code for the three requirements.

\documentclass{amsbook}

\usepackage{xpatch,fmtcount}
\usepackage{xcolor} % added

\usepackage{hyperref} %changed to control the colors in TOC
\hypersetup{
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}

\makeatletter
\global\def\param{} 
% color of page numbers in TOC <<<<<<<<<<<<<<<<
\renewcommand{\@tocpagenum}[1]{\hss{\mdseries\ifx\param\empty\color{red}\else\color{blue}\fi #1}}

\renewcommand{\tocsection}[3]{% style of section in TOC <<<<<<<<<<
\global\def\param{section} \color{blue}
\indentlabel{\@ifnotempty{#2}{\ignorespaces#1\;#2\;}}#3\dotfill} % spaces, color and dots

\renewcommand{\tocchapter}[3]{%% style of chapter in TOC <<<<<<<<<<
\global\def\param{}\color{red}
\indentlabel{\@ifnotempty{#2}{\ignorespaces#1\;#2\enspace}}#3\dotfill} % spaces,color and dots

\let\tocsubsection\tocsection
\let\tocparagraph\tocsection
\let\tocsubparagraph\tocsection

\renewcommand{\sectionname}{Unit}
\renewcommand{\thechapter}{\ifnum\value{chapter}<10 0\fi\arabic{chapter}}

% remove the period from the section numbers
\renewcommand\@seccntformat[1]{%
    \protect\textup{\protect\@secnumfont
        \csname the#1\endcsname
        \protect\enspace % was \@secnumpoint
    }%
}

% remove the period after the titles
\patchcmd{\@sect}{\@addpunct.}{}{}{}
% fix the page style
\def\ps@headings{\ps@empty
    \def\@evenhead{%
        \setTrue{runhead}%
        \normalfont\scriptsize
        \hfil % <---- was \rlap{\thepage}\hfil
        \def\thanks{\protect\thanks@warning}%
        \leftmark{}{}\hfil}%
    \def\@oddhead{%
        \setTrue{runhead}%
        \normalfont\scriptsize \hfil
        \def\thanks{\protect\thanks@warning}%
        \rightmark{}{}\hfil}% <--- was \hfil\llap{\thepage}
    % taken from \ps@plain
    \def\@oddfoot{\normalfont\scriptsize \hfil\thepage\hfil}%
    \let\@evenfoot\@oddfoot
    %
    \let\@mkboth\markboth
    \def\partmark{\@secmark\markboth\partrunhead\partname}%
    \def\chaptermark{%
        \@secmark\markboth\chapterrunhead{}}%
    \def\sectionmark{%
        \@secmark\markright\sectionrunhead\sectionname}%
}
\makeatother
\renewcommand{\thesection}{\padzeroes[2]{\decimal{chapter}}-\padzeroes[2]{\decimal{section}:}}% extra space removed <<<<
\begin{document}
    \tableofcontents
    \chapter{First}
    \section{one}
    \chapter{Second}
    \section{two}
    \section{three}
    \chapter{Third}
\end{document}

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.