[Tex/LaTex] Section title gradient

gradientkoma-scriptsectioningtitlesec

Background

Writing a manual using KOMA Script. The default fonts are perfect and should not be changed.

Problem

KOMA Script produces the following section outline:

enter image description here

The desired outline looks similar to the following (gradient extended to left and right margins):

enter image description here

The troubles I have encountered:

  • The fount for numerals and text changes.
  • Retaining the text spacing.
  • Expanding or extending the gradient box to:
    • the left and right margins (\textwidth?); and
    • a bit larger than the section (or subsection) text's top and bottom margins.

Source Code

My first attempt resembles:

\let\oldsection\section
\renewcommand{\section}[1]{%
  \oldsection[#1]{
    \setlength\fboxrule{0pt}%
    \setlength\fboxsep{0pt}%
    \tikz[background rectangle/.style={left color=blue!20,right color=white},
    show background rectangle] 
    \node [inner sep=0pt] (0,0) {#1};%
  }
}

This produces:

enter image description here

Update

Changing the text width for the node helps:

\node [text width=\textwidth, inner sep=0pt] (0,0) {#1};%

This produces:

enter image description here

Related

Question

How would you implement a margin-to-margin gradient (preferably using TikZ) that is slightly larger than the text?

Best Answer

This is relatively easy to achieve using the titlesec package:

\documentclass{scrbook}
\usepackage{tikz,titlesec}

\usepackage[english]{babel}
\usepackage{blindtext}      % for a test document

\titleformat{\section}[block]%              
    {\usekomafont{sectioning}\usekomafont{section}%
     \tikz[overlay] \shade[left color=blue!20,right color=white] (0,-1ex) rectangle (\textwidth,1em);}%    
    {\thesection}%                   
    {1em}%
    {}

\begin{document}
\blinddocument 
\end{document}

example

Note the following:

  • \usekomafont{sectioning}\usekomafont{section} sets the font to whatever is chosen using the KOMA Script styling mechanism.

  • The [overlay] option of \tikz means that TikZ won't take any space.

  • This breaks if the title is longer than a line.


Edit: Here is second version, that works with long lines and does not ignore ascenders and descenders. The code is more complicated, though. It draws the whole section heading with TikZ.

\documentclass{scrbook}
\usepackage{tikz,titlesec}
\usetikzlibrary{calc}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommand\boxedsection[1]{{%
    \usekomafont{sectioning}\usekomafont{section}%
    \begin{tikzpicture}[inner sep=0pt, inner ysep=0.3ex]
        \node[anchor=base west] at (0,0) (counter) {\thesection};
        \path let \p1 = (counter.base east) in node[anchor=base west, text width={\textwidth-\x1-0.33em}] (content) at ($(counter.base east)+(0.33em,0)$) {#1};
        \begin{pgfonlayer}{background}
            \shade[left color=blue!20,right color=white] let \p1=(counter.north), \p2=(content.north) in
            (0,{max(\y1,\y2)}) rectangle (content.south east);
        \end{pgfonlayer}
    \end{tikzpicture}%
}}

\titleformat{\section}%              
    {}%
    {}%
    {0pt}%
    {\boxedsection}%

\begin{document}
\chapter{test}
\section{Overview}
\section{Implementation}
\section{a very long section entry a very long section entry a very long section entry a very long section entry a very long section entry}
\end{document}

example