Menu

HowTo_NumbersAtOuterMargin

How to print the heading numbers in the outer margin

After “How to print the heading numbers in the left margin” already showed how the number can simply be moved to the left margin in single-page documents, the question was obvious how to achieve something similar in double-page documents. However, the number should then no longer always be in the left margin, but in the left margin on left pages and in the right margin on right pages—i.e. generally in the outer margin. Furthermore, the text of the heading should no longer be generally left-justified, but left-justified on left pages and right-justified on right pages.

Here it is obviously no longer sufficient to redefine \chapterformat, \sectionformat, \subsectionformat and \subsubsectionformat. Instead, the output of the complete heading must be changed. This is possible with KOMA-Script by redefining \chapterlinesformat and \sectionlinesformat:

\documentclass[twoside]{scrreprt}
\usepackage{blindtext}

% Wie die Überschriftennummern in den äußeren Rand gesetzt werden können
% → <https://sourceforge.net/p/koma-script/wiki-de/HowTo_Headings/HowTo_NumbersAtOuterMargin>
% How to print the headings numbers in the outer margin
% → <https://sourceforge.net/p/koma-script/wiki-en/HowTo_Headings/HowTo_NumbersAtLeftMargin>

\renewcommand*{\chapterlinesformat}[3]{%
  \Ifthispageodd{%
    \parbox[t]{\linewidth}{\raggedleft #3}%
    \makebox[0pt][l]{\hspace{\marginparsep}#2}%
  }{%
    \makebox[0pt][r]{#2\hspace{\marginparsep}}%
    \parbox[t]{\linewidth}{\raggedright #3}%
  }%
}
\renewcommand*{\sectionlinesformat}[4]{%
  \Ifthispageodd{%
    \parbox[t]{\linewidth}{\raggedleft #4}%
    \makebox[0pt][l]{\hspace{\marginparsep}#3}%
  }{%
    \makebox[0pt][r]{#3\hspace{\marginparsep}}%
    \parbox[t]{\linewidth}{\raggedright #4}%
  }%
}

\renewcommand*{\chapterformat}{\thechapter\autodot}
\renewcommand*{\sectionformat}{\thesection\autodot}
\renewcommand*{\subsectionformat}{\thesubsection\autodot}
\renewcommand*{\subsubsectionformat}{\thesubsection\autodot}

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

\begin{document}
\tableofcontents

\chapter{Chapter on even Page}
\blindtext
\section{Section on even page}
\blindtext
\subsection{Subsection on even page}
\blindtext
\subsubsection{Subsubsection on even page}
\blindtext
\paragraph{Paragraph on odd page}
\blindtext
\subparagraph{Subparagraph on odd page}
\Blindtext[4]

\chapter{Chapter on odd Page}
\blindtext
\section{Section on odd page}
\blindtext
\subsection{Subsection on odd page}
\blindtext
\subsubsection{Subsubsection on odd page}
\blindtext

\end{document}

A redefinition of \chapterformat, \sectionformat, \subsectionformat and \subsubsectionformat was still necessary here to remove the space after the number. The space is now set in \chapterlinesformat or \sectionlinesformat instead. Depending on whether it is a right or left page, it is inserted before or after the number.

Especially on right pages I personally find the result very unsatisfactory. However, this is less due to the implementation than to the requirements. With the additional requirement that the numbers should be generously colored in the width of the note column, it looks much better:

\documentclass[twoside]{scrreprt}
\usepackage[svgnames]{xcolor}
\usepackage{blindtext}

% Wie die Überschriftennummern in den äußeren Rand gesetzt werden können
% → <https://sourceforge.net/p/koma-script/wiki-de/HowTo_Headings/HowTo_NumbersAtOuterMargin>
% How to print the headings numbers in the outer margin
% → <https://sourceforge.net/p/koma-script/wiki-en/HowTo_Headings/HowTo_NumbersAtLeftMargin>

\newcommand*{\enlightnumber}[2][l]{%
  {%
    \setlength{\fboxsep}{1ex}%
    \smash{%
      \colorbox{PeachPuff}{%
        \makebox[\dimexpr\marginparwidth-2\fboxrule-2\fboxsep\relax][#1]{#2}%
      }%
    }%
  }%
}

\renewcommand*{\chapterlinesformat}[3]{%
  \Ifthispageodd{%
    \parbox[t]{\linewidth}{\raggedleft #3}%
    \IfArgIsEmpty{#2}{}{%
      \makebox[0pt][l]{\hspace{\marginparsep}%
        \enlightnumber{#2}%
      }%
    }%
  }{%
    \IfArgIsEmpty{#2}{}{%
      \makebox[0pt][r]{%
        \enlightnumber[r]{#2}%
        \hspace{\marginparsep}}%
    }%
    \parbox[t]{\linewidth}{\raggedright #3}%
  }%
}
\renewcommand*{\sectionlinesformat}[4]{%
  \Ifthispageodd{%
    \parbox[t]{\linewidth}{\raggedleft #4}%
    \IfArgIsEmpty{#3}{}{%
      \makebox[0pt][l]{\hspace{\marginparsep}%
        \enlightnumber{#3}%
      }%
    }%
  }{%
    \IfArgIsEmpty{#3}{}{%
      \makebox[0pt][r]{%
        \enlightnumber[r]{#3}%
        \hspace{\marginparsep}}%
      \parbox[t]{\linewidth}{\raggedright #4}%
    }%
  }%
}

\renewcommand*{\chapterformat}{\thechapter\autodot}
\renewcommand*{\sectionformat}{\thesection\autodot}
\renewcommand*{\subsectionformat}{\thesubsection\autodot}
\renewcommand*{\subsubsectionformat}{\thesubsection\autodot}

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

\begin{document}
\tableofcontents

\chapter{Chapter on even Page}
\blindtext
\section{Section on even page}
\blindtext
\subsection{Subsection on even page}
\blindtext
\subsubsection{Subsubsection on even page}
\blindtext
\paragraph{Paragraph on odd page}
\blindtext
\subparagraph{Subparagraph on odd page}
\Blindtext[4]

\chapter{Chapter on odd Page}
\blindtext
\section{Section on odd page}
\blindtext
\subsection{Subsection on odd page}
\blindtext
\subsubsection{Subsubsection on odd page}
\blindtext

\end{document}

It should be noted that due to the redefinitions shown in both examples, both \raggedchapter and \raggedsection as well as the second argument of \sectionlinesformat, i.e. the indent value of \DeclareSectionCommand, have lost their function.

Also note that these examples cannot be used with the chapterprefix or appendixprefix option. For these two settings \chaperlineswithprefixformat would have to be redefined additionally. However, the requirements for the display would have to be specified first.

By the way, by using \Ifthispageodd it can happen that the formatting is correct only after two LaTeX runs. For more details see the explanation of the instruction in the KOMA-Script-Anleitung.


Related

Issues: #1
Wiki (English): HowTo_Headings
Wiki (English): HowTo_Manual
Wiki (English): HowTo_NumbersAtLeftMargin

MongoDB Logo MongoDB