I'm working with the python markdown package in Jupyter, and I can't find a way to in line evaluate variables when using them in markdown cells.
For example, my code cell would contain:
a = 1
b = 2
And my markdown cell using python markdown should be:
$\frac{{{a}}{{{b}}$
to produce
Is there a way to evaluate "a" and "b" so that I can put them into the LaTeX markdown cell? I'd like to use Jupyter notebooks to do step by step calculations for reports, so it's critical that I can substitute in variable values in the process of writing a notebook to pdf or a .tex file.
I've explored pylatex, but this also doesn't seem to have a math example using fractions (only classes for Matrices and Vectors).
You will need this extension.
The Python Markdown extension allows displaying output produced by the current kernel in markdown cells. The extensions is basically agnostic to the kernel language, however most testing has been done using Python.
For example: If you set variable a in Python
a = 1.23
and write the following line in a markdown cell:
a is {{a}}
It will be displayed as:
a is 1.23
As you know, the python markdown extension evaluates to {{a}}
the variable a
and prints it. The thing is the value inside the brackets is actually evaluated as a python expression! Since $\frac{{{a}}{{{b}}$
doesn't work, how about:
{{r'$\frac{'+str(a)+'}{'+str(b)+'}$'}}
This should evaluate to the python string r'$\frac{1}{2}$'
, which is the markdown content you inteded to write (which is actually latex :P)
Not as nice, but then again, if you use this a lot, you can make a python helper function in a previous cell for this (or anything else really) and call it:
def frac(a, b):
return r'$\frac{'+str(a)+'}{'+str(b)+'}$'
And then
This is markdown showing the fraction of {{a}} and {{b}}: {{frac(a, b)}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With