# Vertical color background of section title

Good night. It's my problem about gradient colored section title, with vertical length. It's codes are,

\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[svgnames]{xcolor}
\usepackage{lipsum}
\usepackage[explicit]{titlesec}
\usepackage{tikz}

\titleformat{\section}[block]%
{\normalsize\bfseries\itshape\tikz[overlay] \shade[left color=red!20!yellow,right color=black!40!white] (0,-1ex) rectangle  (\linewidth,1em);}%
{\thesection}%
{1em}%
{\color{Maroon}#1}

\begin{document}
\section{A textwidth section - OK.}
\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 - NOT OK.}
\lipsum[1]
\section{A section header full of \lipsum[1]}
\end{document}

But, It is not coloring all of second section. Related HTML pages not exactly for me. Therefore what is the simple code block that should be added this code?

Related:

Format section titles as white text on black background

How to draw a gradient box around sections

\documentclass[svgnames]{scrbook}
\usepackage{tikz,lipsum}
\usepackage{lipsum}
\usepackage{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},text=Maroon] (content) at ($(counter.base east)+(0.33em,0)$) {#1};
\begin{pgfonlayer}{background}
\shade[left color=red!20!yellow,right color=black!40!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}
\section{A textwidth section - OK.}
\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 - NOT OK.}
\lipsum[1]
\end{document}

And if you want to modify your code so that it works use the following:

\documentclass[a4paper,twoside,openright,11pt]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[svgnames]{xcolor}
\usepackage{lipsum}
\usepackage[explicit]{titlesec}
\usepackage{tikz,showframe}

\titleformat{\section}[block]%
{\normalsize\bfseries\itshape}%
{}%
{0em}%
{\tikz\node[left color=red!20!yellow,right color=black!40!white,text=Maroon,inner xsep=0pt,outer sep=0pt,text width=\linewidth,] {\thesection \space #1};}

\begin{document}
\chapter{Chapter One}
\section{Section One}
\section{Section Two}
\section{Mass of this entire universe and me are not equal as I am a part of the universe}

\end{document}

• in the second example You can't use `\dimexpr' in restricted horizontal mode. I don't want use '\usekomafont{sectioning}\usekomafont{section}' because there is '\usepackage[explicit]{titlesec}' another place in the my code. It's take error that I use '\usekomafont'. Only I have to use '\usepackage[explicit]{titlesec}'. It may be solve? – Özgür Sep 6 '14 at 0:35
• It's perfect working again, @Harish Kumar. – Özgür Sep 6 '14 at 0:53
• Also, I am a Physics teacher, too. :) – Özgür Sep 6 '14 at 0:57
• @Özgür Oh Nice! Glad to meet you :-) – user11232 Sep 6 '14 at 0:58
• Section numbers are not black color. How to fix it @Harish Kumar? – Özgür Sep 6 '14 at 1:15