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?

1 Answers1

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}

esdd
  • 85,675
  • 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. – Lyndon Drake Apr 22 '21 at 08: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 '21 at 09:35
  • Ah, ok that makes it clearer. Thank you! – Lyndon Drake Apr 22 '21 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 – Lyndon Drake Apr 22 '21 at 17:40
  • I get the following error in my thesis file: – Lyndon Drake Apr 22 '21 at 17:47
  • /Users/lyndon/repo/writing/input-thesis/thesis_jeremiah_32_as_economic_metaphor.tex:194: Extra }, or forgotten \endgroup. \UL@stop ...alty \ifnum \lastkern =\thr@@ \egroup \egroup \ifdim \wd \UL@box... l.194 \tableofcontents – Lyndon Drake Apr 22 '21 at 17:47
  • Any thoughts? I'm puzzled by why the identical section formatting commands don't trigger it – Lyndon Drake Apr 22 '21 at 17:48
  • Sorry, but I can not reproduce the issue if I add \tableofcontents to my code. Maybe you have to ask a new question including a MWE showing your issue. Do you load another package regarding ToC, eg. tocloft? Or there are other changes for the ToC in your code? – esdd Apr 22 '21 at 18:26
  • Yep in the MWE it works fine, and it must be an interacting package. I'll do the tedious (!) job of getting the thesis down to an MWE in due course and post a new question. Thanks for your help so far! – Lyndon Drake Apr 22 '21 at 18:57
  • OK, so the problem is if I have a bibliography file (I'm not even doing citations or generating the bibliography). Just the line, \addbibresource{~/repo/workflow/zot-export.bib} is enough to break the MWE – Lyndon Drake Apr 22 '21 at 22:45
  • 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 '21 at 09:59
  • Must be something in my .bib file then. I'll investigate – Lyndon Drake Apr 23 '21 at 23:24