# Giving headlines a background color spanning across the entire typearea

I posted this at stackoverflow and got redirected here. This does indeed look like a better place to ask.

I would like to decorate my headlines (section, subsection, etc.) by adding a background color. I would like this box filled with a single solid color to span across the entire typearea.

This picture is a screenshot of an OpenOffice document where the headline has the formating, layout and color I want.

## Requirements on a solution

1. The resulting style is supposed to be used by others, meaning an equivalent latex output to the picture above shall be created by this code:

Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text

\subsubsection{This is a headline}

Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text

2. If the section is supposed to be unnumbered one should be using \subsubsection*.

3. LaTeX still needs to be in charge of the whitespace between paragraphs and the blue box.

## What I have tried so far

As far as I can google the headline together with the section numbering are a simple paragraph. Latex adds some skips before and after and squeezes in a few commands that modify the font(s).

My ideas revolve around locking this paragraph in a \colorbox. The colorbox macro works like this: \colorbox{color}{the content around which to draw a box filled with the specified color}. My problem is I can't get a grasp on this paragraph.

### The easy 80%

My work is based on koma-script and by redefining \othersectionlevelsformat and \(sub)*section. I can draw two boxes, one surrounding the section numbering and another surrounding the section title. However this has 3 open issues:

1. the upper edges of the boxes are unaligned and the box with the title is slightly larger than the line with the numbering
2. The box around the title does not strech to the right end of the typearea
3. the box around the numbering doesn't grow if the title spans multiple lines (no surprise here)

### The hard 20%

I tried to make latex put the section numbering into a \savebox I could access later and put as part of the title. That would eliminate the need for two boxes and solve all three of my problems at once, but that doesn't work. I only get to modify the title before the actual numbering is done. So while I can put the numbering into a savebox, I can't do anything with it until it's too late.

I tried putting the \section ... statement into a colorbox, but it doesn't like that and the document fails to compile with ! LaTeX Error: Something's wrong--perhaps a missing \item. The same holds for saveboxes. I guess colorbox uses those.

I tried putting the \section ... statement in a minipage and put a box around that. This has two issues. 1st: latex doesn't put any empty space above a section. 2nd: not only the section numbering and title have the background but also the empty space between section title and first paragraph.

So how can one hack latex to do a section title formating as shown in the screenshot above?

-
Thanks for the quick replys, but using titlesec breaks out of koma-script and the tikz approach can't adapt the rectangle to the actual height (esp. if multiline) of the headline. However, I think the titlesec approach can be modified to honor koma-settings and fix my problems. Thanks again! –  user1129682 Jan 5 '12 at 11:26
Because you’re using KOMA script – Markus Kohm himself provided a solution almost exactly 6 years ago: Farbig unterlegte Überschriften. He stated, that this works only for one lined section titles! –  Speravir Jan 6 '12 at 0:06
add comment

## 4 Answers

Here my second try. It is using the same Idea as Sharpie, but doesn't need tikz.

\documentclass{scrartcl}

\usepackage{xcolor}
\usepackage{lipsum}

\setkomafont{section}{\mysection}
\newcommand{\mysection}[1]{%
\Large\sf\bf%
\setlength{\fboxsep}{0cm}%already boxed
\colorbox{orange!80}{%
\begin{minipage}{\linewidth}%
\vspace*{2pt}%Space before
#1
\vspace*{2pt}%Space after
\end{minipage}%
}}

\begin{document}

\section{Example section}
\lipsum[1]

\section*{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
\lipsum[1]
\end{document}


result is: feel free to adopt for subsection etc ;)

-
This is what I was looking for! To round it off I use \addtokomafont instead of \setkomafont. That way koma-script's settings remain in tact, I don't need the line "\Large\sf\bf%" and I only need to add lines \addtokomafont{subsection –  user1129682 Jan 9 '12 at 8:41
add comment

This is for a section, but you can extend it easily for subsections with a similar scheme:

\documentclass[a4paper]{scrbook}
\usepackage{xcolor,lipsum}
\usepackage{titlesec}

\titleformat{name=\section}[block]
{\sffamily\large}
{}
{0pt}
{\colorsection}
\titlespacing*{\section}{0pt}{\baselineskip}{\baselineskip}

\newcommand{\colorsection}[1]{%
\colorbox{blue!20}{\parbox{\dimexpr\textwidth-2\fboxsep}{\thesection\ #1}}}

\begin{document}

\lipsum[1]
\section{This is the title}
\lipsum[2]
\end{document}


If you want white on colored text, just modify the \colorsection command, for example as

\newcommand{\colorsection}[1]{%
\colorbox{blue}{\parbox{\dimexpr\textwidth-2\fboxsep}{\color{white}\thesection\ #1}}}


-
add comment

I have a different approach. I used tikz:

\documentclass[12pt]{scrartcl}
\usepackage{tikz}
\makeatletter
\setkomafont{section}{\color{white}%
\bfseries\Large
\begin{tikzpicture}[overlay]
\draw[fill=blue] (0,-2pt) rectangle
(\linewidth,16.4pt);
\end{tikzpicture}}

\begin{document}
\section{test}
\subsubsection{This is a headline}
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text

\section*{test 2}
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text Some Text Some Text
Some Text Some Text Some Text Some Text Some Text
\end{document}


The result is this (you can change the color blue to match your color):

of cause you can easily adopt this for subsection etc...

-
add comment

Here is another method that exploits the behavior of passing a macro that takes one argument to \setkomafont. The actual styling is done using TikZ nodes.

\documentclass{scrartcl}

\usepackage{tikz}
\usepackage{lipsum}

\tikzset{
% Styling of header text is done using key/value options for TikZ nodes. See
% section 16.4 of the PGF manual for a complete list of options that affect
% text.
headings/base/.style = {
% Zap node seperation, set text width and alignment.
outer sep = 0pt,
% Trim off 2/3rd of an em to compensate for the inner xsep which spaces the
% text nicely away from the left side, but causes the node to hang into the
% right margin.
text width = {\textwidth - 0.6666em},
align = left,
text = white,
},
headings/section/.style = {
headings/base,
fill = blue!50,
font = \sffamily\Large
},
headings/subsection/.style = {
headings/base,
fill = blue!25,
font = \sffamily\large
}
}

\newcommand{\colorboxedsec}[2]{%
\tikz{\node[headings/#1]{#2};}}

\setkomafont{section}{\colorboxedsec{section}}
\setkomafont{subsection}{\colorboxedsec{subsection}}

\begin{document}

\section{Lorem Ipsum}
\lipsum[1]

\subsection{History of Lorem Ipsum}
Foo bar baz.

\section*{A section header full of \lipsum[1]}

\end{document}


The results:

-
great solution. How did you know that Koma will pass the command? I haven't read this in documentation. I just played with this function and build a non tikz multi line version. –  someonr Jan 6 '12 at 0:38
It was a guess. If KOMA executes something like \sectionfont\sectiontext but \sectionfont has been defined to take one argument, the result will behave like \sectionfont{\sectiontext}. –  Sharpie Jan 6 '12 at 0:39
thx. Only problem is that the koma script authors may change it any time, because this is not an official feature. Anyway I like this solution. –  someonr Jan 6 '12 at 1:33
add comment