Menu

HowTo_HeadingsBackgroundColor

How to provide headings with a colored background

If you only want to provide the number and text of the heading with a colored background, this is somehow easy by redefining \chapterlinesformat, \sectionlinesformat and \sectioncatchphraseformat:

\documentclass{scrbook}

\usepackage[svgnames]{xcolor}
\usepackage{mwe}

\NewCommandCopy{\ChapterLinesFormat}{\chapterlinesformat}
\renewcommand*{\chapterlinesformat}[3]{%
  \colorbox{LightPink}{%
    \ChapterLinesFormat{#1}{#2}{#3}%
  }%
}

\NewCommandCopy{\SectionLinesFormat}{\sectionlinesformat}
\renewcommand*{\sectionlinesformat}[4]{%
  \colorbox{%
    \UseName{str_case:nnF}{#1}{%
      {section}{Gold}%
      {subsection}{Lavender}%
      {subsubsection}{GreenYellow}%
    }{Thistle}%
  }{\SectionLinesFormat{#1}{#2}{#3}{#4}}%
}

\NewCommandCopy{\SectionCatchPhraseFormat}{\sectioncatchphraseformat}
\renewcommand*{\sectioncatchphraseformat}[4]{%
  \colorbox{%
    \UseName{str_case:nnF}{#1}{%
      {paragraph}{RoyalBlue}%
      {subparagraph}{Olive}%
    }{Thistle}%
  }{\SectionLinesFormat{#1}{#2}{#3}{#4}}%
}
\addtokomafont{paragraph}{\color{white}}
\addtokomafont{subparagraph}{\color{white}}

\begin{document}
\tableofcontents
\blinddocument

\subparagraph{Heading on level 5 (subparagraph)} \blindtext

\end{document}

Here, the original definitions of the three commands are saved as \ChapterLinesFormat, \SectionLinesFormat and \SectionCatchPhraseformat and then reused in the redefinition of the commands within a \colorbox.

For \sectionlinesformat, a case distinction for the selection of the color has also been added, so that \section is colored in Gold, \subsection in Lavender, \subsubsection in GreenYellow and unknown levels in Thistle. Similarly with \sectioncatchphraseformat, whereby the text color was also changed to white for paragraph and subparagraph using \addtokomafont. The case distinction according to the name of the level in the first argument was realized with the LaTeX3 function \str_case:nnF. More information on this function can be found in The LaTeX3 interface.

The solution shown above does not work on the part level, nor when using the chapterprefix option. At these levels, the heading consists of several paragraphs. \colorbox is a horizontal box and therefore does not allow paragraphs. In this case, it does not really make sense to only color the number and the text. For \chapter with chapterprefix, it makes sense to color the entire area of the heading instead. To do this, a \parbox is inserted within the \colorbox over the entire remaining width (taking into account the colored border of the width \fboxsep within the \colorbox):

\documentclass[chapterprefix]{scrbook}

\usepackage[svgnames]{xcolor}
\usepackage{mwe}

\NewCommandCopy{\ChapterLinesWithPrefixFormat}{\chapterlineswithprefixformat}
\renewcommand*{\chapterlineswithprefixformat}[3]{%
  \setlength{\fboxsep}{1ex}% more color around the text
  \colorbox{LightPink}{%
    \parbox{\dimeval{\linewidth-2\fboxsep}}{%
      \ChapterLinesWithPrefixFormat{#1}{#2}{#3}%
    }%
  }%
}

\NewCommandCopy{\SectionLinesFormat}{\sectionlinesformat}
\renewcommand*{\sectionlinesformat}[4]{%
  \colorbox{%
    \UseName{str_case:nnF}{#1}{%
      {section}{Gold}%
      {subsection}{Lavender}%
      {subsubsection}{GreenYellow}%
    }{Thistle}%
  }{\SectionLinesFormat{#1}{#2}{#3}{#4}}%
}

\NewCommandCopy{\SectionCatchPhraseFormat}{\sectioncatchphraseformat}
\renewcommand*{\sectioncatchphraseformat}[4]{%
  \colorbox{%
    \UseName{str_case:nnF}{#1}{%
      {paragraph}{RoyalBlue}%
      {subparagraph}{Olive}%
    }{Thistle}%
  }{\SectionLinesFormat{#1}{#2}{#3}{#4}}%
}
\addtokomafont{paragraph}{\color{white}}
\addtokomafont{subparagraph}{\color{white}}

\begin{document}
\tableofcontents
\blinddocument

\subparagraph{Heading on level 5 (subparagraph)} \blindtext

\end{document}

Of course you can also use the same for \sectionlinesformat.

This method also works for \part. However, the question arises as to exactly how much of the page you want to color. You may want to color the entire text area or even the entire page.

A little hint: Of course, you can also use the method shown to insert more complex boxes for the heading using TikZ or tcolorbox.


Related

Wiki (English): HowTo_Headings

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.