# How to place a center aligned rule on the left of a section title?

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?

-
Should the rule hang in the margin? – Gonzalo Medina Nov 25 '12 at 15:29

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}


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.

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

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}
`

-
Thank you for your help. But what I intend is to put the rule in the text region. – lhhNJU Nov 26 '12 at 1:59
@lhhNJU OK, but you should consider what would happen if a section title spans more than one line. – Gonzalo Medina Nov 26 '12 at 2:01
I see. Thank you for your reminder. – lhhNJU Nov 26 '12 at 2:22