1

In another question (How to place the chapter number behind the chapter title in KOMA script) code was shown to have the chapter numer overprinting the chapter heading. I would like to have the chapter number (large) in the right (outside) margin.

I copy here the code from the previous solution which I do not fully understand but found working. What must be changed to have the number in the margin? Replacing \textwidth by \pagewidth did not work. I would appreciate some hints how to read the code.

\documentclass{scrbook}
\usepackage{lmodern}
\usepackage{xcolor}

\renewcommand\raggedchapter{\raggedleft}
\renewcommand*{\chapterformat}{%
  \fontsize{60}{68}\selectfont\textcolor{lightgray}{\thechapter}}

\makeatletter
\renewcommand\chapterlinesformat[3]{%
  \ifstr{#1}{chapter}
    {\hfill\makebox[0pt][r]{#2}\makebox[0pt][r]{\parbox[b]{\textwidth}{\raggedchapter#3}}}%
    {\@hangfrom{#2}{#3}}% original definition for other commands with style=chapter
}
\makeatother

\usepackage{blindtext}
\begin{document}
\tableofcontents
\blinddocument
\chapter{Text elements}
In this chapter, some textual elements are shown, like figures,
tables, lists, equations, etc. Also, bananas.
\end{document}
user855443
  • 1,120

1 Answers1

4

Like this?

\documentclass{scrbook}
\usepackage{lmodern}
\usepackage{xcolor}

\renewcommand\raggedchapter{\raggedleft}
\renewcommand*{\chapterformat}{%
  \rlap{\fontsize{60}{68}\selectfont\textcolor{lightgray}{\thechapter}}}

\makeatletter
\renewcommand\chapterlinesformat[3]{%
  \ifstr{#1}{chapter}
    {\hfill#2\makebox[0pt][r]{\parbox[b]{\textwidth}{\raggedchapter#3}}}%
    {\@hangfrom{#2}{#3}}% original definition for other commands with style=chapter
}
\makeatother

\usepackage{blindtext}
\begin{document}
\tableofcontents
\blinddocument
\chapter{Text elements}
In this chapter, some textual elements are shown, like figures,
tables, lists, equations, etc. Also, bananas.
\end{document}

number in outer margin


Some explanations:

  • The chapter number is stored in \thechapter, so we know we should modify the code containing \thechapter.
  • The \rlap code creates a box of width zero, and then puts its contents hanging out to the right. You can play around with \llap too. With the mathtools package, there is \clap, and there are math mode equivalences: \mathrlap, \mathllap and \mathclap.
  • I would recommend adding some space between chapter title and chapter number, so you may write \rlap{ \fontsize... (note the space added). This is of course my personal preference.
Ruixi Zhang
  • 9,553
  • 2
    Two things: (1) The chapterformat is okay, but should be grouped (font selection), because when you do (2) remove the useless \makebox around #2 in the lines format you could see some bad effects otherwise. – TeXnician Aug 14 '18 at 17:06
  • 1
    @TeXnician Ah yes, you are right! I didn’t even notice the \makebox situation. – Ruixi Zhang Aug 14 '18 at 17:20
  • I do not fully understand these two comments - could one of you fix the code to reflect the proposed improvement? Thank you! – user855443 Aug 14 '18 at 19:04
  • It works as expected - great! Another "stupid" question: how to force the chapter title to be left justified? – user855443 Aug 14 '18 at 19:06
  • 1
    @user855443 My current MWE is improved: 1) \rlap applies to the whole \fontsize...\thechapter} now. 2) The first \makebox in your \renewcommand\chapterlinesformat is unnecessary so it’s removed. Secondly, there’re no “stupid” questions. However, from your new question I got the impression that you simply copy-pasted the code from the other post without understanding what it does. This is dangerous! Perhaps you should first read Koma-Script documentation pp. 471–472 to learn about \chapterlinesformat. – Ruixi Zhang Aug 14 '18 at 20:24
  • @user855443 Also, to my knowledge, there’s no such a thing called “left justified”. It’s either “flush left” (aka “ragged right”) or “justified”. See here. I have absolutely no idea what \hfill#2\makebox[... is trying to accomplish. So, again, you should read the documentation first to learn about customizing your chapter headings. – Ruixi Zhang Aug 14 '18 at 20:39
  • thank you for the pointer - the Koma-script doc is quite large and it is not always easy to find and understand the information given. I have learned a few things now, but do not fully understand the command yet. – user855443 Aug 14 '18 at 21:15
  • 1
    @user855443 I think this might be what you want: From my MWE,1) remove \renewcommand\raggedchapter{\raggedleft}, and 2) change \raggedchapter#3 to \raggedright#3. You may also want to change the \parbox[b] (bottom aligned) to \parbox[t] (top aligned) or to \parbox[c] (center aligned). I personally find \parbox[t] looks best. – Ruixi Zhang Aug 14 '18 at 21:20
  • thank you - that helps to understand me a few steps more. Indeed, \parbox[t] looks better! – user855443 Aug 14 '18 at 21:35