!rm -rf latexify_py
!git clone https://github.com/odashi/latexify_py -b develop
!pip install -e latexify_py
# Before running following cells, restart the runtime so that the installed
# module is activated.
import math
import latexify
@latexify.with_latex
def solve(a, b, c):
return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)
print(solve(1, 4, 3))
print(solve)
print()
solve
-1.0
\operatorname{solve}(a, b, c) \triangleq \frac{-b + \sqrt{b^{2} - 4ac}}{2a}
\(\operatorname{solve}(a, b, c) \triangleq \frac{-b + \sqrt{b^{2} - 4ac}}{2a}\)
@latexify.with_latex
def sinc(x):
if x == 0:
return 1
else:
return math.sin(x) / x
print(sinc)
\operatorname{sinc}(x) \triangleq \left\{ \begin{array}{ll} 1, & \mathrm{if} \ x=0 \\ \frac{\sin{\left({x}\right)}}{x}, & \mathrm{otherwise} \end{array} \right.
\[\operatorname{sinc}(x) \triangleq \left\{ \begin{array}{ll} 1, & \mathrm{if} \ x=0 \\ \frac{\sin{\left({x}\right)}}{x}, & \mathrm{otherwise} \end{array} \right.
\]
# Elif or nested else-if are unrolled.
@latexify.with_latex
def fib(x):
if x == 0:
return 1
elif x == 1:
return 1
else:
return fib(x-1) + fib(x-2)
print(fib)
\operatorname{fib}(x) \triangleq \left\{ \begin{array}{ll} 1, & \mathrm{if} \ x=0 \\ 1, & \mathrm{if} \ x=1 \\ \operatorname{fib}\left(x - 1\right) + \operatorname{fib}\left(x - 2\right), & \mathrm{otherwise} \end{array} \right.
\[\operatorname{fib}(x) \triangleq \left\{ \begin{array}{ll} 1, & \mathrm{if} \ x=0 \\ 1, & \mathrm{if} \ x=1 \\ \operatorname{fib}\left(x - 1\right) + \operatorname{fib}\left(x - 2\right), & \mathrm{otherwise} \end{array} \right.
\]