# Add ToC to ToC with dotted leaders and page number while omitting them for every other entry

Using scrbook.

I have removed leader dots and page number from all numbered chapters, so that only unnumbered ones have them (e.g. acknowledgement, abstracts, list of figures, list of tables, etc). I used in my preamble

\setkomafont{chapterentrypagenumber}{\nullfont}
\renewcommand\addchaptertocentry[2]{%
\IfArgIsEmpty{#1}
{\addtocontents{toc}
{\protect\begingroup
\protect\setkomafont{chapterentrypagenumber}{}%
\protect\KOMAoptions{chapterentrydots}%
}%
\addtocentrydefault{chapter}{}{#2}%
\addtocontents{toc}{\protect\endgroup}%
}
{\addtocentrydefault{chapter}{#1}{#2}}%
}


All is well until I am asked to have the ToC entry in the Table of Content, with leader dots and page number. Using \addcontentsline{toc}{chapter}{Table of Contents} puts ToC in Table of Contents without them---I assume because of above codes. I need the leader dot-less numbered chapters, but I also need the ToC entry with those leader dots and page number. Appreciate any help.

• When you read the table of contents, you want to know on which page you'll find it? :o) – Bernard Sep 17 '17 at 17:23
• The documented solution for a KOMA-Script class is \setuptoc{toc}{totoc}. – esdd Sep 17 '17 at 17:35
• If the definition of \addchaptertocentry should be used, replace \addcontentsline by \addxcontentsline. Additionally you have to ensure that the number of the first TOC page will be in TOC. Therefore you can use \AfterTOCHead[toc]{...}in your preamble. But recommended is \setuptoc{toc}{totoc}. – esdd Sep 17 '17 at 18:10
• @Bernard I argued the same, but it did not work. I'll follow what they suggested, and try to graduate :-) – M. Ridha Siregar Sep 17 '17 at 19:11

## 2 Answers

With scrbook use \setuptoc{toc}{totoc}. This command is provided by the KOMA-Script package tocbasic:

\documentclass{scrbook}

\setuptoc{toc}{totoc}

\setkomafont{chapterentrypagenumber}{\nullfont}
\renewcommand\addchaptertocentry[2]{%
\IfArgIsEmpty{#1}
{\addtocontents{toc}
{\protect\begingroup
\protect\setkomafont{chapterentrypagenumber}{}%
\protect\KOMAoptions{chapterentrydots}%
}%
\addtocentrydefault{chapter}{}{#2}%
\addtocontents{toc}{\protect\endgroup}%
}
{\addtocentrydefault{chapter}{#1}{#2}}%
}

\begin{document}
\tableofcontents
\chapter{Test}
\end{document}


Result:

With https://tex.stackexchange.com/a/391849/43317 and redefined \contentsname:

\documentclass[
12pt,a4paper,
chapterprefix=true,% chapterprefixline does the same
headings=optiontoheadandtoc,
oneside
]{scrbook}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{lipsum}
\usepackage[T1]{fontenc}% <- added
\usepackage{mathptmx}

\usepackage{xpatch}% needed to path \addchaptertocentry

% Style Table of Content.
\setuptoc{toc}{totoc}
\setkomafont{chapterentrypagenumber}{\nullfont}
\newcommand\chapterentrynumberformat[1]{\MakeUppercase\chapapp\ #1\kern 0.25em---\kern 0.25em}
\xpatchcmd{\addchaptertocentry}
{\addtocentrydefault{chapter}{#1}{#2}}
{\IfArgIsEmpty{#1}
{\addtocontents{toc}
{\protect\begingroup
\protect\setkomafont{chapterentrypagenumber}{}%
\protect\KOMAoptions{chapterentrydots}%
}%
\addtocentrydefault{chapter}{}{\MakeUppercase{#2}}%
\addtocontents{toc}{\protect\endgroup}%
}
{\addtocentrydefault{chapter}{}{\protect\chapterentrynumberformat{#1}\MakeUppercase{#2}}}%
}{}{\PatchFailed}
\renewcommand\contentsname{Table Of Contents}
%% Document Parts %%
\begin{document}
\frontmatter
\pagestyle{plain}
\tableofcontents
\addchap{ABSTRAK} % Abstract in language other than English
\lipsum[1]
\addchap[tocentry=\textit{ABSTRACT}]{ABSTRACT} % Abstract in English, must be shown italicised in ToC
\lipsum[2]
\mainmatter
\addchap{SOMESTUFFS}
\lipsum[3]
\chapter{FOO}
\section{Bar}
\lipsum[4]
\end{document}


• This works perfectly. p.s. You helped solving two of my problems. Thank you :-) – M. Ridha Siregar Sep 17 '17 at 18:52
• One quick question --- If I use hyperref package with that, what should I do? My (more complex) codes throw error \MakeUppercaseUnsupportedInPdfStrings, which I stops using \pdfstringdefDisableCommands. Any way to avoid this? – M. Ridha Siregar Sep 18 '17 at 15:06
• @M.RidhaSiregar Use \texorpdfstring - see my updated answer to your other question. – esdd Sep 19 '17 at 8:36
• Awesome. The \MakeUppercaseUnsupportedInPdfStrings is now gone. Thank you very much! – M. Ridha Siregar Sep 19 '17 at 11:07

Obviously you want to add the ToC as a ToC entry treating it typographically different which makes sense. Your guidelines how the formatting should be are biased however (in my opinion). Including the page number seems quite obscure to me.

However, your starting point was quite right. You need to include the entry manually: but not using \addcontentsline. This will employ the formatting used by every other ToC entry - which you are trying to avoid. There is a more general tool to add material to the ToC, i.e. write to the .toc file which is the similarly named \addtocontents command. You could use it like

\addtocontents{toc}{\textbf{\contentsname}\dotfill\thepage\par}


To ensure the right page numbering the page number should be fetched with \pageref and of course a \lable right before \tableofcontents. This can be done by patching the \tableofcontents macro in the preamble and leave the document body untouched - which is convenient anyways.

\makeatletter
\let\latex@tableofcontents\tableofcontents
\def\tableofcontents{%
\label{chap:toc}%
\latex@tableofcontents
\addtocontents{toc}{%
\bgroup\sffamily\textbf{\contentsname}\dotfill\pageref{chap:toc}\egroup\par
}%
}
\makeatother


Note that I used \frontmatter and \mainmatter in the examples (see below). They ensure that page numbering is different in the front and main matter which makes more sense if you want to have a page number for the ToC-ToC-entry. If you need roman numbering throughout you can omit those commands.

Moreover you could now use the above soultion to create even a different formatting:

\documentclass{scrbook}
\usepackage[x11names]{xcolor}

\setkomafont{chapterentrypagenumber}{\nullfont}
\renewcommand\addchaptertocentry[2]{%
\IfArgIsEmpty{#1}
{\addtocontents{toc}
{\protect\begingroup
\protect\setkomafont{chapterentrypagenumber}{}%
\protect\KOMAoptions{chapterentrydots}%
}%
\addtocentrydefault{chapter}{}{#2}%
\addtocontents{toc}{\protect\endgroup}%
}
{\addtocentrydefault{chapter}{#1}{#2}}%
}

\makeatletter
\newcommand*\addspecialchaptertotoc[2][\thepage]{%
\addtocontents{toc}{\vskip.25pc\bgroup{\color{Coral3}\MakeUppercase{#2}}\hfill#1\egroup\par}%
}
\let\latex@tableofcontents\tableofcontents
\def\tableofcontents{%
\label{chap:toc}
\latex@tableofcontents
\addspecialchaptertotoc[\pageref{chap:toc}]{\contentsname}
}
\makeatother

\begin{document}
\frontmatter
\tableofcontents
\chapter*{Preface}\addspecialchaptertotoc{Preface}
\mainmatter
\chapter{Test chapter}
\section{First subsection}
\section{Second subsection}
\end{document}

• The redefinition of \addchaptertocentry inserts the dots for unnumbered chapters. This command will be used if \addcontentsline would be replaced by \addxcontentsline. But the OP have to ensure that the entry is for the first and not the last page of the TOC. \AfterTOCHead[toc]{\addxcontentsline{toc}{chapter}{\contentsname}} would work. But the recommended way ist \setup{toc}{totoc}. See the KOMA-Script documentation. – esdd Sep 17 '17 at 18:06