0

The fraction \frac{num}{den} are usually rendered by centering the numerator and the denominator.

Unfortunately, I find the result in the following MWE quite ugly:

\documentclass{article}

\begin{document}

If $r\neq 1$, then 

\[
\sum_{k=0}^nr^k = \frac{1-r^{n+1}}{1-r}
\]

\end{document}

Indeed, the $r^{n+1}$ part is too big. I wish that the two - signs of a fraction of the form \frac{a-b}{c-d} to be vertically aligned (whatever the size of a,b,c and d) at the center of the fraction, in order to show the symmetry of the formula.

How can I get such a result?

Note: if it matters for the answer, I would like the alignment to work also for inline maths mode.

Taladris
  • 328
  • 3
    Unrelated point 1, \displaystyle isn't a command, it's a switch, like {\bfseries foo} In its role as a switch it will almost never need braces, but if it did need them, they would have to be like {\displaystyle }. That's the general rule for switches anyway, although I'm not sure \displaystyle would be very happy with you if you tried to scope it like this, I haven't tried. Unrelated point 2, \[ ... \] enters display math mode by definition, so \displaystyle is completely redundant – Au101 Oct 10 '16 at 01:44
  • What do you mean by working in non-maths mode? You can't use \frac{}{} in text mode so no solution can possibly be applicable there. – cfr Oct 10 '16 at 02:22
  • @au101: thanks a lot. I had been taught the wrong way. I edited the code. – Taladris Oct 10 '16 at 02:45
  • @cfr: I meant inline. – Taladris Oct 10 '16 at 02:45

3 Answers3

3

Do you mean something like this?

possibility

That looks pretty ugly to me, but they say beauty is in the eye of the beholder ...

\documentclass{article}

\begin{document}

  If $r\neq 1$, then
  \[
  \sum_{k=0}^nr^k = \frac{1-r^{n+1}}{1-r^{\phantom{n+1}}}
  \]

\end{document}
egreg
  • 1,121,712
cfr
  • 198,882
  • That's what I am looking for. Maybe with some space on the left, so that the minus signs have a central position. Is there a way I can make it automatic? A a macro \diffratio{a}{b}{c}{d}? – Taladris Oct 10 '16 at 02:50
  • I find ”ugly“ too weak for describing that output. – egreg Oct 10 '16 at 06:24
  • 1
    The problem with this layout is that it looks like the printer has accidentally left out an index from the denominator. – Thruston Oct 10 '16 at 08:43
3

If you really think this is what you want, here's the \diffratio macro:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\diffratio}[4]{%
  \frac
    {\diffratio@left{#1}{#3}-\diffratio@right{#2}{#4}}
    {\diffratio@left{#3}{#1}-\diffratio@right{#4}{#2}}%
}

\newcommand{\diffratio@left}[2]{%
  \mathpalette\diffratio@left@i{{#1}{#2}}%
}
\newcommand{\diffratio@right}[2]{%
  \mathpalette\diffratio@right@i{{#1}{#2}}%
}
\newcommand\diffratio@left@i[2]{%
  \diffratio@measure{#1}{#2}%
  \hb@xt@\dimen@{\hss\box\z@}%
}
\newcommand\diffratio@right@i[2]{%
  \diffratio@measure{#1}{#2}%
  \hb@xt@\dimen@{\box\z@\hss}%
}
\newcommand\diffratio@measure[2]{%
  \sbox\z@{$\m@th#1\@firstoftwo#2$}%
  \sbox\tw@{$\m@th#1\@secondoftwo#2$}%
  \dimen@=\wd\z@
  \ifdim\wd\tw@>\dimen@ \dimen@=\wd\tw@\fi
}
\makeatother

\begin{document}

\begin{gather*}
\diffratio{1}{x^{n+1}}{1}{x} \qquad \frac{1-x^{n+1}}{1-x} \\
\diffratio{a}{bc}{xy}{z} \qquad \frac{a-bc}{xy-z}
\end{gather*}

\end{document}

enter image description here

I have no doubt whatsoever that the normal \frac way is better.

egreg
  • 1,121,712
  • Aesthetically the normal way is of course better. I'm even struggling to think of a teaching situation where this would be useful. And what happens (more a question for the OP, but also what happens with this the code) if you have \frac{a-b}{a-b-c}? – Chris H Oct 10 '16 at 10:34
  • @ChrisH There's no natural alignment possible in your example. – egreg Oct 10 '16 at 10:57
  • I thought you'd say that. And I see you've put the onus on the user to make the decision as to where to break it – Chris H Oct 10 '16 at 11:06
1

If you really want the two to line up, you need to add \phantom as a space filler.

\documentclass{article}
\usepackage{mathtools}% for \mathrlap

\begin{document}

If $r\neq 1$, then 

\[
\sum_{k=0}^nr^k = \frac{1-r^{n+1}}{1-r^{\phantom{n+1}}}
 = \frac{1-r^{\mathrlap{n+1}}}{1-r} \phantom{\scriptstyle n+1}\textrm{with added white space}
\]

\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120