3

I am writing some complicated formulas that involve several layers of fractions.

Example:

\begin{displaymath}
\scalebox{2.5}[3]%
{$
1+\frac{1^2}{2+\frac{3^2}{2+\frac{5^2}{2+\frac{7^2}{2+\frac{9^2}{2+\cdots}}}}}
$}
\end{displaymath}

but the visual result, although readable, could be improved with some white space over all the squared odd terms. How would I achieve that? The vertical scaling of \scalebox does not help since I guess that the spacing between the fraction line and the exponent under it is too close to zero.

Mico
  • 506,678

3 Answers3

10

Load the amsmath package and use its \cfrac ("continued fraction") macro:

enter image description here

\documentclass{article}
\usepackage{amsmath} % for \cfrac macro
\begin{document}
\begin{displaymath}
1+\cfrac{1^2}{2+\cfrac{3^2}{2+\cfrac{5^2}{2+\cfrac{7^2}{2+\cfrac{9^2}{2+\cdots}}}}}
\end{displaymath}
\end{document}

The main difference to a \dfrac-based solution is that, with \cfrac, the right-hand edge of the fractional expressions is nice and vertical, which is not the case if \dfrac is used repeatedly.

Mico
  • 506,678
  • 1
    @CarLaTeX And you might enjoy a simplified syntax for continued fractions: https://tex.stackexchange.com/a/43412/4427 – egreg Dec 02 '17 at 09:14
  • Great! It's not super neat with other formulas though – marco trevi Dec 02 '17 at 09:15
  • @marcotrevi - Please give an example or two of "other formulas" which don't look "super neat" when using \cfrac. (It's not easy to respond to a generic, non-specific comment...) – Mico Dec 02 '17 at 09:18
  • @egreg Of course, a LaTeX3 solution must make an appearance! – CarLaTeX Dec 02 '17 at 09:30
  • @Mico I got an undeserved tick, perhaps the OP intends to use \mathstrut in his original way of writing the formula... – CarLaTeX Dec 02 '17 at 09:38
  • 1
    @CarLaTeX - Thanks, but don't fret or worry about it. These things do tend to even out in the medium to long term. In the past, I'm sure that some of my answers received ticks when others were at least as deserving. :-) – Mico Dec 02 '17 at 13:20
3

Warning: Mico's solution is better (see below). With mine, the edges of the fractional expressions are not perfectly aligned on the right.

You could use \dfrac and \mathstrut:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
    \begin{displaymath}
        1+\dfrac{1^2}{2+\dfrac{3^{\mathstrut 2}}{2+\dfrac{5^{\mathstrut 2}}{2+\dfrac{7^{\mathstrut 2}}{2+\dfrac{9^{\mathstrut 2}}{2+\cdots}}}}}
    \end{displaymath}
\end{document}

enter image description here

CarLaTeX
  • 62,716
  • Great! I did not know about \mathstrut – marco trevi Dec 02 '17 at 09:17
  • @marcotrevi Look at this: https://tex.stackexchange.com/questions/41185/strutting-around-whats-the-difference-between-strut-mathstrut-and-vphantom – CarLaTeX Dec 02 '17 at 09:20
  • 2
    @marcotrevi Thank you for accepting my answer but Mico's one is better, see the end of the fraction lines, they are not perfectly aligned. – CarLaTeX Dec 02 '17 at 09:25
2

If you are printing more continued fractions in your document then you can define the macro \cfraction which gives you more compendious source:

\def\cfraction{\def\cfracE{}\cfracA}
\def\cfracA#1+#2{#1\ifx#2\relax +\cdots\cfracE \else 
   + \bgroup \xdef\cfracE{\cfracE\egroup} \strut #2\expandafter\cfracB \fi}
\def\cfracB#1\over{#1\over \displaystyle\cfracA}


$$
\cfraction 
1 + 1^2 \over 2 + 3^2 \over 2 + 5^2 \over 2 + 7^2 \over 2 + 9^2 \over 2 + \relax
$$

\bye
wipet
  • 74,238
  • 1
    There's no drifting of the fraction lines if you add \kern-\nulldelimiterspace after \egroup – egreg Dec 02 '17 at 11:01
  • OK, but the "drifting of fraction lines" is more aesthetic from my point of view. – wipet Dec 02 '17 at 11:31