3

my question is basically a follow-up on Adding a Prefix to Section Title, where the question is how to add an arbitrary prefix to the number that appears before a headline/section.

My follow-up question is: What do I need to do in order to ensure that the TOC has enough space so that the prefix-number does not overlap with the headline?

Consider the following MWE:

\documentclass{scrartcl}

\renewcommand*{\thesection}{Some Prefix~\arabic{section}:}

\begin{document}

  \tableofcontents  

  \section{First Headline}
  Some text.

  \section{Second Headline}
  More text!

  \section{Third Headline}
  And so on...  

\end{document}

Without the second line (i.e., the renewcommand) the result looks as expected:

enter image description here

With the renewcommand, the sections themselve appear correct (btw: this is is the solution to the question linked above), but the text in the TOC overlaps:

enter image description here

Thus,

Question: What do I need to do to have enough space before the actual headline begins in the toc?

1

1 Answer 1

2

Since you are using a koma class, you can use tocbasic to format your toc. The following should work without a koma class as well, if you load tocbasic explicitly (usepackage{tocbasic}). You can choose two different approaches. In general I'd recommend you to check out the koma manual (English or German).

Increase the space for the section number (+ prefix)

To increase the space left of the section name in toc, you can do the following:

\DeclareTOCStyleEntry[numwidth=8em]{tocline}{section}

This makes all section names start at the same position, 8em from the left.

Dynamic width depending on length of prefix + number

Alternatively, you can put the section names in toc just after the longest number (+prefix) with

\DeclareTOCStyleEntry[dynnumwidth=true]{tocline}{section}

You could add numsep=1em to the arguments to set the space between the number and name to a different value:

\DeclareTOCStyleEntry[dynnumwidth=true,
                      numsep=1em,
]{tocline}{section}

Both in a MWE

\documentclass{scrartcl}

\renewcommand*{\thesection}{Some Prefix~%
  \if\arabic{section}1Ooone\else
    \if\arabic{section}2Two\else%
      \if\arabic{section}33%
      \fi
    \fi
  \fi:}

% Try *one* of the following styles. They add up!
\DeclareTOCStyleEntry[dynnumwidth=true,
                      numsep=1em,
]{tocline}{section}

%\DeclareTOCStyleEntry[numwidth=10em]{tocline}{section}

\begin{document}

  \tableofcontents  

  \section{First Headline}
  Some text.

  \section{Second Headline}
  More text!

  \section{Third Headline}
  And so on...  

\end{document}

I changed the prefix in length, to show the effect of dynnumwidth.

First style, with dynnumwidth:

result1

Second style, fixed:

result2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .