how to add more white space over symbols in a nested fraction? [duplicate]

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.

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}


• Great! I did not know about \mathstrut Dec 2 '17 at 9:17
• @marcotrevi Look at this: tex.stackexchange.com/questions/41185/… Dec 2 '17 at 9:20
• @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. Dec 2 '17 at 9:25

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

\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.

• @CarLaTeX And you might enjoy a simplified syntax for continued fractions: tex.stackexchange.com/a/43412/4427 Dec 2 '17 at 9:14
• Great! It's not super neat with other formulas though Dec 2 '17 at 9: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 2 '17 at 9:18
• @egreg Of course, a LaTeX3 solution must make an appearance! Dec 2 '17 at 9:30
• @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 2 '17 at 13:20

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

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