2

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.

2
  • 1
    Your code needs \usepackage{xcolor}. You can replace \usepackage[colorlinks=true]{hyperref} by it.
    – Sigur
    Commented Jul 23, 2015 at 10:21
  • @Andrew, I know. You are right. But the OP said he doesn't know why the code does not compile withou hyperref. I just said that in fact what he needs is xcolor (also loaded by hyperref as you said).
    – Sigur
    Commented Jul 23, 2015 at 11:01

2 Answers 2

7

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}
2
  • 1
    AFAIK there will be a better way to underline the section headings with the next KOMA-Script version (3.19), see http://www.komascript.de/releasesvn (German).
    – esdd
    Commented Jul 23, 2015 at 12:15
  • This also works for non-headings, great! In the case of using it not for headings but text one should only rename to something like \reduline. Commented Sep 22, 2016 at 12:13
0

Thanks for all contributions. for a while I’m looking for the simplest (without using koma ) way to underline section and subsection without affecting chapter heading, I find several talks about that but I got always troubles and affecting chapter heading. Finally it works for me , hope this helps others ….

\usepackage{titlesec}
\usepackage{xcolor}
\usepackage[normalem]{ulem}

\titleformat{\chapter}[display]
{\normalfont\huge\bfseries\centering\color{blue}}{\sc{\chaptertitlename\ \thechapter}}{20pt}{\Huge}

\titlespacing*{\chapter}
    {15pt}
    {15pt}
    {50pt}
\titleformat{\section} {\normalfont\Large\bfseries\raggedright}{\thesection}{1em}{\uline}

\titleformat{\subsection}{\normalfont\large\bfseries\raggedright}{\thesubsection}{1em}{\uline}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .