# Controling underline depth in math mode

I use the ulem package to control the depth of underlines by setting

\renewcommand{\ULdepth}{1pt}


This works fine outside math mode. However, when using \uline inside math mode, I seem to lose control over the underline depth. (Adding \smash doesn't really solve the problem either.)

\documentclass{article}

\usepackage[normalem]{ulem}
\setlength{\parskip}{15pt} % add space between paragraphs for illustration

\begin{document}

\renewcommand{\ULdepth}{1pt}
\uline{normal text} $\uline{f(x)=y}$

\uline{normal text} $\uline{\smash{f(x)=y}}$

\renewcommand{\ULdepth}{10pt}
\uline{normal text} $\uline{f(x)=y}$

\end{document}


Is there a way to "regain control" over underline depth within math mode. If such control is not possible with the ulem package (I also tried with soul, which didn't work), are there other packages that can achieve the purpose of making the depth of underlines uniform both inside and outside math mode?

• do you want to underline the entire expression or subterms? If the former you can simply do \uline{normal text} \uline{$f(x)=y$} and it seems to obey the parameter – David Carlisle Sep 13 '13 at 22:11
• @DavidCarlisle: I know that \uline{$<math content>$} works. But I'm underlining texts in the game environment provided by Martin Osborne's sgame package, in which the cells in a matrix is automatically set in math mode. So when I do use \uline in that environment, it would be as if I'm using $\uline{...}$. – Herr K. Sep 13 '13 at 22:11
• well you can do $\mbox{\uline{$....$}}$ – David Carlisle Sep 13 '13 at 22:14
• See tex.stackexchange.com/questions/126291/… where the censor package has been hacked to permit underlining in math mode. You can set the underrule depth and the underrule thickness. – Steven B. Segletes Sep 13 '13 at 22:15
• As a follow-up on my prior comment, \xblackout{abc} $\mblackout[1pt]{y = x^2}$ seems to align the math underline and text underline, regardless of descenders in the math mode. – Steven B. Segletes Sep 13 '13 at 22:30

You can use $\mbox{\uline{$....$}}$ if you want to underline a subterm of the math with ulem text settings.

This is an extension to David's answer:

• It defines \mathuline that can be used in math mode to get the text mode version of \uline, using David's trick. Additionally \mathpalette is used to get the correct math style.
• [Optional] \uline is redefined to use \mathuline in math mode instead of the default \underline. But beware, if you have larger symbols and formulas with larger depth (e.g. \int, \frac), then the line will cross the formula.

Example file:

\documentclass{article}
\usepackage{ulem}

\makeatletter
\newcommand*{\saved@uline}{}
\let\saved@uline\uline

\newcommand*{\mathuline}{%
\mathpalette{\math@uline\saved@uline}%
}
\newcommand*{\math@uline}[3]{%
% #1: ulem command
% #2: math style
% #3: contents
\mbox{#1{$#2#3\m@th$}}%
}

% optional
\renewcommand*{\uline}{%
\relax
\ifmmode
\expandafter\mathuline
\else
\expandafter\saved@uline
\fi
}
\makeatother

\begin{document}
\uline{normal text} $\uline{f(x)=y}$
\end{document}