# uncommon form of continued-fraction expression

I have a copy of the text from the book to do. But I have been working in latex for a month and I have a problem with remaking these continued fractions. My code is looking like this:

$\frac{n_{0}}{n_{1}}=q_{1}+\frac{1}{q_{2}+\frac{1}{q_{3}+\frac{1}{q_4+\ddots}}} & \\ & +\frac{1}{q_{k-1}+\frac{1}{q_{k}}.}$

\begin{equation*}
3,14159=3+\frac{1}{7+\frac{1}{1+\frac{1}{15+\frac{1}{1+\frac{1}{25+\frac{1}{1+\frac{1}{7+\frac{1}{4}.}}}}}}}
\end{equation*}


Do you know how to do it under and how to align it to the left side?

• Welcome to TeX.se Dec 22, 2021 at 15:34
• (it's better if you post the screenshot of the current code output.) Looks like you want some \phantom and \mathrlap. Dec 22, 2021 at 15:59
• Quite similar to How to typeset a continued fraction in the following format? - TeX - LaTeX Stack Exchange , although the alignment is slightly different (the + is aligned with the vinculum instead of the numerator) Dec 22, 2021 at 16:03
• Not an answer, but a question: from which book is it? It looks like a book from "Mała Biblioteczka Matematyczna". Dec 22, 2021 at 23:10

An array with a bit of visual formatting:

\documentclass{article}
\usepackage{amsmath,array}

\usepackage{lipsum}

\begin{document}

\lipsum[1-4]
$\setlength{\arraycolsep}{0pt} \newcommand{\md}[1.45]{\mathbin{\raisebox{-#1ex}[0pt][0pt]{\displaystyle#2}}} \md{\frac{n_0}{n_1}=q_1+{}} \begin{array}[t]{ *{9}{>{\displaystyle{\mathstrut}}c<{{}}} } 1 \\ \cline{1-3} \md{q_2} & \md{+} & 1 \\ \cline{3-5} & & \md{q_3} & \md{+} & 1 \\ \cline{5-7} & & & & \md{q_4} & \md{+} & \md{\ddots} \\ &&&&&&& \md{+} & 1 \\ \cline{9-9} &&&&&&& & q_{k-1} + \cfrac{1}{q_k} \end{array}$
\lipsum[1-4]
$\setlength{\arraycolsep}{0pt} \newcommand{\md}[1.45]{\mathbin{\raisebox{-#1ex}[0pt][0pt]{\displaystyle#2}}} \md{3{,}14159=3+{}} \begin{array}[t]{ *{13}{>{\displaystyle{\mathstrut}}c<{{}}} } 1 \\ \cline{1-3} \md{7} & \md{+} & 1 \\ \cline{3-5} && \md{15} & \md{+} & 1 \\ \cline{5-7} &&&& \md{1} & \md{+} & 1 \\ \cline{7-9} &&&&&& \md{25} & \md{+} & 1 \\ \cline{9-11} &&&&&&&& \md{1} & \md{+} & 1 \\ \cline{11-13} &&&&&&&&&& 7 & + & \cfrac{1}{4} \end{array}$
\lipsum[1-4]

\end{document} • Thank you so much for helping me, happy Christmas :) Dec 23, 2021 at 13:23

I'm sure there are easier ways, but this seems to get it done.

\documentclass{article}
\usepackage{amsmath}
\def\mywd{35pt}
\begin{document}
$\frac{n_0}{n_1} = q_1 + \dfrac{\makebox[\mywd][l]{1}} {\makebox[\mywd][l]{q_2 + \dfrac{\makebox[\mywd][l]{1}} {\makebox[\mywd][l]{q_3 + \dfrac{\makebox[\mywd][l]{1}} {\makebox[\mywd][l]{q_4 + \raisebox{-6pt}{\ddots} \raisebox{-12pt}{+\dfrac{\makebox[\mywd][l]{1\kern30pt}} {q_{k-1} + \dfrac{1} {q_k}}}}}}}}}$
\end{document} • Plus a slight point that OP 's dots are less sloped than ddots, but probably it doesn't matter. • that the + right after the ddots is interpreted as unary makes the spacing bad however. Dec 22, 2021 at 16:17
• To my eye the 1s are a little close to the horizontal line above them.
– Simd
Dec 23, 2021 at 6:53
• Thank you so much for helping me, happy Christmas :) Dec 23, 2021 at 13:24
• @Cloudy Likewise, a merry and blessed Christmas to you and yours! Dec 23, 2021 at 16:00
• @graffe That is a function of the font. For example, change to \usepackage{newpxmath} and see that dimension larger than before. Dec 23, 2021 at 16:02

First, you will want to align the 1 above the q_i. For this, you will need measure the width of every q_i and place the 1 manually. A helper macro is convenient:

\def\leftfrac#1#2#3#4{\frac % {#1} {#2#3#4}
{\setbox0=\hbox{$\displaystyle#2$}
\hbox to\wd0{\hfil$\displaystyle#1$\hfil}\hfill}
{\displaystyle#2#3#4}}

$\leftfrac 1 {q_i} + 1$ The next step is continuing the fraction without extending the rule. This is the trickiest part, because we want to preserve both the spacing between the rule and the 1 and the proper binary spacing around the +, while retaining the overall width and height of the fraction.

We solve this problem by following each fraction by two skips, both equalling the width of the part after q_i, but the first negative and the second positive. Then, inside the continued fraction, if we can somehow move out the second skip and place it behind the fraction, we should have all we need.

Expanding the previous macro:

\def\leftfrac#1#2#3#4{% #1 \over #2 #3 #4
% measure partial and full denominator
\setbox0=\hbox{$\displaystyle#2$}
\setbox2=\hbox{$\displaystyle#2#3#4$}
\frac
% a strut in the numerator ensures proper spacing
{\hbox to\wd0{\hfil
$\displaystyle \strut #1$
\hfil} \hfill}
% move the last skip of the denominator out of the fraction
{\displaystyle#2#3#4
\dimen0=\lastskip \unskip
\expandafter\egroup
\expandafter\hskip
\the\dimen0
\bgroup}
% two opposite skips (will cancel unless interfered with)
\hskip\dimexpr\wd0-\wd2
\hskip\dimexpr\wd2-\wd0 \relax
}

$\leftfrac 1 {q_1} + {\leftfrac 1 {q_2} + {\leftfrac 1 {q_3} + 1} } .$ I have placed a dot in the example to show you the horizontal width is the total width of all fractions. Note that the \strut is essential to get the proper vertical spacing around the ones.

Now as a final step, add the diagonal dots and the lower-right fraction. We have to measure the latter first; then we can use the fact that our new \leftfrac moves the last skip out of its fourth argument:

$\frac {n_0}{n_1} = q_1 + \leftfrac 1 {q_2} + {\leftfrac 1 {q_3} + {\leftfrac 1 {q_4} + { % lower the ddots a bit \setbox0=\hbox{\displaystyle\ddots} \lower5pt\box0 % measure the lower-right fraction \setbox0=\hbox{\displaystyle {} + \leftfrac 1 {q_{k-1}} + {\frac {\strut1}{q_k}}} \lower7pt\copy0 % give two skips for \leftfrac to shift \hskip-\wd0 \hskip+\wd0 } } }$


This gives you your equation in the form you requested: • Thank you so much for helping me, happy Christmas :) Dec 23, 2021 at 13:24
• Thank you! I have to say this is a beautiful way of typesetting continued fractions, I was quite glad to figure it out. Dec 25, 2021 at 11:19