2

I'm using Koma and have got my section heading numbers set in the margin of the page. I have also got a coloured underline for the section headings. I want, if possible, the underline for the section headings to extend to include the number, i.e. to extend into the margin enough to start where the section number itself starts.

As a bonus, at the moment my section numbers appear at varying distances into the margin - they are in the margin just enough to fit that number. I would prefer to set them at a constant amount left of the margin, so that the left-hand side of each number lines up.

MWE:

\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

% KOMA and general setup
\renewcommand*{\sectionformat}{%
  \llap{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
  \llap{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
  \llap{\thesubsubsection\autodot\enskip}%
}

% underline section headings
\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
    \@hangfrom{\hskip #2\expandafter\headuline\expandafter{#3}}%
    {#4}%
}
\makeatother
\newcommand\headuline{%
    \bgroup\markoverwith{\textcolor{MyThemeColour}{\rule[-0.75ex]{1pt}{1pt}}}\ULon% 2pt 2pt
}
\AtBeginDocument{\renewcommand\Sectionformat[2]{\headuline{#1}}}

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

\begin{document}
\section{Lipsum}
\blinddocument
\end{document}

2 Answers2

3

The following draws the line under the number and heading in one go. We put the numbers in a box of fixed width, governed by a new length \mysecnumhang. Then we define the section line format to (1) back up this width, (2) print the box with the section number and then the title, and (3) underline everything.

Sample output

\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}%
}

\renewcommand{\sectionlinesformat}[4]{%
    \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}
\section{Lipsum}
\blinddocument
\end{document}

For the extra request to deal with \section* reasonably, one can use the following:

\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}

Example with section*

Andrew Swann
  • 95,762
1

It's not really a clean solution, but you can draw zero-width rules under the left-overlapped section numbers that blend into the actual underlines. Note that the box of the rule is zero-width (using \makebox[0pt]) while the rule itself has the width specified in the \rule command - LaTeX just ignores the width for calculating the position of the following content.

MWE:

\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

% KOMA and general setup
\renewcommand*{\sectionformat}{%
  \makebox[0pt]{\color{MyThemeColour}\rule[-0.75ex]{33pt}{1pt}}\llap{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
  \makebox[0pt]{\color{MyThemeColour}\rule[-0.75ex]{48pt}{1pt}}\llap{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
  \makebox[0pt]{\color{MyThemeColour}\rule[-0.75ex]{58pt}{1pt}}\llap{\thesubsubsection\autodot\enskip}%
}

% underline section headings
\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
    \@hangfrom{\hskip #2\expandafter\headuline\expandafter{#3}}%
    {#4}%
}
\makeatother
\newcommand\headuline{%
    \bgroup\markoverwith{\textcolor{MyThemeColour}{\rule[-0.75ex]{1pt}{1pt}}}\ULon% 2pt 2pt
}
\AtBeginDocument{\renewcommand\Sectionformat[2]{\headuline{#1}}}

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

\begin{document}
\section{Lipsum}
\blinddocument
\end{document}

Result:

enter image description here

Marijn
  • 37,699