# Extending section heading underline into margin

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 Answers

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.

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


• This is perfect. Thank you! Jun 3 '19 at 18:03
• This is a long time later, but is there a way to set this so that unnumbered sections (\section*{}) don't protrude into the margin? I.e. still underlined, but aligned with the left margin (just like the words are here), and with no underline extending into the margin? Apr 21 '20 at 9:17
• So "Lipsum" would still appear in the same place relative to the rest of the text, and still be underlined, but of course without any number, and also the underline just under the text Apr 21 '20 at 9:18
• @LyndonDrake Extra code to deal with this added :-) Apr 21 '20 at 15:08
• Thank you - very much appreciated! Apr 21 '20 at 17:54

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:

• Thank you - also great but the other answer was even closer to what I was after. Jun 3 '19 at 18:04