7

The proposed effect

How can I achieve this effect? I have tried the following code:

\parbox[c]{3.5cm}{\color{NavyBlue}{\rule{90px}{7px}}} \parbox[c]{6cm}{\section*{Section Title}}

But the rule is not center aligned with the title because \section command contain some extra space below the text.

And I had tried a alternative:

\parbox[c]{3.5cm}{\color{NavyBlue}{\rule{90px}{7px}}} \parbox[c]{6cm}{\Large{Section Title}}

Although this time the rule is center aligned, but if I add text below the title, the line space would be too small.

Can anyone help me with this?

lhhNJU
  • 545

2 Answers2

5

Seems that there was a related question yesterday How can I make a bold horizontal rule under each section title? Following that example, I seem to be able to create the effect that you desire:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{titlesec}

\titleformat{\section}
  {\color{NavyBlue}\normalfont\Large\bfseries}{\parbox{10em}{\rule{10em}{0.5em}}}{1em}{}[{}]

\begin{document}

\section{Test Section}

Random stuff

\end{document}

enter image description here

I'd never heard of the titlesec package before. Looks like it has some very nice tools for modifying the look and feel of sections.

A.Ellett
  • 50,533
  • Don't use px for the units. It hasn't a fixed value and changes in the preamble might modify it. And I don't think that the \parbox is really needed (use the optional arguments to `\rule). – egreg Nov 25 '12 at 15:22
  • 1
    @egreg. Thanks. I was just following lhhNJU's example as closely as possible. I used the parbox to force vertical centering with the section title. I know you can write \rule[1ex]{10em}{0.5ex} but is there a way to tell rule to center with text on the same line? – A.Ellett Nov 25 '12 at 15:31
  • Just play with the "raise" argument; possibly something like 0.4ex – egreg Nov 25 '12 at 15:33
  • I just noticed I used a non-fixed value length again. @egreg. Are you suggesting that I doing sometihng like \rule[3pt]{2in}{3pt}? – A.Ellett Nov 25 '12 at 15:35
  • ex is good, as it depends on the current font, which is really what you want. – egreg Nov 25 '12 at 15:36
  • Using \rule[0.3ex]{10em}{0.5ex} works fine! – lhhNJU Nov 26 '12 at 01:57
  • @A.Ellett I wonder how I can find documentation for package titlesec ? – lhhNJU Nov 26 '12 at 02:25
5

Here's a variation in which the rule hangs in the margin:

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

\titleformat{\section}
  {\normalfont\Large\bfseries\color{NavyBlue}}{}{0em}{\llap{\rule[.5ex]{90pt}{4pt}\hspace*{1em}}}

\begin{document}

\section{Section Title}
\lipsum[2]

\end{document}

enter image description here

Gonzalo Medina
  • 505,128