[Tex/LaTex] Draw horizontal line left and right of some text (a single line)

horizontal alignment

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.

Best Answer

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

Related Question