[Tex/LaTex] Color Underline Section Headings

colorkoma-scriptsectioning

I would like to underline section headings using different colors (e.g. red for section, yellow for subsection). So far I have only achieved to change the text color of the headings (see code), but I want to underline the headings and then change the line color while keeping the text color black.

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[colorlinks=true]{hyperref}

\addtokomafont{section}{\color{red}}
\addtokomafont{subsection}{\color{yellow}}

\begin{document}
\section{Section}
\subsection{Subsection}
\end{document}

PS: I don't know why the code example needs \usepackage[colorlinks=true]{hyperref}, but it doesn't compile without.

Best Answer

Update

Since KOMA-Script version 3.19 command \sectionlinesformat can be redefined to get the desired result using package ulem:

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage[normalem]{ulem}
\usepackage{blindtext}

\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
  \ifstr{#1}{section}
    {\colorlet{ulinecolor}{red}}
    {\ifstr{#1}{subsection}{\colorlet{ulinecolor}{green}}
      {\colorlet{ulinecolor}{.}}}%
  \@hangfrom{\hskip #2\expandafter\sectionuline\expandafter{#3}}{\expandafter\sectionuline\expandafter{#4}}%
}
\makeatother

\newcommand\sectionuline{%
  \bgroup\markoverwith{\textcolor{ulinecolor}{\rule[-0.75ex]{2pt}{0.4pt}}}%
  \ULon%
}
\colorlet{ulinecolor}{black}

\addtokomafont{section}{\Huge}

\begin{document}
\section{Really long section title that needs more than one line}
\subsection{Subsection}
\subsubsection{Subsubsection}
\end{document}

results in

enter image description here


Original answer

If you want to color only the lines you can load package ulem and define new commands for colored lines as suggested in its manual:

\newcommand\sectionuline{%
  \bgroup\markoverwith{\textcolor{red}{\rule[-0.5ex]{2pt}{0.4pt}}}%
  \ULon%
}
\newcommand\subsectionuline{%
  \bgroup\markoverwith{\textcolor{blue}{\rule[-0.5ex]{2pt}{0.4pt}}}%
  \ULon%
}

Then you can add these commands to the font settings of section and subsection by

\addtokomafont{section}{\sectionuline}
\addtokomafont{subsection}{\subsectionuline}

Warning: \...uline must be the last added one to the font settings because of they take an argument. Additionally this \...uline commands are formating commands so this is something like a misuse of the font settings.

enter image description here

Code:

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{ulem}

\newcommand\sectionuline{%
  \bgroup\markoverwith{\textcolor{red}{\rule[-0.5ex]{2pt}{0.4pt}}}%
  \ULon%
}
\newcommand\subsectionuline{%
  \bgroup\markoverwith{\textcolor{blue}{\rule[-0.5ex]{2pt}{0.4pt}}}%
  \ULon%
}

\addtokomafont{section}{\sectionuline}
\addtokomafont{subsection}{\subsectionuline}

\begin{document}
\section{Section}
\subsection{Subsection}
\end{document}