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".
- 525
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.
- 1,121,712
-
Do you have a link to where you found that on TeX.SE? That would be useful to include in this answer. – Null Aug 02 '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 02 '16 at 15:29
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.
- 12,451
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.
- 89,023
- 26
- 255
- 291
-
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 08: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}
\textoverline, just to be consistent with all the other\text...macros. – Martin Scharrer Jul 26 '11 at 14:01\underline. – egreg Jul 26 '11 at 14:04\textoverlinedefinition fails, using\renewcommandwill do the trick. Really good answer! – logo_writer May 28 '16 at 03:11\textoverline{awordverylong}is applied over a large word and hyphenation is necessary? Is it kept overline for each one of pieces? – skpblack Jul 05 '17 at 08:35soulpackage. – egreg Jul 05 '17 at 08:43