I'm new to LaTex and need some help. I want to have a TOC where the chapter numbers have a background color and a different color than the chapter title. Another thing I want to do ist a border on the top of every level 1 chapter in the TOC.

Here is a picture of what I mean:

enter image description here

I hope someone can help me please. Thank you in advance.

  • 3
    Welcome to TeX.SX! On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are either "off topic", "too broad", or "unclear". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. – Stefan Pinnow Aug 20 '18 at 8:56
  • A lot depends on what document class and which packages you are using. The easy way to do the graphics is to use TikZ, but you could probably achieve the same results with xcolor, \fbox and \hrule. – John Kormylo Aug 20 '18 at 14:47

In article class, a section entry in TOC is rendered using macro \l@section:

$ latexdef l@section

macro:#1#2->\ifnum \c@tocdepth >\z@ \addpenalty \@secpenalty \addvspace {1.0em \@plus \p@ }\setlength \@tempdima {1.5em}\begingroup \parindent \z@ \rightskip \@pnumwidth \parfillskip -\@pnumwidth \leavevmode \bfseries \advance \leftskip \@tempdima \hskip -\leftskip #1\nobreak \hfil \nobreak \hb@xt@ \@pnumwidth {\hss #2}\par \endgroup \fi 

Furthermore, the #1 will start with \numberline{<section number>} and

$ latexdef numberline

macro:#1->\hb@xt@ \@tempdima {#1\hfil }

Based upon this, one can hack \numberline during execution of \l@section and insert then the needed code to obtain graphical result. For that, I used low-level coding. See tocloft package explanations about how LaTeX by default hard codes the TOC. The tocloft package provides hooks into this which may facilitate the task here, but I have not tried. You may however need to use it if you want more customization than what I achieve here. With package etoc you can obtain any desired looks, but it requires doing it all (i.e. the \l@section stuff above is not enriched with hooks but simply completely rewritten using user provided coding in \etocsetstyle{section}{}{}{}{}.) However example in the documentation can serve as starting point.



\usepackage{hyperref}% to check compatibility



\def\my@numberline #1%
          \hb@xt@ \@tempdima
              {\kern-\fboxsep\colorbox{red}{\color{white}#1.}% I added a dot here




\section{Objekt und Zweck}


\subsection{Allgemeine Legislative Einleitung}


enter image description here

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.