1

In my earlier question, I got a very helpful response on extending section headings in a KomaScript article document into the margins: Extending section heading underline into margin

The MWE answer was:

\PassOptionsToPackage{svgnames,table,dvipsnames}{xcolor}
\documentclass[a4paper,10pt,oneside,DIV=9]{scrartcl}

% Packages
\usepackage{scrlayer-scrpage}
\usepackage{xcolor} % showframe loads this
\usepackage[normalem]{ulem}
\usepackage{hyperref}

% demo only
\usepackage{lipsum}
\usepackage{mwe}

% theme colours
\definecolor{MyThemeColour}{cmyk}{0.10,0.10,1.00,0} % personal yellow

\newlength{\mysecnumhang}
\setlength{\mysecnumhang}{30pt}
% KOMA and general setup
\renewcommand*{\sectionformat}{%
  \makebox[\mysecnumhang][l]{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsubsection\autodot\enskip}%
}

\newbox\tmpseclinenobox
\renewcommand{\sectionlinesformat}[4]{%
  \sbox\tmpseclinenobox{#3}%
  \ifdim\wd\tmpseclinenobox>0pt\hspace{-\mysecnumhang}\fi
  \expandafter\headuline{#3#4}%
}

\newcommand\headuline{%
    \bgroup\markoverwith{\textcolor{MyThemeColour}{\rule[-0.75ex]{1pt}{1pt}}}\ULon% 2pt 2pt
    }

% recalc the text block after all the font loading
\KOMAoptions{DIV=last}

\begin{document}
\section*{Lipsum}
Some text.
\section{Lipsum}
\blinddocument
\end{document}

If I change the class to scrbook and add some `\chapter{}\s to it, I can't seem to replicate the effect with the chapter headings.

Would anyone feel able to update the MWE to deal with chapters?

3
  • @Andrew Swann provided the original answer I've used as the MWE above… Apr 21 at 2:00
  • Adding an illustration of why the first answer isn't quite right… Apr 22 at 8:36
  • And removed with the comment below explaining further Apr 22 at 17:38
1

Redefine \chapterformat in the same way:

\renewcommand*{\chapterformat}{%
  \makebox[\mysecnumhang][l]{\thechapter\autodot\enskip}%
}

and redefine \chapterlinesformat:

\renewcommand{\chapterlinesformat}[3]{%
  \sbox\tmpseclinenobox{#2}%
  \ifdim\wd\tmpseclinenobox>0pt\hspace{-\mysecnumhang}\fi
  \expandafter\headuline{#2#3}%
}

or

\renewcommand{\chapterlinesformat}[3]{%
  \Ifstr{#2}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#2#3}%
}

Example:

\PassOptionsToPackage{svgnames,table,dvipsnames}{xcolor}
\documentclass[a4paper,10pt,oneside,DIV=9]{scrbook}

% demo only
\usepackage{mwe}

% Packages
\usepackage{scrlayer-scrpage}
\usepackage{xcolor} % showframe loads this
\usepackage[normalem]{ulem}
\usepackage{hyperref}

% theme colours
\definecolor{MyThemeColour}{cmyk}{0.10,0.10,1.00,0} % personal yellow

\newlength{\mysecnumhang}
\setlength{\mysecnumhang}{30pt}
% KOMA and general setup
\renewcommand*{\chapterformat}{%
  \makebox[\mysecnumhang][l]{\thechapter\autodot\enskip}%
}
\renewcommand*{\sectionformat}{%
  \makebox[\mysecnumhang][l]{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsubsection\autodot\enskip}%
}

\renewcommand{\chapterlinesformat}[3]{%
  \Ifstr{#2}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#2#3}%
}
\renewcommand{\sectionlinesformat}[4]{%
  \Ifstr{#3}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#3#4}%
}

\newcommand\headuline{%
  \bgroup\markoverwith{\textcolor{MyThemeColour}{\rule[-0.75ex]{1pt}{1pt}}}\ULon% 2pt 2pt
}

% recalc the text block after all the font loading
\KOMAoptions{DIV=last}

\begin{document}
\addchap{Foo}
\addsec*{Lipsum}
Some text.
\section{Lipsum}
\blinddocument
\end{document}

Additional remark: The solution is limited to one line headings because of the underline.


Regarding a comment below:

If you visualize the bounding box of the letters in the headings (using eg. https://tex.stackexchange.com/a/57860), the alignment of the heading texts will be visible:

enter image description here

Code for the picture:

\PassOptionsToPackage{svgnames,table,dvipsnames}{xcolor}
\documentclass[a4paper,10pt,oneside,DIV=9]{scrbook}

% demo only
\usepackage{mwe}

% Packages
\usepackage{scrlayer-scrpage}
\usepackage{xcolor} % showframe loads this
\usepackage[normalem]{ulem}
%\usepackage{hyperref}

% theme colours
\definecolor{MyThemeColour}{cmyk}{0.10,0.10,1.00,0} % personal yellow

\newlength{\mysecnumhang}
\setlength{\mysecnumhang}{30pt}
% KOMA and general setup
\renewcommand*{\chapterformat}{%
  \makebox[\mysecnumhang][l]{\thechapter\autodot\enskip}%
}
\renewcommand*{\sectionformat}{%
  \makebox[\mysecnumhang][l]{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsubsection\autodot\enskip}%
}

\renewcommand{\chapterlinesformat}[3]{%
  \Ifstr{#2}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#2#3}%
}
\renewcommand{\sectionlinesformat}[4]{%
  \Ifstr{#3}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#3#4}%
}

\newcommand\headuline{%
  \bgroup\markoverwith{\textcolor{MyThemeColour}{\rule[-0.75ex]{1pt}{1pt}}}\ULon% 2pt 2pt
}


% recalc the text block after all the font loading
\KOMAoptions{DIV=last}

% show bounding box for each letter (https://tex.stackexchange.com/a/57860)
\makeatletter
\def\showboxes#1{%
  \begingroup\color{red}\fboxrule=.1pt \fboxsep=-\fboxrule
  \@showboxes#1\@showboxes\@empty
  \endgroup}
\def\@showboxes#1#2{%
  \ifx#2\@showboxes
    \fbox{\color{gray}#1}\expandafter\@gobble
  \else
    \setbox0=\hbox{#1\kern0pt#2}\setbox2=\hbox{#1#2}%
    \dimen0=\wd0 \advance\dimen0 -\wd2 % \dimen0 contains the kern between the two chars
    \fbox{\color{gray}#1}\kern-\dimen0
    \expandafter\@showboxes
  \fi#2}

\begin{document}
\chapter{\showboxes{Lipsum}}
\section{\showboxes{Lipsum}}
\end{document}
13
  • I can't post an image in a comment — this is v close to working but doesn't quite align text correctly. I'll edit the original question to show what I mean. Apr 22 at 8:37
  • The text is aligned. But if you the font is enlarged, the bounding boxes of the letters are enlarged too. See my updated answer.
    – esdd
    Apr 22 at 9:35
  • Ah, ok that makes it clearer. Thank you! Apr 22 at 17:37
  • Frustratingly, this solution doesn't work in my larger thesis document. It seems to break the \tableofcontents command somehow. It must be some other interaction with packages loaded for the thesis, I guess Apr 22 at 17:40
  • 1
    Sorry, but I can not reproduce the issue, if I load package biblatex and add \addbibresource{biblatex-examples.bib} (I do not have your bib file) to the preamble. Even if I use \nocite{*} and \printbibliography with or without option heading=totoc, I do not get any error with my code.
    – esdd
    Apr 23 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.