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}

\makeboxaround#2in the lines format you could see some bad effects otherwise. – TeXnician Aug 14 '18 at 17:06\makeboxsituation. – Ruixi Zhang Aug 14 '18 at 17:20\rlapapplies to the whole\fontsize...\thechapter}now. 2) The first\makeboxin your\renewcommand\chapterlinesformatis 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\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\renewcommand\raggedchapter{\raggedleft}, and 2) change\raggedchapter#3to\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\parbox[t]looks better! – user855443 Aug 14 '18 at 21:35