24

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".

27

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.

  • 2
    IMHO it should be called \textoverline, just to be consistent with all the other \text... macros. – Martin Scharrer Jul 26 '11 at 14:01
  • 3
    @Martin Done. The definition is just copied from the kernel (re)definition of \underline. – egreg Jul 26 '11 at 14:04
  • I don't know if more recent packages define it, but just to note: if \textoverline definition fails, using \renewcommand will do the trick. Really good answer! – logo_writer May 28 '16 at 3:11
  • @egreg And if \textoverline{awordverylong} is applied over a large word and hyphenation is necessary? Is it kept overline for each one of pieces? – skpblack Jul 5 '17 at 8:35
  • @skpblack You may want to look at the soul package. – egreg Jul 5 '17 at 8:43
9

The basic answer to your question is that you can use the \overline{} command from math mode for text, too, if you combine it with \mbox{} command. The \mbox{} command lets you shift out of math mode while being in math mode. So try this:

\documentclass{article}
\begin{document}
$\overline{\mbox{This is under a line}}$ but this isn't.
\end{document}

Your question was essentially answered by egreg but there wasn't much explanation and this is a little too long to put as a comment.

8

If you want to have line breaks in the text you can use the soul package:

\documentclass{article}
\usepackage{soul}

\newcommand\ol[1]{{\setul{-0.9em}{}\ul{#1}}}

\begin{document}
\ol{This is under a line.}

\Huge\ol{This is under a line.}
\end{document}

The disadvantage of this approach is that the line height does not depend on the height of the content (so capital letters with accents will be problematic). It does scale with font size though.

  • A short hint: smaller numbers in \setul{} (e.g. \setul{-1.1em}) will increase the space between line and text. – Qaswed Feb 24 '18 at 8:46
5
\documentclass{article}
\newsavebox\TBox
\def\textoverline#1{\savebox\TBox{#1}%
  \makebox[0pt][l]{#1}\rule[1.1\ht\TBox]{\wd\TBox}{0.4pt}}
\begin{document}

\textoverline{foo}
\textoverline{\textit{foo\"A}}
\tiny\textoverline{bar}

\end{document}
4

Use \={o}, it works in text mode.

  • Do you have a link to where you found that on TeX.SE? That would be useful to include in this answer. – Null Aug 2 '16 at 15:27
  • Welcome to TeX.SX! This is for getting a bar accent over one character, not for overlining a bunch of them. – egreg Aug 2 '16 at 15:29

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.