9

I want a horizontal line to the left and right of some text, that is at most one line wide. The lines should be equally sized such that they fill the remaining horizontal space (like two \hfills in a \hbox).

In case it's not clear what I want, at the top of this page there is a row of buttons labeled Questions | Tags | Users | Badges | Unanswered. And above that row of buttons are two horizontal lines filling the remaining space around a symbol. That is what I want to accomplish in LaTeX.

  • Ok, and how should the text be placed? Centered? – Martin Scharrer Apr 6 '11 at 9:05
  • Yes, the text should be centered. – Mike B. Apr 6 '11 at 9:08
  • Should the line be at the baseline (where the uppercase letters sit on) or vertically centered? – Martin Scharrer Apr 6 '11 at 9:09
  • @Martin: vertically centered would be great. Thanks! – Mike B. Apr 6 '11 at 9:16
10

You can add the lines using \hrulefill and create the line using \makebox[\linewidth]. The vertical alignment is a little tricky. I don't know how to raise the filler, so one way is to lower the text first and than raise it again with the lines.

\documentclass{article}

\newcommand*\ruleline[1]{\par\noindent\raisebox{.8ex}{\makebox[\linewidth]{\hrulefill\hspace{1ex}\raisebox{-.8ex}{#1}\hspace{1ex}\hrulefill}}}

\begin{document}

\hrule
Before\\
\ruleline{TestXg}
After

\end{document}

Result:

Result

  • Thank you! Is it possible to center the lines vertically? I suppose that centering on the height of the lower case letters would look the best. – Mike B. Apr 6 '11 at 9:25
  • @Mike: I just added that. I'm using .8ex as height. 1ex would be the height of a lower case 'x'. An upper case 'X' is about 1.6ex, so .8ex seemed to be a good value. – Martin Scharrer Apr 6 '11 at 9:28
  • The resulting line is shifted to the right on a right page by ~1cm. – Mike B. Apr 6 '11 at 9:44
  • @Mike: Might be the paragraph indent. Place a \noindent before it or at the beginning of the macro (maybe with a leading \par). – Martin Scharrer Apr 6 '11 at 9:49
8
\documentclass{minimal}

\def\headline#1{\hbox to \hsize{\hrulefill\quad\lower.3em\hbox{#1}\quad\hrulefill}}

\begin{document}    
\headline{Centered \ddag{} Symbol} 
\end{document}

headline macro example

Admittedly, this is a TeX solution but I think it is appropriate because this is where TeX is really great. If you need some vertical space after the headline you could incorporate a \medskip right before the last closing brace of the \headline definition. And on second thought: the .3em dimension should be better expressed using ex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.