[Tex/LaTex] Aligned numerator and denominator in \frac or any other variant

fractionsmath-mode

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

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}

Best Answer

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
}

screenshot

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}