# How to typeset a continued fraction in the following format?

How does one typeset the following continued fraction as shown below?

Source: Notes on continued fractions and recurrence sequences by Alfred van der Poorten.

Note: Alf van der Poorten's style is similar to Euler's (in E071).

Source: The Euler Archive, Index Number E71.

-
Interesting beta Euler uses there ... – Christian Jul 22 '13 at 14:02

This variant saves a vertical space. Because of the shortened line, there is many free space above the continued fractions that is used in this example. Also it keeps the math axis for the summations.

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\newcommand*{\cofrac}[2]{%
{%
\rlap{$\dfrac{1}{\phantom{#1}}$}%
\genfrac{}{}{0pt}{0}{}{#1+#2}%
}%
}
$a_0 + \cofrac{a_1}{ \cofrac{a_2}{ \cofrac{a_3}{ \genfrac{}{}{0pt}{0}{}{\ddots} }}}$
\end{document}


-

I don't know how to generate a continued fraction in the style you show. The amsmath package offers the \cfrac command, though, which can be used to generate the following expression -- which, I would argue, is substantially easier to parse than the one from van der Poorten's book.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
a_0+\cfrac{1}{a_1+\cfrac{1}{a_2+\cfrac{1}{a_3+\cdots}}}
\end{equation*}
\end{document}


-
I agree that it is easier to parse. Alf van der Poorten's style is similar to Euler's (in E071). – Américo Tavares Sep 19 '12 at 14:13
The TeXbook also shows a version which aligns the numerator to the left; i.e., \def\cof#1{\displaystyle{1\strut\hfill\over #1}} $$a_0 + \cof{a_1 + \cof{a_2 + \cof{a_3 + {\atop\ddots}}}}$$ \bye – morbusg Sep 19 '12 at 17:11
@morbusg That was used in the translation An Essay on Continued Fractions by Myra F. Wyman and Bostwick F. Wyman of Euler´s E71 De fractionibus continuis dissertatio. – Américo Tavares Sep 20 '12 at 13:52
\documentclass{article}
\usepackage{amsmath,mathtools}
\newcommand*{\underBracket}[2][1]{\underbracket[.6pt][0pt]{~#1\vphantom{g}~}_{\displaystyle #2\vphantom{g+1}}}

\begin{document}
a_0 + \underBracket{a_1\mathrlap{{}+ \underBracket{a_2\mathrlap{{} + \underBracket{a_3\mathrlap{{}+{}\raise-2.2ex\hbox{\smash\ddots}}}}}}} \hphantom{a_1 + a_2 + a_3 + {}} % to reset the spacing/aligning
\end{document}


-

Here is the solution I use.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyvrb}

\setlength{\parindent}{0cm}
\newcommand\latex{\verb}
\DefineVerbatimEnvironment{Latex}{Verbatim}{numbers=left,numbersep=2mm}

\renewcommand\quote[1]{"#1"}

% == PACKAGES USED == %

\usepackage{mathtools}
\usepackage{ifmtarg}

% == DEF : Continued Fractions == %

% Sources :
%    * http://tex.stackexchange.com/questions/68190/continued-fraction-in-inline-equations/68196#68196
%    * http://tex.stackexchange.com/questions/23432/how-to-create-my-own-math-operator-with-limits

\makeatletter
% Operator-like output
\def\contFracOpe{%
\operatornamewithlimits{%
\mathchoice{% * Display style
\vcenter{\hbox{\huge $\mathcal{K}$}}%
}{%           * Text style
\vcenter{\hbox{\Large $\mathcal{K}$}}%
}{%           * Script style
\mathrm{\mathcal{K}}%
}{%           * Script script style
\mathrm{\mathcal{K}}%
}
}
}

% Operation-like output
\newcommand\contFrac{\@ifstar{\@contFracStar}{\@contFracNoStar}}

\def\singleContFrac#1#2{%
\begin{array}{@{}c@{}}%
\multicolumn{1}{c|}{#1}%
\\%
\hline%
\multicolumn{1}{|c}{#2}%
\end{array}%
}

% No star version
\def\@contFracNoStar#1{%
% //\@nil is usefull if only one argument is given.
\mathchoice{% * Display style
\@contFracNoStarDisplay@#1//\@nil%
}{%           * Text style
\@contFracNoStarInline@#1//\@nil%
}{%           * Script style
\@contFracNoStarInline@#1//\@nil%
}{%           * Script script style
\@contFracNoStarInline@#1//\@nil%
}%
}

% No star version - Display style
\def\@contFracNoStarDisplay@#1//#2\@nil{%
\@ifmtarg{#2}{%
#1%
}{%
#1+\cfrac{1}{\@contFracNoStarDisplay@#2\@nil}%
}%
}

% No star version - Inline style
\def\@contFracNoStarInline@#1//#2\@nil{%
\@ifmtarg{#2}{%
#1%
}{%
#1 \@@contFracNoStarInline@@#2\@nil%
}%
}
\def\@@contFracNoStarInline@@#1//#2\@nil{%
\@ifmtarg{#2}{%
+ \singleContFrac{1}{#1}%
}{%
+ \singleContFrac{1}{#1} \@@contFracNoStarInline@@#2\@nil%
}%
}

% Star version
\def\@contFracStar#1{%
\mathchoice{% * Display style
% ////\@nil is usefull if only one argument is given.
\@contFracStarDisplay@#1////\@nil%
}{%           * Text style
% //\@nil is usefull if only one argument is given.
\@contFracStarInline@#1//\@nil%
}{%           * Script style
\@contFracStarInline@#1//\@nil%
}{%           * Script script style
\@contFracStarInline@#1//\@nil%
}%
}

% Star version - Display style
\def\@contFracStarDisplay@#1//#2//#3\@nil{%
\@ifmtarg{#2}{%
#1%
}{%
#1 + \cfrac{#2}{\@contFracStarDisplay@#3\@nil}%
}%
}

% Star version - Inline style
\def\@contFracStarInline@#1//#2\@nil{%
\@ifmtarg{#2}{%
#1%
}{%
#1 \@@contFracStarInline@@#2\@nil%
}%
}
\def\@@contFracStarInline@@#1//#2//#3\@nil{%
\@ifmtarg{#3}{%
+ \singleContFrac{#1}{#2}%
}{%
+ \singleContFrac{#1}{#2} \@@contFracStarInline@@#3\@nil%
}%
}
\makeatother

\begin{document}

\section{Unstarred version}

$\displaystyle \contFrac{u_0 // u_1 // u_2 // \dots // u_n}$

$\contFrac{u_0 // u_1 // u_2 // \dots // u_n}$

\section{Starred version}

$\displaystyle \contFrac*{a // b // c // d // e // f // \dots // y // z}$

$\contFrac*{a // b // c // d // e // f // \dots // y // z}$.

\section{Operator}

$\displaystyle \contFracOpe_{k=1}^{n} (b_k:c_k) = \cfrac{b_1}{\displaystyle \contFrac*{c_1 // b_2 // c_2 // b_3 // \dots // b_n // c_n}}$

\end{document}

-

you can modify the vertical spacing.

\documentclass{article}
\usepackage{mathtools}
\begin{document}
$a_0+ \raisebox{-.25\height}{\dfrac{1}{a_1\mathrlap{\,+}}}\mkern15mu \raisebox{-1.1\height}{\dfrac{1}{a_2\mathrlap{\,+}}}\mkern15mu \raisebox{-2.0\height}{\dfrac{1}{a_3\mathrlap{\,+}}}\mkern15mu \raisebox{-3\height}{\ddots}$
\end{document}


-

This is an alternative which comes closer to Euler's format. It creates the staggered appearance that is commonly used. The width of the finished fraction is the width at the widest point. To use, at the highest level use \cofrac{a}{b}{c} for a/(b+c) and then nest sub-levels using \cofracel{a}{b}{c}, e.g.

\cofrac{a}{b+}{\cofracel{aa}{bb+}{\cofracel{aaa}{bbb+}{\cofracdots}}}


The \cofracdots command adds diagonal dots.

It is probably not the most elegant LaTeX formulation.

% -----------------------------------------------------------------------------------
% Continued fractions for use in math environment
% Use \cofrac{a}{b}{c} for a/(b+c) at highest level
% Set c to a continued fractior to build recursive structure
% Use \cofracel{a}{b}{c} for recursive terms
% Matches line for fractions to correct size
% -----------------------------------------------------------------------------------
% Temporary variables
%
\newlength{\cfDeleted}
\newlength{\cfA}
\newlength{\cfB}
\newlength{\cfC}
\newlength{\cfX}
\newlength{\cfY}
% -----------------------------------------------------------------------------------
\newcommand{\cofrac}[3]
{
\setlength{\cfDeleted}{0pt}
\cofracel{#1}{#2}{#3}\; \hspace{\cfDeleted}
}
% ------------------------------------------------------------------------------------
\newcommand{\cofracel}[3]
{
\settowidth{\cfA}{$#1$}
\settowidth{\cfB}{$#2\;$}
\settowidth{\cfC}{$#3$}
% Calculate B + C and take max of B + C and A
\setlength{\cfY}{\cfB}
\setlength{\cfX}{\maxof{\cfY}{\cfA}}
% \cfX has "true" width of fraction - at least at this level
% Now get 1/2 (A + B + C) and take greater of that and A by itself
\setlength{\cfY}{0.5\cfY}
\setlength{\cfY}{\maxof{\cfY}{\cfA}}
% \cfY has shortened width of fraction
% calculate amount deleted
\setlength{\cfC}{\cfX}
% Promote to global here and save for later
\global\cfDeleted=\cfDeleted
\setlength{\cfX}{\cfDeleted}
% then draw .. this may include multiple additional recursions
{
\mathrlap{\cfrac{#1}{{#2\;}{#3}}}\hspace{\cfY}
}
% and restore the saved deleted
\global\cfDeleted=\cfX
}
% ------------------------------------------------------------------------------------
\newcommand{\cofracdots}{\genfrac{}{}{0 pt}{}{\phantom{1}}{\ddots}}
% End of continued fractions
% ---------------------------------------------------------------

-