For KOMA-Script classes you do not need an additional package. At KOMA-Script classes the numbers of sectioning titles are printed by \partformat
, \chapterformat
, \sectionformat
, \subsectionformat
… \subparagraphformat
. So I first suggestion could be to redefine these commands:
\documentclass{scrreprt}
\usepackage{blindtext}
\setcounter{secnumdepth}{\paragraphnumdepth}% To show the result for all
% levels down to paragraph
\newcommand*{\numberinmargin}[1]{%
\makebox[0pt][r]{#1\autodot\hskip\marginparsep}}
\renewcommand*{\chapterformat}{\numberinmargin{\thechapter}}
\renewcommand*{\sectionformat}{\numberinmargin{\thesection}}
\renewcommand*{\subsectionformat}{\numberinmargin{\thesubsection}}
\renewcommand*{\subsubsectionformat}{\numberinmargin{\thesubsubsection}}
\renewcommand*{\paragraphformat}{\numberinmargin{\theparagraph}}
\begin{document}
\chapter{A Chapter Title}
\blindtext
\section{A Section Title}
\blindtext
\subsection{A Subsection Title}
\blindtext
\subsubsection{A Subsubsection Title}
\blindtext
\paragraph{A Paragraph Title}
\blindtext
\end{document}
You can left align the numbers by using another definition of \numberinmargin
, e.g.:
\newcommand*{\numberinmargin}[1]{%
\makebox[0pt][r]{%
\makebox[\marginparwidth][l]{#1\autodot}%
\hskip\marginparsep
}%
}
would result in:
But if you use the same code for \subparagraph
:
\setcounter{secnumdepth}{\subparagraphnumdepth}
\renewcommand*{\subparagraphformat}{\numberinmargin{\thesubparagraph}}
in the document preamble and
\subparagraph{A Subparagraph Title}
\blindtext
in the body you will find, that it does not work:
Why does it fail? The default of \subparagraph
is to indent the heading. So the \makebox[0pt][r]{…}
, that prints the content (= the number) right from the current position, now does not print from the left edge of the text area but from the indent position.
We could work around the issue by removing the indent:
\RedeclareSectionCommand[indent=0pt]{subparagraph}
But if we want to respect the indent, that could also be configured for other section levels, we have make a step down the stairs to a lower level interface of KOMA-Script and redefine \sectionlinesformat
and \sectioncatchphraseformat
.
\sectionlinesformat
is used to format the sectioning titles of section levels, that are printed displayed. By default these are \section
, \subsection
and \subsubsection
. The default definition (given in the KOMA-Script manual) is:
\newcommand{\sectionlinesformat}[4]{%
\@hangfrom{\hskip #2#3}{#4}%
}
The first argument it the name of the sectioning level, i. e., section
, subsection
, subsubsection
. The second argument is the indent. The third argument is the number given by \…format
and the fourth argument is the title text. \@hangfrom
is used to format a hanging title for multi-line titles. Assume we want the number in the margin but the indent in the text area, we can move #2' to the front still using the redefined
\…format` commands:
\renewcommand{\sectionlinesformat}[4]{%
#3\@hangfrom{\hskip #2}{#4}%
}
\sectioncatchpraseformat
is used for section headings that at printed in-text as a catch phrase. Usually this is used for \paragraph
and \subparagraph
. The default is similar to \sectionlinesformat
but without \@hangfrom
:
\newcommand{\sectioncatchphraseformat}[4]{%
\hskip #2#3#4%
}
The redefinition would be similar, too:
\renewcommand{\sectioncatchphraseformat}[4]{%
#3\hskip #2#4%
}
Let's use a right aligned version of \numbersinmargin
again:
\documentclass{scrreprt}
\usepackage{blindtext}
\setcounter{secnumdepth}{\subparagraphnumdepth}% To show the result for all
% levels down to subparagraph
\newcommand*{\numberinmargin}[1]{%
\makebox[0pt][r]{%
\makebox[\marginparwidth][r]{#1\autodot}%
\hskip\marginparsep
}%
}
\renewcommand*{\chapterformat}{\numberinmargin{\thechapter}}
\renewcommand*{\sectionformat}{\numberinmargin{\thesection}}
\renewcommand*{\subsectionformat}{\numberinmargin{\thesubsection}}
\renewcommand*{\subsubsectionformat}{\numberinmargin{\thesubsubsection}}
\renewcommand*{\paragraphformat}{\numberinmargin{\theparagraph}}
\renewcommand*{\subparagraphformat}{\numberinmargin{\thesubparagraph}}
\makeatletter% because of @ in \@hangfrom
\renewcommand{\sectionlinesformat}[4]{%
#3\@hangfrom{\hskip #2}{#4}%
}
\makeatother% switch back from \makeatletter
\renewcommand{\sectioncatchphraseformat}[4]{%
#3\hskip #2#4%
}
\begin{document}
\chapter{A Chapter Title}
\blindtext
\section{A Section Title}
\blindtext
\subsection{A Subsection Title}
\blindtext
\subsubsection{A Subsubsection Title}
\blindtext
\paragraph{A Paragraph Title}
\blindtext
\subparagraph{A Subparagraph Title}
\blindtext
\end{document}
As you can see, the \subparagraph
title is indented now, without interfering the position of the number.
If you want to handle also chapter title with prefix line (see option chapterprefix
in the KOMA-Script manuals) or part titles you need additional definitions. For example for prefix lines you could move the number to the margin like above but the term Chapter
stay in the text area:
\renewcommand*{\chapterformat}{%
\numberinmargin{\thechapter}\chapappifchapterprefix{}%
}
If you now add option chapterprefix
to \documentclass
you will get:
Let me say: I do not like this result and would suggest not to use chapterprefix
.
For part titles you could do similar, but I also do not like the result, so I do not show it.