Section title gradient

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


It is not recommended to use titlesec together with a KOMA-Script class. But with KOMA-Script Version 3.19 or newer you can redefine \sectionlinesformat to get the desired section title layout.

\usepackage{tikz}
\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \ifstr{#1}{section}
      {\tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
      {\@hangfrom{\hspace*{#2}#3}{#4}}%
}
\makeatother

enter image description here

Note that this works also for \addsec and \section*.

Code:

\documentclass{scrbook}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\usepackage{tikz}
\makeatletter
  \renewcommand\sectionlinesformat[4]{%
    \Ifstr{#1}{section}
      {\tikz\node[inner xsep=0pt,inner ysep=0.3ex,left color=blue!20,right color=white]
        {\parbox{\textwidth}{\raggedsection\@hangfrom{\hspace*{#2}#3}{#4}}};}
      {\@hangfrom{\hspace*{#2}#3}{#4}}%
}
\makeatother

\usepackage{blindtext} %dummy text
\begin{document}
\tableofcontents
\chapter{test}
\section{Overview}
\addsec{Section without number}
\section*{Second section without number}
\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}
\Blindtext
\end{document}