# Gradient fill around section headings

Trying to make a document that has gradient fill around section titles, that are not effected by margins, as such:

I managed to get the gradient effect but I can't figure out how to get it to start from the leftmost part of the page. I get this:

with this code:

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usetikzlibrary{shapes,shadows,calc}
\usepackage{lipsum}
\usepackage[hmargin=0.5in,bmargin=1in,tmargin=1in,centering]{geometry}

\definecolor{gray}{RGB}{236,236,236}

\newcommand\SecTitle[4]{%
\begin{tikzpicture}
\node[inner xsep=0pt,minimum height=3cm,text width=0.7\textwidth,
align=left,left color=gray,right color=white, signal to=#1,font=\Huge,anchor=#2]
at (#3,0) {#4};
\end{tikzpicture}%
}
%\titleformat{command}[shape]{format}{label}{sep}{before-code}{after-code}
\titleformat{\section}
{\normalfont}{}{0em}
{\SecTitle{east}{west}{0\paperwidth}{#1}}

\begin{document}

\section{Test Section One}
\lipsum[2]
\section{Test Section Two}
\lipsum[2]
\clearpage
\section{Test Section Three}
\lipsum[2]
\section{Test Section Four}
\lipsum[2]

\end{document}

• How do I extend the gradient fill to the left so it hits the edge of the page without effecting the margin of the text.
• How do I indent the section heading so that there's space between the beginning of the gradient and the title.
-

## Updated version

\documentclass{article}
\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{shapes,shadows,calc}
\usepackage{lipsum}
\usepackage[hmargin=0.5in,bmargin=1in,tmargin=1in,centering]{geometry}

\definecolor{gray}{RGB}{236,236,236}

% Distance between left side of node and title text
\newlength\TitleIndentLeft
% Distance between right side of node and title text
\newlength\TitleIndentRight
% Total width for the node
% the total width of the title is internally calculated
% as \TitleWidth-\TitleIndentLeft-\TitleIndentRight
\newlength\TitleWidth

\setlength\TitleIndentLeft{0.25in}
\setlength\TitleIndentRight{0pt}
\setlength\TitleWidth{\dimexpr\textwidth+0.5in\relax} % the node will
% start from the left margin and will extend to the right margin
% 0.5in is added since it's the value for the left margin of the document

\newcommand\SecTitle[1]{%
\begin{tikzpicture}
\node[
outer sep=0pt,
inner xsep=0pt,
inner ysep=1cm,
text width=\TitleWidth,
align=left,
left color=gray,
right color=white,
font=\Huge,
anchor=west
]
at (0,0)
{%
\parbox{\TitleIndentLeft}{\mbox{}}%
\parbox{\dimexpr\textwidth-\TitleIndentLeft-\TitleIndentRight\relax}{\raggedright\strut#1\strut}%
\parbox{\TitleIndentRight}{\mbox{}}%
};
\end{tikzpicture}%
}
%\titleformat{command}[shape]{format}{label}{sep}{before-code}{after-code}
\titleformat{\section}
{\normalfont}
{}
{-0.5in}% -0.5in here since it's the value for the left margin
{\SecTitle}

\begin{document}

\section{Test Section One}
\lipsum[2]
\section{Test Section Two  with a Longer Title so that it Spans more than one Line}
\lipsum[2]

% Just for the example to illustrate the effect of the lengths controling the
% title alignment and length
\setlength\TitleIndentLeft{0.5in}
\section{Test Section Two  with a Longer Title so that it Spans more than one Line}
\lipsum[2]

\end{document}


## Remarks:

• I changed the definition for \SecTitle so it only uses one mandatory argument; in this way, it's not necessary to use the explicit option for titlesec.

• I defined three auxiliary lengths to help control the total width for the node as well as the distance between the left, right borders of the node and the title:

% Distance between left side of node and title text
\newlength\TitleIndentLeft
% Distance between right side of node and title text
\newlength\TitleIndentRight
% Total width for the node
% the total width of the title is internally calculated
% as \TitleWidth-\TitleIndentLeft-\TitleIndentRight
\newlength\TitleWidth


Changing the values for those length, you can easily control the overhang for the title as well as the total width desired; in the example code above I illustrate this in the third section title.

• Instead of using minimum height, I now use inner ysep so the box will adjust to longer titles and the vertical separation between the top/bottom of the text and the borders of the box will remain constant.

## Initial versions:

Like this? (I was not sure about the desired horizontal shift for the title from the left margin, so based on the image posted I assumed it had to overhang a little, if this is not the case, let me know where exactly it should be to correct the positioning):

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usetikzlibrary{shapes,shadows,calc}
\usepackage{lipsum}
\usepackage[hmargin=0.5in,bmargin=1in,tmargin=1in,centering]{geometry}

\definecolor{gray}{RGB}{236,236,236}

\newcommand\SecTitle[4]{%
\begin{tikzpicture}
\node[inner xsep=10pt,minimum height=3cm,text width=0.7\textwidth,
align=left,left color=gray,right color=white, signal to=#1,font=\Huge,anchor=#2]
at (#3,0) {#4};
\end{tikzpicture}%
}
%\titleformat{command}[shape]{format}{label}{sep}{before-code}{after-code}
\titleformat{\section}
{\normalfont}{}{-3.5em}
{\SecTitle{east}{west}{0}{#1}}

\begin{document}

\section{Test Section One}
\lipsum[2]
\section{Test Section Two}
\lipsum[2]
\clearpage
\section{Test Section Three}
\lipsum[2]
\section{Test Section Four}
\lipsum[2]

\end{document}


It's not clear to me why so many arguments for \SecTitle if only the fourth one is really gonna be used as variable.

In fact, if just the last argument is to be really a variable used for the title, you can reduce the definition of \SecTitle to something like

\newcommand\SecTitle[1]{%
\begin{tikzpicture}[remember picture,overlay]
\node[inner xsep=10pt,minimum height=3cm,text width=0.7\textwidth,
align=left,left color=gray,right color=white,font=\Huge,anchor=west]
at (current page.west|-0,0) {#1};
\end{tikzpicture}%
}


and this also avoids the use of explicit; notice that I also used

(current page.west|-0,0)


to position the node at the right location (flushed to the left margin) without having to guess lengths. The code (adjust the lengths according to your needs):

\documentclass{article}
\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{shapes,shadows,calc}
\usepackage{lipsum}
\usepackage[hmargin=0.5in,bmargin=1in,tmargin=1in,centering]{geometry}

\definecolor{gray}{RGB}{236,236,236}

\newcommand\SecTitle[1]{%
\begin{tikzpicture}[remember picture,overlay]
\node[inner xsep=10pt,minimum height=3cm,text width=0.7\textwidth,
align=left,left color=gray,right color=white,font=\Huge,anchor=west]
at (current page.west|-0,0) {#1};
\end{tikzpicture}%
}
%\titleformat{command}[shape]{format}{label}{sep}{before-code}{after-code}
\titleformat{\section}
{\normalfont}
{}
{0em}
{\SecTitle}
\titlespacing*{\section}
{0pt}{53pt plus 1ex minus .2ex}{50pt plus .2ex}

\begin{document}

\section{Test Section One}
\lipsum[2]
\section{Test Section Two}
\lipsum[2]
\clearpage
\section{Test Section Three}
\lipsum[2]
\section{Test Section Four}
\lipsum[2]

\end{document}

-
My \TitleSec code was modified from another's and I was unsure how to properly remove unnecessary things for my purpose. Thanks for your updated code, it makes much more sense. Is there also a way for the code to calculate the minimum height of the box required so that there's always, say, 1cm of space between the top and bottom of the box and the respective ends of text instead of a constant 3cm. I may need some titles to be more than one line, in which case the code I have now won't add extra space, making the titles look uneven. –  TLC Apr 27 '14 at 17:47
@TLC Sure. I'll update the answer. I also have some doubts regarding the total length of the colorized box. Do you want it to be the current value of 0.7\textwidth or do you want it to extend all the way to the right margin? Also, is the horizontal placement for the title right in my code or do you want the title to be aligned with the normal text in the document? –  Gonzalo Medina Apr 27 '14 at 18:07
@TLC I updated my answer (see the code at the beginning under "Updated version") introducing the required modification. I also made some other improvements so now you can easily control the margins for the title inside the node and the total width. The code contains some explanatory comments and I'll some comments to my answer later. –  Gonzalo Medina Apr 27 '14 at 18:52
this is perfect, thanks. Yes, I wanted the box to be a little shorter than the width of the page because it looked a little odd otherwise. And the overhang is correct, if I need to change it I just adjust the \setlength\TitleIndentLeft{0.5in} to whatever indent I desire correct? –  TLC Apr 27 '14 at 19:10
@TLC You are welcome, And, yes, that's correct. Changing \TitleIndentLeft to the desired value you control the overhang. –  Gonzalo Medina Apr 27 '14 at 20:47

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usetikzlibrary{shapes,shadows,calc}
\usepackage{lipsum}
\usepackage[hmargin=0.5in,bmargin=1in,tmargin=1in,centering]{geometry}

\definecolor{gray}{RGB}{236,236,236}

\newcommand\SecTitle[4]{%
\hspace*{-1.5cm}\begin{tikzpicture}
\node[inner xsep=0pt,minimum height=3cm,text width=0.8\textwidth,
align=left,left color=gray,right color=white, signal to=#1,font=\Huge,anchor=#2]
at (#3,0) {\hspace*{1.5cm}#4};
\end{tikzpicture}%
}
%\titleformat{command}[shape]{format}{label}{sep}{before-code}{after-code}
\titleformat{\section}
{\normalfont}{}{0em}
{\SecTitle{east}{west}{0\paperwidth}{#1}}

\begin{document}

\section{Test Section One}
\lipsum[2]
\section{Test Section Two}
\lipsum[2]
\clearpage
\section{Test Section Three}
\lipsum[2]
\section{Test Section Four}
\lipsum[2]

\end{document}

-