I would insert two horizontal line here an example:
-------
Foo
-------
I had used hrulefill but the line has the length of the page.
You could use
\begin{tabular}{c}
\hline
Foo \\
\hline
\end{tabular}
This should leave an overlap of the horizontal rules of length \tabcolsep on either side of Foo. Other, perhaps more crude ways of obtaining this kind of output include
$\overline{\mbox{\underline{\ Foo\ }}}$
Of course, regardless of the choice, if this is a frequent use of something, consider making a macro:
\newcommand{\ouline}[1]{%
\begin{tabular}{c}
\hline
#1 \\
\hline
\end{tabular}}
You can create a length for your bar and then set its width to the same width of a text, something like:
\newlength{\foorulewidth}
\settowidth{\foorulewidth}{Foo}
Later you can create a command to do the whole thing. Something like:
\makeatletter
\newcommand{\ruledtext}[1]{
\newlength{\ruledtextwidth}
\settowidth{\ruledtextwidth}{#1}
\rule[.2\baselineskip]{\ruledtextwidth}{.3pt}\par
#1\par
\rule[.2\baselineskip]{\ruledtextwidth}{.3pt}
\let\ruledtextwidth\@undefined % This is needed to be able to use the same length later
}
\makeatother
Here's a MWE:
\documentclass[12pt]{article}
\makeatletter
\newcommand{\ruledtext}[1]{
\newlength{\ruledtextwidth}
\settowidth{\ruledtextwidth}{#1}
\rule[.2\baselineskip]{\ruledtextwidth}{.3pt}\par
#1\par
\rule[.2\baselineskip]{\ruledtextwidth}{.3pt}
\let\ruledtextwidth\@undefined % This is needed to be able to use the same length later
}
\makeatother
\begin{document}
\ruledtext{Foo}
\ruledtext{Bar}
\ruledtext{create a horizonal line having the length of the text}
\end{document}
\overline/\underlineapproach should work though, right? Or are you concerned with the descenders/ascenders causing problems with the depth/height of your output? You could update the question to reflect your concerns, perhaps providing more attention to the subject and getting better answers. – Werner Nov 09 '12 at 20:52