Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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{%
        \advance \hsize by -1cm
        \advance \hsize by -2\fboxrule
        \advance \hsize by -2\fboxsep
        \parindent=0pt
        \leavevmode\raggedright\bfseries\Large
        #2
        }%
      }}\nobreak
  \vskip 2mm\nobreak
  \addcontentsline{toc}{section}{%
    \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{%
%        \advance \hsize by -1cm
        \advance \hsize by -2\fboxrule
        \advance \hsize by -2\fboxsep
        \parindent=0pt
        \leavevmode\raggedright\bfseries\Large
        #2
        }%
      }}\nobreak
  \vskip 2mm\nobreak
  \addcontentsline{toc}{section}{%
%    \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:

Shaded section styles

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.

share|improve this answer
    
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:

Sections with backgrounds

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.