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".
5 Answers
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.
-
2IMHO it should be called
\textoverline
, just to be consistent with all the other\text...
macros. Jul 26, 2011 at 14:01 -
3@Martin Done. The definition is just copied from the kernel (re)definition of
\underline
.– egregJul 26, 2011 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! May 28, 2016 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?– skpblackJul 5, 2017 at 8:35 -
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.
Use \={o}
, it works in text mode.
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.– QaswedFeb 24, 2018 at 8:46
\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}