# How to draw a gradient box around sections

How would you implement a margin-to-margin gradient using TikZ that is slightly larger than the text and that cover the whole lines of section text? The following code produced the results shown below

\documentclass[a4paper,svgnames,10pt]{book}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage[margin=1.5cm]{geometry}
\usepackage{lipsum}
\usepackage{calc}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[cm-default]{fontspec}
\setromanfont{FreeSerif}
\setsansfont{FreeSans}
\setmonofont{FreeMono}
\usepackage{xgreek}
\setmainfont{Arial}

%%%%%%%%%%%%%%%%%%%% fancy heading  \section %%%%%%%%%%%%%%%%%%%%%%

\titleformat{\section}[block]%
{\huge\bfseries%
\tikz[overlay] \shade[left color=LightSkyBlue,right color=white,] (0,-1ex) rectangle (\textwidth,1em);}%
{\thesection}%
{1em}%
{#1}

%%%%%%%%%%%%%%%%%% fancy heading  \chapter %%%%%%%%%%%%%%%%%%%%%%%%

\newcommand*\chapterlabel{}
\titleformat{\chapter}
{\gdef\chapterlabel{}
\normalfont\sffamily\Huge\bfseries\scshape}
{\gdef\chapterlabel{\thechapter\ }}{0pt}
{\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-3cm] at (current page.north west)
{\begin{tikzpicture}[remember picture, overlay]
\draw[fill=LightSkyBlue] (0,0) rectangle
(\paperwidth,3cm);
\node[anchor=east,xshift=.9\paperwidth,rectangle,
rounded corners=20pt,inner sep=11pt,
fill=MidnightBlue]
{\color{white}\chapterlabel#1};
\end{tikzpicture}
};
\end{tikzpicture}
}
\titlespacing*{\chapter}{0pt}{50pt}{0pt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\part{Developmental Disturbance of Oral and Paraoral tissues}

\chapter{Soso and Mimi}

\section{Toto}
\lipsum[1]

\section{Developmental Disturbance of Oral and Paraoral tissues and Related Structures}
\lipsum[1]

\section{Lolo}
\lipsum[1]

\end{document}


The desired output should be like this

-
Friendly hint: to make your code easier to read, you can make it a code block by indenting all of the lines 4 spaces or by highlighting it all and clicking on the {} button in the question editor. –  Adam Liter Aug 7 '13 at 5:02
related ux.SE post: Case studies on using gradients in a design –  Tobias Kienzler Aug 7 '13 at 8:01

## 1 Answer

Here's one option; the eventual number and the title are placed inside a node with the desired horizontal shading:

\documentclass[a4paper,svgnames,10pt]{book}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{linegoal}
\usepackage[explicit]{titlesec}
\usepackage[margin=1.5cm]{geometry}
\usepackage{lipsum}

\definecolor{myblue}{RGB}{158,158,255}

\titleformat{\section}
{\normalfont\Large\bfseries}{}{0em}
{%
\begin{tikzpicture}
\node[inner xsep=0pt,text width=\textwidth,
align=left,left color=myblue,right color=myblue!10]
{\begin{varwidth}[t]{5em}\thesection\hfill\end{varwidth}\hspace{0.5em}\parbox[t]{\linegoal}{\raggedright #1}};
\end{tikzpicture}%
}
\titleformat{name=\section,numberless}
{\normalfont\Large\bfseries}{}{0em}
{%
\begin{tikzpicture}
\node[inner xsep=0pt,text width=\textwidth,
align=left,left color=myblue,right color=myblue!10]
{\parbox[t]{\linewidth}{\raggedright#1}};
\end{tikzpicture}%
}

\pagestyle{plain}

\begin{document}

\chapter{Test chapter}
\section{A test numbered section with a really long title spanning several lines just for the example}
\lipsum[4]
\section*{A test unnumbered section with a really long title spanning several lines just for the example}
\lipsum[4]

\end{document}


-
Thank You That is Perfect –  Dr Adel Aug 7 '13 at 7:53
@DrAdel You're welcome! Don't forget that you can accept the answer by clicking the checkmark to its left. In case of doubt, please see How do you accept an answer?. –  Gonzalo Medina Aug 7 '13 at 18:28
@DrAdel Thanks for accepting the answer! :-) –  Gonzalo Medina Aug 9 '13 at 0:32