# Align chapter and section headings to outer edge

I am trying to achieve the following result (made with Photoshop):

I already managed to get the right page formatted correctly, but this does obsiously not work for double sided documents:

\documentclass[a4paper,11pt,twoside,chapterprefix=true]{scrbook}

\usepackage{lipsum}
top=25mm,
left=40mm,
right=35mm
]{geometry}

\makeatletter
\makeatother

\begin{document}
\chapter{Sample}
\section{This Is a First Level Section} \lipsum[1]
\subsection{And Second Level} \lipsum[2] \pagebreak
\section{First Level Again} \lipsum[3]
\subsection{Second Level} \lipsum[4]
\end{document}


How can i align the headings to the outer edge?

• Such a layout may look more symmetrical but is a pain for the reader. Even in twoside layouts text is read from left to right so you shouldn't force the reader eyes to jump to the right to get the section number, to the middle to read the section title and back again to the left to read the text. – Ulrike Fischer Oct 14 '15 at 11:25
• Thanks, but I would still be interested in a solution for this. Any ideas? – sonovice Oct 14 '15 at 12:11
• There is an answer by Markus Kohm on the KOMA-Script website: komascript.de/node/1976 (German) – esdd Oct 14 '15 at 13:16
• @esdd: The first solution will not give the (probably) expected result for section titles with more than one line. – Ulrike Fischer Oct 14 '15 at 13:25
• @esdd: Thank you very much, I am getting close. Unfortunately the second solution renders the numbers on the wrong side of the page. (Can't try the first one, I am restricted to an older version of the koma scripts.) – sonovice Oct 14 '15 at 13:46

Somebody has asked Markus. There are two suggestions on the KOMA-Script website: Überschriften am äußeren Rand ausrichten mit Nummer im Rand (German). Both suggestions needs some runs to get the numbers right.

The first solution needs KOMA-Script version 3.19a or newer. I have changed this code a little so it works now with longer section titles too.

\documentclass[chapterprefix]{scrbook}
%\providecommand*\Ifthispageodd{\ifthispageodd}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{blindtext}% dummy text
\renewcommand*{\raggedsection}{\Ifthispageodd{\raggedleft}{\raggedright}}
\renewcommand*{\sectionlinesformat}[4]{%
\ifthispagewasodd
\parbox[t]{\linewidth}{\leavevmode\raggedsection#4}\makebox[0pt][l]{\hskip\marginparsep #3}%
\else
\makebox[0pt][r]{#3\hskip\marginparsep}#4%
\fi
}
\renewcommand*{\sectionformat}{\thesection\autodot}
\renewcommand*{\subsectionformat}{\thesubsection\autodot}
\renewcommand*{\subsubsectionformat}{\thesubsubsection\autodot}
\begin{document}
\chapter{A Chapter}
\section{A section with a long, long, long title that needs more than one line}
\Blindtext
\section{Another section}
\blindtext
\blinddocument
\end{document}


The second example uses package marginnote but works also with older KOMA-Script versions.

\documentclass[chapterprefix]{scrbook}
%\providecommand*\Ifthispageodd{\ifthispageodd}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{marginnote}
\usepackage{mwe}
\renewcommand*{\raggedsection}{\Ifthispageodd{\raggedleft}{\raggedright}}
\renewcommand*{\sectionformat}{%
\marginnote{%
\usekomafont{disposition}{\usekomafont{section}{%
\thesection\autodot}}}}
\renewcommand*{\subsectionformat}{%
\marginnote{%
\usekomafont{disposition}{\usekomafont{subsection}{%
\thesubsection\autodot}}}}
\renewcommand*{\subsubsectionformat}{%
\marginnote{%
\usekomafont{disposition}{\usekomafont{subsubsection}{%
\thesubsubsection\autodot}}}}
\begin{document}
\chapter{A chapter}
\section{A section with a long, long, long title that needs more than one line}
\Blindtext
\section{Another section}
\blindtext
\blinddocument
\end{document}