# putting square brackets around the underline of a letter (in math mode)

I would be very grateful for some help.

\documentclass{article}
\begin{document}
$a,\underline{b},\underline{c},d$
\end{document}


I need to enclose the underline under the letter $b$ in small square brackets, and likewise for the underline under the letter $c$, but

1. without changing the distance between an underline and the letter above it,
2. without changing the horizontal distance between adjacent letters, and
3. without changing the sizes of the letters or their underlines.

Each pair of square brackets should be slightly shorter (in height) than the commas.

Thanks

• What does this mean? I'm curious. – Manuel Apr 13 '16 at 13:24

You can push down to the depth of the underlined letter a scaled version of the brackets:

\documentclass{article}
\usepackage{amsmath,graphicx}

\makeatletter
\newcommand\bunderline{}% check for being undefined
\DeclareRobustCommand\bunderline[1]{\mathord{\mathpalette\b@underline{#1}}}
\newcommand{\b@underline}[2]{%
\sbox\z@{$\m@th#1\underline{#2}$}%
\raisebox{-\dp\z@}{\scalebox{.5}[.25]{$\m@th#1[$}}%
\copy\z@
\raisebox{-\dp\z@}{\scalebox{.5}[.25]{$\m@th#1]$}}%
}
\makeatother

\begin{document}
$a,\underline{b},\bunderline{b},\underline{g},\bunderline{g}, \underline{i},\bunderline{i},d$

$x_{\bunderline{z}}$
\end{document}


• I like that this only uses amsmath and graphicx. Thanks for this! – texer Apr 13 '16 at 15:45

Something like this? Note that some of the dimensions may need adjustment for the font or point size employed in a document.

\documentclass{article}
\usepackage{stackengine,graphicx}
\stackMath
\def\tinylb{\smash{\scalebox{.5}{$\scriptscriptstyle[$}}}
\def\tinyrb{\smash{\scalebox{.5}{$\scriptscriptstyle]$}}}
\def\bunderline#1{\setbox0=\hbox{$#1$}\def\useanchorwidth{T}%
\stackunder[1.2pt]{#1}{\tinylb\rule[.3pt]{\wd0}{.4pt}\tinyrb}}
\begin{document}
$a,\underline{b},\bunderline{b},\underline{g},\bunderline{g}, \underline{i},\bunderline{i},d$
\end{document}


If you need math styles to be preserved:

\documentclass{article}
\usepackage{stackengine,graphicx,scalerel}
\stackMath
\def\tinylb{\smash{\scalebox{.25}{$\SavedStyle[$}}}
\def\tinyrb{\smash{\scalebox{.25}{$\SavedStyle]$}}}
\def\bunderline#1{\ThisStyle{\setbox0=\hbox{$\SavedStyle#1$}\def\useanchorwidth{T}%
\stackunder[1.2pt]{\SavedStyle#1}{\tinylb\rule[.3\LMpt]{\wd0}{.4pt}\tinyrb}}}
\begin{document}
$a,\underline{b},\bunderline{b},\underline{g},\bunderline{g}, \underline{i},\bunderline{i},d$

$\scriptstyle a,\underline{b},\bunderline{b},\underline{g},\bunderline{g}, \underline{i},\bunderline{i},d$

$\scriptscriptstyle a,\underline{b},\bunderline{b},\underline{g},\bunderline{g}, \underline{i},\bunderline{i},d$
\end{document}


• @texer The answer will not currently work in smaller mathstyles (e.g., \scriptstyle, etc.). If that is a requirement, please let me know. – Steven B. Segletes Apr 13 '16 at 13:18