[Tex/LaTex] Create a banner for section style

sectioningtikz-pgf

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?

Best Answer

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