14

I want to overline a piece of text, but the line is too close to the number, which is not nice, so I want to add some kind of padding between the overline and the text. This is what I've made up so far:

$\overline{\mbox{XXX\raisebox{0.8cm}{}}}$

where XXX is the text I want to overline.

This looks very ugly for me, so I was wondering whether there is any better way to do it.

SztupY
  • 1,346
  • 10
  • 16
  • 1
    probably \overline{\strut XXX} (or make a similar different strut if that isn't the correct spacing, but strut is used in other places to get even vertical spaces) – David Carlisle Dec 19 '12 at 11:12

2 Answers2

14

The following example shows some alternatives.
Remarks:

  • \raisebox can also be used to specify the final height (and depth). The height is known as \height that can be used for calculations (\dimexpr or package calc).

  • \vphantom: placeholder for height and depth of the argument. The argument is not set and the width is zero.

  • \rule with zero width can be used to insert an invisible vertical rule.

  • \strut adds a invisible vertical box with height .7\baselineskip and depth .3\baselineskip.

Example file:

\documentclass{article}

\makeatletter
\newcommand*{\ov}[1]{%
  $\m@th\overline{\mbox{#1}}$%
}
\newcommand*{\ovA}[1]{%
  $\m@th\overline{\mbox{#1}\raisebox{3mm}{}}$%
}
\newcommand*{\ovB}[1]{%
  $\m@th\overline{\mbox{#1\rule{0pt}{3mm}}}$%
}
\newcommand*{\ovC}[1]{%
  $\m@th\overline{\mbox{#1\strut}}$%
}
\newcommand*{\ovD}[1]{%
  $\m@th\overline{\mbox{#1\vphantom{\"A}}}$%
}
\newcommand*{\ovE}[1]{%
  $\m@th\overline{\raisebox{0pt}[1.2\height]{#1}}$%
}
\newcommand*{\ovF}[1]{%
  $\m@th\overline{\raisebox{0pt}[\dimexpr\height+1mm\relax]{#1}}$%
  % Package `calc' can be used as alternative for `\dimexpr'.
}
\newcommand*{\ovG}[1]{%
  $\m@th\overline{\raisebox{0pt}[\dimexpr\height+1mm\relax]{#1\vphantom{A}}}$%
}
\makeatother

\begin{document}

\setlength{\parskip}{5mm}

\ov{XXX} \ov{xxx} \ov{\"A}

\ovA{XXX} \ovA{xxx} \ov{\"A} \qquad\verb|\raisebox{3mm}{}|

\ovB{XXX} \ovB{xxx} \ov{\"A} \qquad\verb|\rule{0pt}{3mm}{}|

\ovC{XXX} \ovC{xxx} \ov{\"A} \qquad\verb|\strut|

\ovD{XXX} \ovD{xxx} \ov{\"A} \qquad\verb|\vphantom{\"A}|

\ovE{XXX} \ovE{xxx} \ov{\"A} \qquad\verb|\raisebox{0pt}{1.2\height}|

\ovF{XXX} \ovF{xxx} \ov{\"A} \qquad\verb|\raisebox{0pt}{\height+1mm}|

\ovG{XXX} \ovG{xxx} \ov{\"A} \qquad\verb|\raisebox{0pt}{\height+1mm} + \vphantom{A}|

\end{document}

Result

Heiko Oberdiek
  • 271,626
  • Very nice list, for all kind of different needs. I actually went the \strut way, but the \vphantom method looks very nice for longer texts – SztupY Dec 19 '12 at 18:07
  • I tried to use the second command with: \begin{equation} \ovA{\rho_{0}} \end{equation}, but I encounter the following problems Extra }, or forgotten $. \ovA{\rho_{0}} and Bad math environment delimiter. \end{equation} – Antonio Mar 20 '19 at 19:11
  • @Antonio The argument of \ovA is set in text mode, for math the math mode setting needs to be added, e.g.: \ovA{$\rho_{0}$}. – Heiko Oberdiek Mar 28 '19 at 22:22
8

The separation is controlled by a font parameter, so it's a bit difficult to change it. You can add a predetermined height to the text:

\documentclass{article}

\newcommand{\tolstrut}{%
  \vrule height\dimexpr\fontcharht\font`\A+.1ex\relax width 0pt\relax
}

\DeclareRobustCommand{\textoverline}[1]{%
  \ensuremath{\overline{\mbox{\tolstrut#1}}}%
}

\begin{document}
\textoverline{no} \textoverline{with}
\end{document}

Play with the 0.1ex dimension, trying various values until you're satisfied.

enter image description here

egreg
  • 1,121,712
  • I have tried it but doesn't work at least in beamer. Increasing .1ex should rise up \overline? –  Oct 11 '17 at 13:55