# Aligned numerator and denominator in \frac or any other variant

Is there a way to align the numerator and the denominator of a fraction? Consider two fractions (from a SIAM paper)

The denominator (k!) is nicely centred in the first fraction. However in the second fraction the brackets in the numerator and the denominator are not aligned. As the exponent of the numerator gets bigger, the misalignment gets worse. In general, the strategy of centring numerator and denominator is correct. However, it would be good to have the option of aligning brackets in specific instances like this.

MWE:

\documentclass[preview]{standalone}
\usepackage{amsmath}
\begin{document}
$\dfrac{(x_i - x)^{N+1}}{(N+1)!}$
\end{document}

-
The numerator and denominator are horizontally centered with respect to one another; this is by far the most common typographic treatment of fractions. –  egreg Dec 26 '12 at 16:43
With the mathtools package providing the \mathrlap macro you could do (in the denominator) (N+1)\mathrlap{!}\hphantom{^{N+1}} or, in this simple case, (N+1)^{N+1}\hfill. For the latter already exist a then-abused macro: \cfrac[l]{(x_i - x)^{N+1}}{(N+1)^{N+1}}. –  Qrrbrbirlbel Dec 26 '12 at 16:54
@devendra Although I would recommend to use just the default LaTeX style in your case, here are some related questions: How to improve the looks of a fraction?, \frac{1-z^{n+1}}{1-z} doesn't look very good … –  Qrrbrbirlbel Dec 26 '12 at 17:01
Related Question: Align denominator of fraction to left. –  Peter Grill Dec 26 '12 at 17:07
@devendra: I would recommend not deleting the question (but perhaps others disagree). Am sure others will come up with a similar question in the future (see Related Question I linked to) and if the answer is that is a bad idea to do this that may save them the time to post another question. Knowing what not to do is usually a good thing to know. –  Peter Grill Dec 26 '12 at 17:13

As illustrated in the highest-voted answer to Align denominator of fraction to left, \hfill will do the trick.

Here's a macro, \myfrac, that puts the \hfill either in the numerator or the denominator as necessary

\newcommand{\myfrac}[2]{%
\setbox0\hbox{$#1$}        % put the numerator in box0
\dimen0=\wd0               % measure box0
\setbox1\hbox{$#2$}        % put the denominator in box1
\dimen1=\wd1               % measure box1
\ifdim\wd0<\wd1            % if box0 is narrower than box1
\dfrac{#1\hfill}{#2}   % put \hfill in the numerator
\else
\dfrac{#1}{#2\hfill}   % otherwise put \hfill in the denominator
\fi
}


Complete MWE

\documentclass{article}
\usepackage{amsmath}

\newcommand{\myfrac}[2]{%
\setbox0\hbox{$#1$}        % put the numerator in box0
\dimen0=\wd0               % measure box0
\setbox1\hbox{$#2$}        % put the denominator in box1
\dimen1=\wd1               % measure box1
\ifdim\wd0<\wd1            % if box0 is narrower than box1
\dfrac{#1\hfill}{#2}   % put \hfill in the numerator
\else
\dfrac{#1}{#2\hfill}   % otherwise put \hfill in the denominator
\fi
}
\begin{document}
\begin{itemize}
\item[Original] $\dfrac{(x_i - x)^{N+1}}{(N+1)!}$
\item[Test 1] $\myfrac{(x_i - x)^{N+1}}{(N+1)!}$
\item[Test 2] $\myfrac{(N+1)!}{(x_i - x)^{N+1}}$
\end{itemize}
\end{document}

-
Wouldn't \newcommand{\myfrac}[2]{\dfrac{#1\hfill}{#2\hfill}} produce the same (horrible) output? –  egreg Dec 26 '12 at 23:55
@egreg ah, yes, of course :) My code is unnecessarily complicated- thanks for catching :) –  cmhughes Dec 27 '12 at 1:54