6

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

Adam Liter
  • 12,567
Frxstrem
  • 163

2 Answers2

6

Use \hfill:

\documentclass{article}
\usepackage{amsmath}

\begin{document} 

\[
\overset{\hfill 2}{\sqrt{x+x^2}}
\]

\end{document}

enter image description here

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

Gonzalo Medina
  • 505,128
  • Under magnification, it would seem the alignment is not quite perfect. Would you not agree? – Steven B. Segletes Mar 16 '14 at 02:49
  • 1
    @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 03:00
  • 1
    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 03: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 06:51
  • @Mico As they say, a \kern heals all wounds, or something like that. – Steven B. Segletes Mar 16 '14 at 11:15
  • @StevenB.Segletes - Nicely put. :-) Not quite sure, though, if it's really a "wound"... – Mico Mar 16 '14 at 11:51
5

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}

enter image description here

  • 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