4

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}
devendra
  • 2,818

1 Answers1

4

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}
cmhughes
  • 100,947
  • 3
    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 01:54