tags:

views:

21

answers:

1

LaTeX newbie here.

I need to set background color for all my \subsection titles. Whole line should change color, not only the part of it with text.

This does work:

\subsection{\colorbox{Gray}{Title}}

But it does not color whole line. Also I'd like to configure it in a single place for all \subsections.

My google-fu is failing me. Any suggestions on how to do what I want?

+2  A: 

To make the \colorbox the width of the line, use \makebox:

\subsection{\colorbox{Gray}{\makebox[\hfill][l]{Title}}}

I'm not 100% sure "\hfill" is what you need to put in the first set of square brackets. You may need to experiment with that part. An alternative worth trying is

\subsection{\colorbox{Gray}{\makebox[\width][s]{Title\hfill}}}

To configure it in one place for all subsections, the easiest thing to do is define a wrapper command:

\newcommand{\mysubsection}[1]
  {\subsection{\colorbox{Gray}{\makebox[\hfill][l]{#1}}}}

You could also redefine \subsection, but then you have to learn about the internal commands it uses and take care to match your documentclass's other formatting. I don't recommend it.

Zack