Menu

HowTo_HeadingsNumbersSameWidth

How the indent the text of of all heading levels the same size regardless of the width of the number

Normally, the KOMA-Script classes in headings set a fixed distance between the number and the text of the heading. If instead the text of the headings should always have the same distance to the left margin regardless of the level and the width of the number, the instructions \chapterformat ... \subparagraphformat must be redefined accordingly. For \subparagraph, the indentation should also be deactivated via \RedeclareSecionCommand[indent=0pt]{subparagraph}. However, by default, at most the levels \chapter to \subsection resp. \section to \subsubsection are numbered. We will therefore limit ourselves to these here:

\documentclass{scrreprt}
\usepackage{blindtext}

% Wie der Texteinzug aller Überschriftenebenen unabhängig von der Breite der
% Nummer gleich groß gewählt werden kann
% → https://sourceforge.net/p/koma-script/wiki-de/HowTo_HeadingsNumbersSameWidth/
% How the indent the text of of all heading levels the same size regardless of
% the width of the number
% → https://sourceforge.net/p/koma-script/wiki-en/HowTo_HeadingsNumbersSameWidth/

\newcommand{\headingnumberbox}[1]{%
  \makebox[1.5cm][l]{#1\autodot\enskip\hfill}%
}
\Ifundefinedorrelax{chapterformat}{}{%
  \renewcommand*{\chapterformat}{\headingnumberbox{\thechapter}}%
}
\renewcommand*{\sectionformat}{\headingnumberbox{\thesection}}
\renewcommand*{\subsectionformat}{\headingnumberbox{\thesubsection}}
\renewcommand*{\subsubsectionformat}{\headingnumberbox{\thesubsubsection}}

\begin{document}
\tableofcontents

\blinddocument
\end{document}

Note: When defining \headingnumberbox, neither the unit em nor ex can be used, because otherwise the indentation would depend on the font size of the heading level. In addition, the example shown does not support the chapterprefix option, as the demand would make no sense in this case. Of course, the numbers could also be right-aligned. To do this, simply adjust the definition of \headingnumberbox:

\newcommand{\headingnumberbox}[1]{%
  \makebox[1.5cm][r]{\hfill #1\autodot\enskip}%
}

However, the result does not look very pleasing.


Related

Wiki (English): HowTo_Headings

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.