I am trying to generate LaTeX string expression for continued fractions in Jupyter Notebook.
for example, a given Python list x=[1,2,3,4,5] can be written as continued fraction:
Structure expression to generate this LaTeX fraction is \\frac{Numerator}{Denominator}
With Non-recursive code :
from IPython.display import display, Markdown
# Non-recursive:
def nest_frac(previous_expr, numerator_expr1, denominator_expr2):
return previous_expr + " + \\frac{"+ numerator_expr1 + "}{" + denominator_expr2 + "}"
# Cumbersome, error-prone
display(Markdown("$"+ \
nest_frac("1","1", \
nest_frac("2","1", \
nest_frac("3","1", \
nest_frac("4","1", "5") \
) \
) \
) \
+ "$") \
)
x = [1,2,3,4,5]
How to recursively generate expression provided a python list.

We can define the function
nest_frac_Ntakingxas an additional argument:If we need an output for
x=[1,2,3,4,5]we do:To get the markdown format we use :
Let's the
xsize to 10 to ensure that function is flexible :Output
And to get the markdown :
And we can easily re-set the function in a way to display directly the markdown format.