[Tex/LaTex] Giving headlines a background color spanning across the entire typearea

backgroundscolorkoma-scriptsectioningtitlesec

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.

Image showing example

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?

Best Answer

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: result feel free to adopt for subsection etc ;)