# Section numbers in margins in double sided document

I'm trying to outline section titles differently in a double sided document. In particular, I'd like to do so using KOMA script. When on an odd (right) page, the section title should be outlined right and the number should be displayed in the margin to the right; when on the left, the text should be outlined left, with the number to the left of it.

I've tried adapting this question to my needs, but haven't been successful. Case in point: when not editing that answer and simply adding the option twoside, I get:

(a). This section's number isn't truly in the margin.

(b). This section is outlined wrong, and its number isn't in the corresponding margin.

I'm trying to adapt this and still use KOMA script. My attempt was the following:

\documentclass[twoside]{scrartcl}[2015/10/03]
\usepackage{showframe}

% my edit:
\renewcommand\sectionlinesformat[4]{%
\ifthispageodd{%
\raggedleft\makebox[0pt][r]{#4}#3}{%
\raggedright\makebox[0pt][r]{#3}{#4}}
}

\clearpairofpagestyles
\usepackage{blindtext}
\begin{document}
\Blinddocument
\end{document}


However, problem (a) is still very much there (unchanged), while problem (b) has now become:

\documentclass[twoside]{scrartcl}
%\providecommand*\Ifthispageodd{\ifthispageodd}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{showframe}

\renewcommand\sectionlinesformat[4]{%
\Ifthispageodd
{\raggedleft#4\makebox[0pt][l]{\enskip#3}}% <- changed
{\raggedright\makebox[0pt][r]{#3}#4}% <- comment space at line end
}

\clearpairofpagestyles
\usepackage{blindtext}
\begin{document}
\Blinddocument
\end{document}


If the section numbers should be in marginpar (area for the margin notes):

\documentclass[twoside]{scrartcl}
%\providecommand*\Ifthispageodd{\ifthispageodd}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{showframe}

\renewcommand\sectionformat{\thesection\autodot}
\renewcommand\subsectionformat{\thesubsection\autodot}
\renewcommand\subsubsectionformat{\thesubsubsection\autodot}

\renewcommand\sectionlinesformat[4]{%
\Ifthispageodd
{\raggedleft#4\makebox[0pt][l]{\hspace{\marginparsep}#3}}
{\raggedright\makebox[0pt][r]{#3\hspace{\marginparsep}}#4}%
}

\clearpairofpagestyles
\usepackage{blindtext}
\begin{document}
\Blinddocument
\end{document}


• Thank you! Though still the number isn’t fully in the margin. – SvanN Jan 18 '19 at 19:36
• Do you mean the marginpar? Then see my updated answer. – esdd Jan 18 '19 at 21:18
• That is precisely what I was looking for! Thank you. – SvanN Jan 19 '19 at 8:51