Menu

HowTo_SectionPrefix

How to center all headings horizontally and add a prefix line

With scrreprt and scrbook, it is possible to set the chapter headings so that ”Chapter“ is followed by the number in a separate line and the title is then placed underneath. I was once asked whether this is also possible for the levels \section to \subsubsection. At the same time, all these headings should be centered horizontally.

Centering is very easy:

\RenewCommandCopy{\raggedchapter}{\centering}
\RenewCommandCopy{\raggedsection}{\centering}

So \raggedchapter and \raggedsection are set equal to \centering. Alternatively, you could also use \renewcommand instead of \RenewCommandCopy. However, TeX then requires one more expansion step.

To add a prefix line to the headings, redefine \sectionlinesformat. The original definition is:

\newcommand{\sectionlinesformat}[4]{%
  \@hangfrom{\hskip #2#3}{#4}%
}

As we are centering anyway, we no longer need the indent \hskip #2. For the same reason, the hanging indent from the second line no longer makes sense. Instead, we want a line break after the number, i.e. #3. The new definition is therefore:

\renewcommand*{\sectionlinesformat}[4]{%
  #3\\*
  #4%
}

However, this leads to a problem with unnumbered sections such as the table of contents in scrartcl or \subsubsection in the default settings of scrbook and scrreprt. We can easily intercept this with the \IfUseNumber statement, which can be used within \sectionlinesformat to differentiate between numbered and unnumbered headings:

\renewcommand*{\sectionlinesformat}[4]{%
  \IfUseNumber{#3\\*}{}%
  #4%
}

If there is to be a ”Section“ before the number, this should preferably be added to the definition of \sectionformat, \subsectionformat, \subsubsectionformat:

\renewcommand*{\sectionformat}{Abschnitt~\thesection\autodot}
\renewcommand*{\subsectionformat}{Abschnitt~\thesubsection\autodot}
\renewcommand*{\subsubsectionformat}{Abschnitt~\thesubsubsection\autodot}

In total you get:

\documentclass[chapterprefix]{scrreprt}
\usepackage{mwe}

\RenewCommandCopy{\raggedchapter}{\centering}
\RenewCommandCopy{\raggedsection}{\centering}
\renewcommand*{\sectionlinesformat}[4]{%
  \IfUseNumber{#3\\*}{}%
  #4%
}
\renewcommand*{\sectionformat}{Section~\thesection\autodot}
\renewcommand*{\subsectionformat}{Section~\thesubsection\autodot}
\renewcommand*{\subsubsectionformat}{Section~\thesubsubsection\autodot}

\begin{document}
\tableofcontents
\blinddocument
\end{document}

If you want to extend the whole thing to \paragraph and \subparagraph, you only have to additionally apply "How to display \paragraph on a line of its own instead of as a running headline".


Related

Wiki (English): HowTo_ParagraphNonRunin

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.