Decrease length of underline in math

You can set the object to underline making TeX into believing it's shorter:

\documentclass{article}
\newcommand{\bunderline}[1]{\underline{#1\mkern-4mu}\mkern4mu }

\begin{document}
$\beta\in[\bunderline{\beta},\bar{\beta}]\qquad \bunderline{U}$
\end{document}

enter image description here

As remarked by Rasmus, one might also introduce an optional argument for deciding, in particular cases, the amount of shortening:

\newcommand{\bunderline}[2][4]{\underline{#2\mkern-#1mu}\mkern#1mu }

With \bunderline[6]{U} one would get more shortening.

EDIT

Every font family seems to need a particular default for the shortening and, maybe also a "front shortening". For example, this seems to work well with KPfonts:

\usepackage{kpfonts}
\newcommand{\bunderline}[1]{\mkern2mu\underline{\mkern-2mu#1\mkern-4mu}\mkern4mu }

enter image description here

A package to consider is accents that provides \underaccent:

\underaccent{\bar}{U}

Here's the result (with KPfonts) of

$\beta\in[\underaccent{\bar}{\beta},\bar{\beta}]\qquad\underaccent{\bar}{U}$

enter image description here


Well, the reason is clear: the letters are italic, and "stick out" to the right. This is a quick hack, working only (or mainly) for letters (and not very robust: it assumes that the same font (or font with same widths, to be precise) is used for text and math):

\documentclass[12pt]{article}

\usepackage{mathtools}
\newcommand{\narrowunderline}[1]{\mathrlap{\underline{\vphantom{#1}\hphantom{\textup{#1}}}}#1}

\begin{document}
$U\in[\narrowunderline{U},\bar{U}]$

$f\in[\narrowunderline{f},\bar{f}]$
\end{document}

As you can see, it's neither elegant, nor gives beautiful results - but it's better than the default.


Based upon the first solution of egreg, one could also center the underline:

\newcommand{\ubar}[1]{\mkern3mu\underline{\mkern-3mu #1\mkern-3mu}\mkern3mu}

Tags:

Math Mode