2

I am trying to find a way to draw a line below the chapter title including the chapter number. The solution proposed here KOMA-Script line above and below chapter title except that in my case the chapter number is in the margin. The solution should work with latexmk and pdflatexmk.

So is there an easy way to extend the line to include the chapter number?

Here is my MWE:

\documentclass{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{blindtext}
\usepackage[default, scale=.90]{opensans} % police Open Sans

%% Chapter number in the margin
\renewcommand*{\chapterformat}{%
  \llap{\thechapter\autodot\enskip}%
}
 
%% Draw a rule of size \textwidth
%% Modification of https://tex.stackexchange.com/questions/273343/koma-script-line-above-and-below-chapter-title
\newcommand\titlerule[1][1pt]{\rule{\textwidth}{#1}}
\renewcommand\chapterlineswithprefixformat[3]{%
  #2\nobreak%
  \Ifstr{#2}{}{}{\kern-\dp\strutbox}%
  \titlerule\par\nobreak%
  #3%
  \par\nobreak\titlerule%
}
\makeatletter
\renewcommand\chapterlinesformat[3]{%
  \@hangfrom{#2}{#3}%
  \par\nobreak\titlerule%
}
\makeatother
\begin{document}
\blinddocument
\end{document}

1 Answer 1

0

You can measure the width of the chapter number and make the rule longer, but sticking to the left.

\newcommand\titlerule[1][1pt]{%
  \settowidth{\dimen0}{\thechapter\enspace}%
  \hspace*{-\dimen0}\rule{\dimexpr\textwidth+\dimen0}{#1}%
}

Complete example (two chapter headers in the same page just to make a smaller picture).

\documentclass{scrbook}
%\usepackage[utf8]{inputenc}% no longer necessary
\usepackage[T1]{fontenc}
\usepackage[default, scale=.90]{opensans} % police Open Sans

%% Chapter number in the margin
\renewcommand*{\chapterformat}{%
  \llap{\thechapter\autodot\enskip}%
}
 
%% Draw a rule of size \textwidth
%% Modification of https://tex.stackexchange.com/questions/273343/koma-script-line-above-and-below-chapter-title
\newcommand\titlerule[1][1pt]{%
  \settowidth{\dimen0}{\thechapter\enspace}%
  \hspace*{-\dimen0}\rule{\dimexpr\textwidth+\dimen0}{#1}%
}
\renewcommand\chapterlineswithprefixformat[3]{%
  #2\nobreak%
  \Ifstr{#2}{}{}{\kern-\dp\strutbox}%
  \titlerule\par\nobreak%
  #3%
  \par\nobreak\titlerule%
}
\makeatletter
\renewcommand\chapterlinesformat[3]{%
  \@hangfrom{#2}{#3}%
  \par\nobreak\titlerule%
}
\makeatother

\begin{document}

\chapter{Test}

\begingroup\let\cleardoublepage\relax

\setcounter{chapter}{10}

\chapter{Test}

\endgroup

\end{document}

enter image description here

1
  • Thank you. Exactly what I was looking for. As I expected Latex wizards are able to solve the problem I was struggling with for hours.
    – LatexNoob
    Nov 25, 2022 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .