Menu

HowTo_NumbersAtLeftMargin

How to print the heading numbers in the left margin

KOMA-Script offers the experienced typographer possibilities that are not to be suspected at first sight. For example, with the redefinition of a few macros, which are also documented in the manual, it is possible to place the heading numbers in the left margin in such a way that the text of the heading is left-aligned with the text area. However, this approach is only recommended if the left margin is appropriately wide. As a rule, this is only the case with single-page documents.

\documentclass{scrreprt}
\usepackage{blindtext}

% How to print the heading numbers into the left margin
% → <https://sourceforge.net/p/koma-script/wiki-en/HowTo_Headings/HowTo_NumbersAtLeftMargin>
% Wie die Überschriftennummern in den linken Rand ausgerückt werden können
% → <https://sourceforge.net/p/koma-script/wiki-de/HowTo_Headings/HowTo_NumbersAtLeftMargin>

\Ifundefinedorrelax{chapterformat}{}{%
  \renewcommand*{\chapterformat}{\makebox[0pt][r]{\chapappifchapterprefix{\ }\thechapter\autodot\hspace{\marginparsep}}}%
}
\renewcommand*{\sectionformat}{\makebox[0pt][r]{\thesection\autodot\hspace{\marginparsep}}}
\renewcommand*{\subsectionformat}{\makebox[0pt][r]{\thesubsection\autodot\hspace{\marginparsep}}}
\renewcommand*{\subsubsectionformat}{\makebox[0pt][r]{\thesubsubsection\autodot\hspace{\marginparsep}}}

\setcounter{secnumdepth}{\subsubsectionnumdepth}% For demonstration number down to \subsubsections.

\begin{document}
\tableofcontents

\blinddocument
\end{document}

The trick in the solution shown here is to set the heading numbers right-justified in a box of the alleged zero width. This places them to the left of the current position. Unless an indent is explicitly defined with \RedeclareSectionCommand, the current position when the number is output is normally the left edge of the text area. At the end of the output a space of \marginparsep is added. This is the space that is normally also inserted between the text column and margin notes. Of course, users can choose any other spacing instead.

The redefinition of the output of the chapter number, \chapterformat, is done only if the statement was also defined before. With the scrreprt class used in the example, this restriction would not be necessary. The same is true for scrbook. However, with scrartcl, redefining \chapterformat would result in an error. By testing with \Ifundefinedorrelax, however, the code can be used unchanged for scrartcl as well.

For documents with option chapterprefix=true or with option appendixprefix=true the formatting shown here makes no sense. The redefinition should not be used in this case.

Theoretically, the method shown could also be applied to \paragraph and \subparagraph. However, it is very unusual to in-line headings at all and then possibly move the number out into the margin. Furthermore, the headings of the \subparagraph layer are additionally indented, so it would be completely nonsensical to put the number in the margin. I therefore advise against using similar definitions with these two levels. Alternatively, one should then also redefine these levels as free-standing headings instead of as in-line headings. This is easily done with \RedeclareSectionCommand.


Related

Wiki (English): HowTo_Headings
Wiki (English): HowTo_NumbersAtOuterMargin

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.