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.

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?

share|improve this question
    
Should the rule hang in the margin? –  Gonzalo Medina Nov 25 '12 at 15:29
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
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
show 2 more comments

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

share|improve this answer
    
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
add comment

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.