Menu

HowTo_PageHeadBackgroundColor

How to set a background color for the page header

The KOMA-Script user guide scrguide-en.pdf shows a header with a colored background. The question of how to achieve this for any document is therefore almost obvious.

In KOMA-Script, the scrlayer and scrlayer-scrpage packages are responsible for header and footer definitions that go beyond the possibilities of the KOMA-Script classes. The scrlayer package provides the basis with the possibility of layer definition and the definition of page styles, which are made up of the application of these layers. The scrlayer-scrpage package is an usage of these basics, which offers a seamless and convenient integration of the layer page styles with the KOMA-Script classes as well as the standard classes and many other classes based on these two families.

To color the background of the page header, simply define a new layer:

\DeclareLayer[
  head,
  background,
  contents={\color{DeepSkyBlue3}\rule[-\dp\strutbox]{\layerwidth}{\layerheight}}
]{head.background}

The head attribute defines the position and size of the layer so that it exactly covers the page header. You could also define the position and size via the area feature or individually via voffset, hoffset, height and width. head is a welcome simplification here. Further such simplifications for other areas of the pages are documented in the user guides.

The background attribute indicates that this is a background layer, i.e. a layer that is to be output before the actual page text. scrlayer-scrpage also outputs the content of the page header itself, i.e. usually the column title with the repetition of chapter or section headings, with this attribute. If you were to use foreground instead, the colored bar would be output above the actual header content and thus overwrite it. If neither of the two attributes is explicitly specified, the content would be output in the background as well as in the foreground by default, thus also overwriting the actual header content.

After changing the current color, a \rule of the size \layerwidth and \layerheight is simply output as the content. The two macros \layerwidth and \layerheight can be used like lengths within a layer definition and reflect the width and height of the current layer. This is particularly useful for layers that were not defined by explicitly specifying position and size, but by using simplifications such as head. As a \rule is normally positioned on the baseline, it is moved down by the depth of a standard text line \dp\strutbox so that it actually covers the header area exactly.

As an alternative to moving the layer using the optional argument of \rule, you could also declare the layer as an image layer instead of a text layer:

\DeclareLayer[
  background,
  mode=picture,
  head,
  contents={\color{DeepSkyBlue3}\rule{\layerwidth}{\layerheight}}
]{head.background}

This means that contents are not output on the baseline, but from the bottom edge of the layer. This is also useful if you want to draw into the layer with a picture environment or TikZ, for example to work with a color gradient instead of a single color.

In addition to declaring the layer, you must of course ensure that the layer is also used. To do this, you add the layer to a page style. scrlayer offers the option of inserting layers either at the beginning or end of a layer page style or before or after a specific other layer page style. We want the color to be displayed in the background and therefore first, so we add it at the very beginning with \AddLayersAtBeginOfPageStyle.

\documentclass{scrartcl}
\usepackage[x11names]{xcolor}
\usepackage[automark]{scrlayer-scrpage}

\usepackage{blindtext}

\DeclareLayer[
  background,
  mode=picture,
  head,
  contents={\color{DeepSkyBlue3}\rule{\layerwidth}{\layerheight}}
]{head.background}

\AddLayersAtBeginOfPageStyle{scrheadings}{head.background}

\begin{document}
\title{Titel}
\author{Autor}
\maketitle
\blinddocument
\end{document}

As the plain page style plain.scrheadings has no header content when using scrartcl and the default setting of scrlayer-scrpage, it would make no sense to add the header background to this page style. Therefore, the layer in the example is only added to the headings style scrheadings.

If you wanted to add the layer to all layer page styles (including empty!), you could simply use:

\AddLayersAtBeginOfPageStyle{@everystyle@}{head.background}

Related

Wiki (English): HowTo_PageHeadFoot

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.