1

How to format a single entry in the table of contents?

For example, how to add some horizontal space to indent some specific line, including the section number?

ps: I know that I can edit the toc file but to do that any time is boring.

  • untestet: \addtocontents{toc}{\protect\addvspace{1cm}} – Johannes_B Oct 28 '14 at 17:45
  • @Johannes_B, it is inserting vertical space. I'd like to format the entry with horizontal space. – Sigur Oct 28 '14 at 17:49
  • \documentclass{report} \begin{document} \tableofcontents \chapter{tester} \addcontentsline{toc}{chapter}{\hspace{1cm}\protect\numberline{\thechapter}Text} \end{document} – Johannes_B Oct 28 '14 at 17:54
1

Quickly defining a new package to keep the preamble clean. You now have the command \emphchapter taking a mandatory argument, and an optional for the toc if needed. There is a starred variant as well, setting an unnumbered chapter with indent to the toc. After loading the package, you can use \setkomafont{emphfont} or addtokomafont{emphfont} to format the toc entry. The indent can be changed by using the usual setlength to length emphindent.

Patching arguments inside macros can be troublesome, you can use package etoolbox and package regexpatch. Examples can be found at Patching arguments inside a macro

Setting the color to blue using addtokomafont gives something like this:

sigurTocEmph

\begin{filecontents}{emphchapter.sty}
\ProvidesPackage{emphchapter}[2014/10/29 TeX.SX demo package]
\RequirePackage{regexpatch}
\tracingxpatches
\PassOptionsToPackage{x11names}{xcolor}
\RequirePackage{xcolor}
\RequirePackage{scrkbase}
\RequirePackage{xparse}
\newlength{\emphindent}
\newlength{\starredemphindent}
\setlength{\emphindent}{1cm}
\let\starredemphindent\emphindent
\newkomafont{emphfont}{\normalfont\scshape}


\DeclareDocumentCommand{\numemphfontswitch}{}{\protect\usekomafont{emphfont}\protect\hspace{\emphindent}}
\DeclareDocumentCommand{\starredemphfontswitch}{}{\protect\usekomafont{emphfont}\hspace{\starredemphindent}}

\DeclareDocumentCommand{\patchchapter}{}{
\makeatletter
\xpatchcmd{\@chapter}{%
{\protect\numberline{\thechapter}##1}%
}{%
{\numemphfontswitch\protect\numberline{\thechapter}##1}%
}{}{}
\makeatother
}

\NewDocumentCommand{\emphchapter}{ s o m }{
\IfBooleanTF{#1}{
\begingroup
\typeout{starred emphchapter}
\chapter*{#3}
\IfNoValueTF{#2}{
    \addcontentsline{toc}{chapter}{\starredemphfontswitch#3}
}{
    \addcontentsline{toc}{chapter}{\starredemphfontswitch#2}
}
\endgroup
}
{
\typeout{numbered emphchapter}
\begingroup
\patchchapter
\IfNoValueTF{#2}{
\chapter{#3}
}{
\chapter[#2]{#3}
}
\endgroup
}
}

\endinput
\end{filecontents}



\documentclass{report}
\usepackage{emphchapter}
\usepackage{blindtext}
\addtokomafont{emphfont}{\color{blue}}
\begin{document}
\tableofcontents
\chapter{tester}
\blindtext
\emphchapter{emphchapter}
\emphchapter[optional numbered emph]{emphchapter}
\blindtext
\chapter{behind emphchapter}
\blindtext
\chapter{before starred}
\blindtext
\emphchapter*{starred emphchapter}
\blindtext
\emphchapter*[optional starred entry]{starred emphchapter with optional}
\blindtext
\chapter{tester}
\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.