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}
\setuptoc{toc}{totoc}
.\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}
.