[Tex/LaTex] \overline outside of math mode

environmentsmath-mode

If I want to draw an overline over some text, not an equation, how can I do it? \overline seems to only work in math mode, and when used outside of math mode, it automatically says "Missing $ inserted".

Best Answer

Easy answer:

\newcommand{\textoverline}[1]{$\overline{\mbox{#1}}$}

Not so easy, but neater:

\makeatletter
\newcommand*{\textoverline}[1]{$\overline{\hbox{#1}}\m@th$}
\makeatother

We use the normal \overline of math mode, exploiting the fact that a \hbox in math mode typesets its argument in the font which was current at the time the math formula starts (also keeping spaces).

The strange \m@th is a precaution against possible setting of the parameter \mathsurround, a space added before and after in-line math formulas; it's usually zero, but a class might change it. When math mode is used for things like this, it's best to be on the safe side: \m@th sets the parameter to zero, but since a math formula forms a group, the setting is local and won't change the global one.