# Decrease length of underline in math

In math I often find that single underlined letters have too long of a line. Here is an example: $\beta\in[\underline{\beta},\bar{\beta}]$

Or perhaps worse:

I can solve the too narrow bar loading some accents from the mathxm font of package mathabx, but I have not found a good way to decrease the width of e.g. \underline{U}.

I would appreciate a hint.

Here is a summary of different functions. I have added the nunder-function, which is kind of fun. However, it desperately needs an additional optional argument to set the division factor \mkern\the\numexpr#1/2mu\relax (hard-coded to two).

Two see why compare the following examples of KP-fonts (left) and CM|nunder div by 2 (middle) and CM|nunder div. by 10 (right). On the right image nunder is perhaps as nice as bunderline.

nunder works very well KP-fonts, but performs poorly with CM with the hard-coded value of 2. I will have two look further into two optional arguments, but my first attempt was not a success (using the blah example of TeX Faq).

\documentclass{article}
\usepackage{etex}
\usepackage{amsmath}
\usepackage{mathtools}

\newcommand{\bunderline}[2][4]{\underline{#2\mkern-#1mu}\mkern#1mu}
\newcommand{\boverline}[2][4]{\overline{#2\mkern-#1mu}\mkern#1mu}
\newcommand{\nunder}[2][5]{\mathrlap{\mkern\the\numexpr#1/2mu\relax\underline{\phantom{\mathrm{#2}\mkern-#1mu}}}#2}
\newcommand{\nunderline}[2][4]{%
\ensuremath{\mathrlap{\mkern#1mu\underline{\phantom{\mathrm{#2}\mkern-#1mu}}}}#2}
\begin{document}

\begin{align}
\label{eq:4}
\nunderline[3]{U}\nunderline[3]{\beta}\\
\nunderline[6]{U}\nunderline[6]{\beta}\\
\bunderline[6]{U}\bunderline[2]{\beta}\\
\nunder[6]{U}\nunder[2]{\beta}\\
\nunder[3]{U}\nunder[2]{\beta}
\end{align}

\end{document}

-
a remark possibly a bit off-topic: your comparison of \underline with \bar is a bit misguided; replace \bar with \overline and you get something visually compatible with \underline. \bar is supposed to denote some other variable, like a statistical mean, and this is not what \underline is supposed to be used for. So your quite legitimate question could have been formulated rather as "is there an \underbar?". –  jfbu Oct 24 '13 at 13:58

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}


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 }


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}$


-
A potentially nice addition is \newcommand{\bunderline}[2][4]{\underline{#2\mkern-#1mu}\mkern#1mu } –  Rasmus Mar 24 '12 at 23:26
@Rasmus I agree. –  egreg Mar 24 '12 at 23:31
Do you think it would be re-center the underline ex post shortening? The appropriate length for U might be something like \bunderline[6]{U}, but then, at least with KP-fonts, the line is sort of dragged to the left. –  Rasmus Mar 24 '12 at 23:57

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.

-
I did not even think about the reason, but yeah, you are right on the italic thing. –  Rasmus Mar 24 '12 at 23:28
More robustness and adjustment: \newcommand{\narrowunderline}[2][1]{\mathrlap{underline{\phantom{\ensuremath{\m‌​athrm{#2}\mkern-#1mu}}}}#2\mkern#1mu} (I hoped I managed to copy it correctly from my internet-less work machine) –  Rasmus Mar 25 '12 at 0:09
@Rasmus, make this into an answer! (After adding the missing backslash;).) –  mbork Mar 25 '12 at 0:22
I tried to edit it, but the 5 min rule caught me :) I am reluctant as it seems to change the "kerneling" of letters (as it used that mathrm{.} version). Thus, it might be better to use the vphantom(.)/hphantom(.) functions. I'd have to check more... –  Rasmus Mar 25 '12 at 0:42

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

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

-