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}
11
  • 2
    The numerator and denominator are horizontally centered with respect to one another; this is by far the most common typographic treatment of fractions.
    – egreg
    Commented Dec 26, 2012 at 16:43
  • 2
    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}}. Commented Dec 26, 2012 at 16:54
  • 2
    @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 … Commented Dec 26, 2012 at 17:01
  • 2
    Related Question: Align denominator of fraction to left. Commented Dec 26, 2012 at 17:07
  • 1
    @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. Commented Dec 26, 2012 at 17:13

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .