# Right-aligning text over expression

Basically, I want something like this:

$$\overset{\qquad\quad\; 2}{\sqrt{x+x^2}}$$


where the expression above is (automatically) right-aligned, but I have no idea how to achieve this. (The example above is manually spaced, I want it to be automatic.)

-
Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). –  Adam Liter Mar 16 '14 at 2:35
Also, you should use $ ... $ instead of $$ ... $$. See Why is $…$ preferable to $$…$$? –  Adam Liter Mar 16 '14 at 2:36

Use \hfill:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

$\overset{\hfill 2}{\sqrt{x+x^2}}$

\end{document}


In modern LaTeX document you shouldn't use the TeX construct $$...$$ but $...$. See Why is $...$ preferable to $$...$$? .

-
Under magnification, it would seem the alignment is not quite perfect. Would you not agree? –  Steven B. Segletes Mar 16 '14 at 2:49
@StevenB.Segletes I can only zoom at 400% (the attached image uses that factor), and I get the "2" flushed to the right. As I understood the question this was the OP's intent, although I see in your answer that you aligned the "2" with the superscript inside the radical. Whcih kind og alignment is desired by the OP, I cannot tell from the question. The OP will decide, I guess. –  Gonzalo Medina Mar 16 '14 at 3:00
Good point. I guess I assumed that he wanted the 2's aligned, but that is a presumption on my part. May the best alignment win! –  Steven B. Segletes Mar 16 '14 at 3:08
You can fix the alignment issue by adding \mkern1mu -- that's just one third of the width of \thinspace -- to the argument of \overset: \overset{\hfill 2\mkern1mu}{\sqrt{x+x^2}}. When viewed at 6400% magnification, the two 2 glyphs look perfectly aligned... –  Mico Mar 16 '14 at 6:51
@Mico As they say, a \kern heals all wounds, or something like that. –  Steven B. Segletes Mar 16 '14 at 11:15

With a stack, you can get the alignment of the two right \scriptstyle numbers, which is how I read the OP's question. Perhaps that is a more constraining interpretation than the OP requested (Gonzalo interpreted "alignment" in a slightly different way).

\documentclass{article}
\usepackage{stackengine}
\begin{document}
\def\sqrtoverset#1#2{\ensurestackMath{%
\def\stackalignment{r}\def\stacktype{L}\stackon[0pt]{%
\def\stacktype{S}\stackon[1pt]{#1}{{}^{#2}}%
}{\displaystyle\sqrt{\phantom{#1}}}%
}}
$\sqrtoverset{x+x^2}{2}$
\end{document}


-
I'm sorry for the confusion, my intention was not necessarily to align the 2's, only the expressions as a whole. (The fact that there even was a superscript 2 under the radical was just coincidental.) Besides, since I did not have the stackengine package and didn't bother to install it either, this was the easiest answer for me. But thanks anyways :) –  Frxstrem Mar 16 '14 at 19:12
@Frxstrem No problem. Glad you got a suitable answer. –  Steven B. Segletes Mar 16 '14 at 20:14