[Tex/LaTex] Chapter and section number in margin and align chapter prefix with text in scrbook

chaptersscrbooksectioning

I try to customize my chapter headings, without using additional packages like titlesec, as I'd like to avoid incompatibilities with all the other stuff around.

I almost have what I want:

\documentclass[
  fontsize=12pt,                                            
  BCOR=15mm,                                              
  DIV=15, 
  twoside=true,
  open=right,
  chapterprefix = true,
  headings=twolinechapter,
  headings=big,                                     
]{scrbook} 


\usepackage{fontspec}
\setmainfont[Numbers = OldStyle,Ligatures = TeX,SmallCapsFeatures = {Renderer=Basic}]{Minion Pro}

\usepackage{anyfontsize}
\setkomafont{chapter}{\normalfont\Huge}
\renewcommand*{\chapterheadstartvskip}{\vspace*{5\baselineskip}}
\renewcommand*{\chapterheadendvskip}{\vspace*{2\baselineskip}}
\renewcommand*{\chapterformat}{%
                {\fontsize{20}{30}\scshape\chapappifchapterprefix{\nobreakspace}}
                \fontsize{120}{30}\selectfont\thechapter\autodot\enskip}
\renewcommand*{\raggedchapter}{\raggedleft}

\setkomafont{section}{\Large\rmfamily}

\usepackage{blindtext}

\begin{document}

\chapter{Hello World!}
\section{Section 1}
\blindmathpaper

\end{document}

enter image description here


There is just on detail missing:

How can I place chapter and section numbers in the margin and align title and prefix with the text?

So basically I try to achieve:

enter image description here

Best Answer

Add the following definition for sections and alike

\renewcommand*\othersectionlevelsformat[3]{%
  \llap{#3\autodot\enskip}%
}

and change the definition of \chapterformat to

\renewcommand*{\chapterformat}{%
  {\fontsize{20}{30}\scshape\chapappifchapterprefix{}}%
  \fontsize{120}{30}\selectfont\rlap{\thechapter\autodot}%
}

MWE:

\documentclass[
  fontsize=12pt,
  BCOR=15mm,
  DIV=15,
  twoside=true,
  open=right,
  chapterprefix = true,
  headings=twolinechapter,
  headings=big,
]{scrbook}


\usepackage{fontspec}
\setmainfont[Numbers = OldStyle,Ligatures = TeX,SmallCapsFeatures = {Renderer=Basic}]{Minion Pro}

\usepackage{anyfontsize}
\setkomafont{chapter}{\normalfont\Huge}
\renewcommand*{\chapterheadstartvskip}{\vspace*{5\baselineskip}}
\renewcommand*{\chapterheadendvskip}{\vspace*{2\baselineskip}}
\renewcommand*{\chapterformat}{%
  {\fontsize{20}{30}\scshape\chapappifchapterprefix{}}%
  \fontsize{120}{30}\selectfont\rlap{\thechapter\autodot}%
}
\renewcommand*{\raggedchapter}{\raggedleft}

\renewcommand*\othersectionlevelsformat[3]{%
  \llap{#3\autodot\enskip}%
}

\setkomafont{section}{\Large\rmfamily}

\usepackage{blindtext}

\begin{document}

\chapter{Hello World!}
\section{Section 1}
\blindmathpaper

\end{document} 

Output:

enter image description here


UPDATE (KOMA-Script v3.17)

Starting with KOMA-Script v3.17, \othersectionlevelsformat doesn't work as before, so, instead of using

\renewcommand*\othersectionlevelsformat[3]{%
  \llap{#3\autodot\enskip}%
}

you will have to use

\renewcommand*{\sectionformat}{%
  \llap{\thesection\autodot\enskip}%
}

instead. If you also want that behavior for subsections, use

\renewcommand*{\subsectionformat}{%
  \llap{\thesubsection\autodot\enskip}%
}

and so on.