# How to frame headings in \texwidth-long colored bounding box?

I am trying to add a background frame around my sections and subsections. The frame should be colored (in an 80% gray) and span the \textwidth.

I have tried a few (uninformed) approaches and succeeded in coloring either the section number, the period after the number, the title or a little part of the title.

I tried to make sense of the 'titlesec' package, to no avail.

How do I do this?

-

Vincent Zoonekynd's LaTeX website has some really straight-forward examples of chapter and section styles. I took the following minimal example by slightly modifying entry 31 of the section styles.

\documentclass{article}
\usepackage{lipsum,xcolor}
\makeatletter
\def\section{\@ifstar\unnumberedsection\numberedsection}
\def\numberedsection{\@ifnextchar[%]
\numberedsectionwithtwoarguments\numberedsectionwithoneargument}
\def\unnumberedsection{\@ifnextchar[%]
\unnumberedsectionwithtwoarguments\unnumberedsectionwithoneargument}
\def\numberedsectionwithoneargument#1{\numberedsectionwithtwoarguments[#1]{#1}}
\def\unnumberedsectionwithoneargument#1{\unnumberedsectionwithtwoarguments[#1]{#1}}
\def\numberedsectionwithtwoarguments[#1]#2{%
\ifhmode\par\fi
\removelastskip
\vskip 3ex\goodbreak
\refstepcounter{section}%
\hbox to \hsize{%
\colorbox{black!20}{%
\hbox to 1cm{\hss\bfseries\Large\thesection.\ }%
\vtop{%
\parindent=0pt
\leavevmode\raggedright\bfseries\Large
#2
}%
}}\nobreak
\vskip 2mm\nobreak
\protect\numberline{\thesection}%
#1}%
\ignorespaces
}
\def\unnumberedsectionwithtwoarguments[#1]#2{%
\ifhmode\par\fi
\removelastskip
\vskip 3ex\goodbreak
%  \refstepcounter{section}%
\hbox to \hsize{%
\colorbox{black!20}{%
%      \hbox to 1cm{\hss\bfseries\Large\thesection.\ }%
\vtop{%
\parindent=0pt
\leavevmode\raggedright\bfseries\Large
#2
}%
}}\nobreak
\vskip 2mm\nobreak
%    \protect\numberline{\thesection}%
#1}%
\ignorespaces
}
\makeatother
\pagestyle{empty}
\begin{document}
\lipsum[1]
\section*{Introduction}
\lipsum[1]
\section{Suite}
\lipsum[1]
\section{Suite}
\lipsum[1]
\section{Fin}
\lipsum[1]
\end{document}​


This is what it looks like:

I would imagine this is what you're after. Moreover, you can easily change this if you want a different colour for your sectional backgrounds.

-
Thanks! This is exactly what I was looking for and it is in no way as straight-forward as I had imagined it would be. I tinkered with it and modified it to get it to work with subsections. If I understood correctly, the example is a redefinition of the primitive definition of the section command, is that correct? Also, thanks for the link. My french is not that great, but it was good enough to learn a thing or two. –  Ricardo Jul 21 '11 at 15:06
Yes, it is a primitive (or low-level) redefinition of \section, allowing for the standard style of usage - either in the [1] starred/unnumbered or [2] unstarred/numbered version , and both [3] with or [4] without the optional ToC entry. In total there are 7 sectional definitions: The first to decide between [1] and [2]. Then, the definition of [1] and [2] each have 2 extra macros defined to decide between [3] and [4]. Sometimes, using the primitive definitions give you more understanding behind the procedure involved in producing the output. –  Werner Jul 21 '11 at 15:42

Here's a (really hacky?1) way of doing it with color and titlesec:

\usepackage{color}
\usepackage{titlesec}

\makeatletter
\newif\if@secnum
\definecolor{lgray}{gray}{0.75}
\titleformat{\section}[hang]{\Large\bfseries}{%
\global\@secnumtrue
}{0em}{%
{%
\setlength{\fboxsep}{0pt}%
\colorbox{lgray}{\makebox[\textwidth]{\Large\strut}}%
}%
\hspace*{-\textwidth}%
\if@secnum%
\arabic{section}%
\hspace*{1em}%
\fi%
\global\@secnumfalse%
}[]
\makeatother


Result:

1 More experienced TeXnicians are welcome to point out improvements or deadly sins in this code. Using LaTeX is a learning experience :)

-