3

I am wondering if there is any way to change the default style for sections. I would like to create a banner (across the page) with a dark-gray background and the section title in white. It would be great if I can also put two black bold lines on the top and bottom edge of the banner. I saw someone use TikZ to style the section but I am not familiar with that; is there any other package that can do that for me?

Alan Munn
  • 218,180
user1285419
  • 1,121

1 Answers1

2

Of course this could be done using TikZ, but there's not really need to use it here. Here's one possibility using the titlesec package:

\documentclass{article}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}% just to generate text for the example

\titleformat{\section}
  {\normalfont\large\sffamily\color{white}}%
  {}{0em}%
  {{\color{black}\titlerule[3pt]}\vskip-.38\baselineskip\colorbox{gray!80}{\parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{\raggedright\strut\thesection~#1\strut}}}[\vskip-.2\baselineskip{\color{black}\titlerule[3pt]}]
\titleformat{name=\section,numberless}
  {\normalfont\large\sffamily\color{white}}%
  {}{0em}%
  {{\color{black}\titlerule[3pt]}\vskip-.38\baselineskip\colorbox{gray!80}{\parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{\raggedright\strut#1\strut}}}[\vskip-.2\baselineskip{\color{black}\titlerule[3pt]}]

\begin{document}

\section{Test Numbered Section}
\lipsum[4]
\section*{Test Unnumbered Section}
\lipsum[4]

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • Hi Gonzalo, it looks good. But how can I make the lines a bit thicker and remove the gaps between the lines and the box? Thanks again – user1285419 May 02 '13 at 18:46
  • @user1285419 please see my updated answer (the rule thickness can be controlled using the optional argument for \titlerule (I chose 3pt); the separation between the rules and the colored box depends on the length used in the \vskips). – Gonzalo Medina May 02 '13 at 19:38
  • This is a great solution. But if the section heading occurs right at a pagebreak it is possible for the titlerules to become separated from the rest of the heading. Perhaps this could be prevented by putting them inside the parbox? – Ethan Duckworth Aug 19 '22 at 14:38