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 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?

share|improve this question
    
Are you referring to sections, as in \section (in an article) or did you mean chapters, as in \chapter (for a book). –  Gonzalo Medina May 2 '13 at 17:14
    
    
I mean \section. Thanks –  user1285419 May 2 '13 at 17:46
    
Hi cmhughes, thanks for the link. I follow that code, it works. But if the title of the section is too long, the shaded box won't adjust automatically to fit the new height –  user1285419 May 2 '13 at 18:19

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
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 2 '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 2 '13 at 19:38
    
Thanks, it looks great –  user1285419 May 2 '13 at 22:20

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.