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


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.)


\setlength{\parskip}{15pt} % add space between paragraphs for illustration


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

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

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


enter image description here

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?

  • 1
    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 Commented Sep 13, 2013 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.
    Commented Sep 13, 2013 at 22:11
  • 1
    well you can do $\mbox{\uline{$....$}}$ Commented Sep 13, 2013 at 22:14
  • 1
    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. Commented Sep 13, 2013 at 22:15
  • 1
    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. Commented Sep 13, 2013 at 22:30

2 Answers 2


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:



  % #1: ulem command
  % #2: math style
  % #3: contents

% optional

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


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .