# Align \sqrt with baseline of other mathematical input

The following mathmatical expression is not being displayed the way I expect it to be.

\documentclass{article}
\usepackage{amsmath,amssymb}
\begin{document}
$3x\sqrt{y}-4-2\sqrt{y}+6x$
\end{document}


Note in the output above, the square root symbol is not "base aligned" (Don't know how to describe it better in LaTex language). I have tried the \smash command but haven't been successful. I know it shouldn't be anything hard to achieve, but seemingly I can't at the moment.

I know that there exists the \smash[b]{$math input$} and \smash[t]{$math input$}.

## EDIT:

Is the code below sufficient? (The answer is NO and that is why I seeked clarification to understand the typographical effects explained below.)

$3x\mathord{\raisebox{0.5\depth}{$\sqrt{y}$}}-4-2\mathord{\raisebox{0.5\depth}{$\sqrt{y}$}}+6x$


which yields:

-
The remedy is worse than the disease, in my opinion. –  egreg Mar 28 '12 at 19:44
@egreg Could you elaborate? I would like to understand your view into the situation. I like your way of expressiong yourself above +1. –  azetina Mar 28 '12 at 20:17
The image you showed should clearly explain what I'm thinking: the result is wrong in many aspects. You don't want to raise the "y" or the whole formula will be unbalanced. –  egreg Mar 28 '12 at 20:20
Isn't the entire \sqrt{y} being raised by the code \raisebox{0.5\depth}{$\sqrt{y}$}}? –  azetina Mar 28 '12 at 20:24
Indeed that's the problem. If the \sqrt{y} is the only square root in the formula, I'd go with \sqrt{\smash[b]{y}} –  egreg Mar 28 '12 at 20:25
show 1 more comment

Square roots are often a cause for small, but annoying, problems. Something like

$\sqrt{a}+\sqrt{b}+\sqrt{y}$


will show a variety of ways the surd is typeset

Particularly problematic are the characters with a descender, but also "j":

$\sqrt{d}+\sqrt{j}+\sqrt{y}$


The decision about how to treat such a formula depends on the symbol involved; for example, if there are only characters with descenders, a \smash[b] (requires amsmath) can solve the problem:

$\sqrt{\smash[b]{g}}+\sqrt{\smash[b]{y}}$


One can see that the white space above the letter is not too much (and the lower angle of the surd is slighly below the baseline).

However this can't be the solution for the first formula, where there's a letter with an ascender: we need to pretend that the ascender is in all square roots, but the depth of y should be masked off

$\sqrt{\vphantom{b}a}+\sqrt{b}+\sqrt{\vphantom{b}\smash[b]{y}}$


Alternatively, one can use \mathstrut that inserts a "phantom parenthesis":

$\sqrt{\mathstrut a}+\sqrt{\mathstrut b}+\sqrt{\mathstrut y}$


but this uses a sligthly larger surd.

Beware of a related problem:

$\sqrt{\sin x}+\sqrt{\cos x}$


gives the wrong

If such formulas are possible in our document, I recommend to change the definition of \cos:

\let\cos\relax % to make LaTeX happy with the following line
\DeclareMathOperator{\cos}{cos\vphantom{i}}


so that the formula will by typeset correctly as

As it can be clearly seen, there's little hope to get an automatic solution, as a good treatment depends on what's in the formula, not only on the particular square root.

-
Excelent explanation and am greatful for the elaborate response. If am not wrong then the "problem" is the character y with a descender, right? –  azetina Mar 28 '12 at 21:42

The lowered square root symbol is actually by design, but I agree with you that it's really very low here. However, I wouldn't recommend to have the symbol on the baseline (in particular since the y has a descender). You can use \vphantom to raise the square root symbol so that it's as high as over an l, which I find looks quite OK:

\documentclass{article}
\usepackage{amsmath,amssymb}
\begin{document}
$3x\sqrt{y\vphantom{l}}-4-2\sqrt{y\vphantom{l}}+6x$
\end{document}


As for you edit, don't do that! It raises the baseline of the y, which is not recommendable at all.

If you want less space around the y, you can use a square root symbol from a smaller font, but I guess that's not a good practice from a typographic point of view. The resulting symbol is not quite as small as the one that Herbert's \SQRT produces (and which he explicitly did not recommend). The code uses the \smash[b] method from egreg's answer, which in this situation is actually more elegant than the \vphantom{l} I proposed above. You can adjust the number 8 below to your needs.

\documentclass{article}
\usepackage{amsmath,amssymb}
\newcommand\smallsqrt[1]{
\savebox\radicand{$\smash[b]{#1}$}
\text{\fontsize{8}{8}\selectfont
$\sqrt{\usebox\radicand}$%
}}
\begin{document}
$3x\smallsqrt{y}-4-2\smallsqrt{y}+6x$
\end{document}

-
Thanks for the reminder. I noticed the typographical drawback when @egreg replied to my edit saying "The remedy is worse than the disease, in my opinion," which is quite a nice way of putting it. –  azetina Mar 29 '12 at 15:19
\documentclass{article}
\usepackage{mathtools}% for \mathrlap
\def\SQRT#1{\mathrlap{\scriptstyle% for a tricky solution
\sqrt{\smash[b]{\mathstrut\hphantom{#1}}}}\mkern10mu#1}
\begin{document}
$3x\sqrt{{\smash[b]{y\mathstrut}}}-4 -2\sqrt{\smash[b]{y\mathstrut}}+6x$
\end{document}


-
Can the extra vertical space above the y under the sqrt be eliminated? –  azetina Mar 28 '12 at 16:50
@azetina No, that's the minimum size of the symbol; if you remove the \mathstrut the square root symbol will be lowered. –  egreg Mar 28 '12 at 16:51
@azetina: yes that is possible but only with a tricky solution, see edited answer. Replace sqrt with SQRT. However, I wouldn't use it –  Herbert Mar 28 '12 at 17:07