Appendix A. Supplemental Topics
Using LaTeX Rendering with SymPy
As you get more comfortable with mathematical notation, it can be helpful to take your SymPy expressions and display them in mathematical notation.
The quickest way to do this is to use the latex()
function in SymPy on your expression and then copy the result to a LaTeX math viewer.
Example A-1 is an example that takes a simple expression and turns it into a LaTeX string. Of course we can take the results of derivatives, integrals, and other SymPy operations and render those as LaTeX too. But let’s keep the example straightforward.
Example A-1. Using SymPy to convert an expression into LaTeX
from
sympy
import
*
x
,
y
=
symbols
(
'x y'
)
z
=
x
**
2
/
sqrt
(
2
*
y
**
3
-
1
)
(
latex
(
z
))
# prints
# \frac{x^{2}}{\sqrt{2 y^{3} - 1}}
This \frac{x^{2}}{\sqrt{2 y^{3} - 1}}
string is formatted mathlatex, and there are a variety of tools and document formats that can be adapted to support it. But to simply render the mathlatex, go to a LaTeX equation editor. Here are two different ones I use online:
In Figure A-1 I use Lagrida’s LaTeX editor to render the mathematical expression.
![emds aa01](/api/v2/epubs/9781098102920/files/assets/emds_aa01.png)
Figure A-1. Using a math editor to view the SymPy LaTeX output
If you want to save the copy/paste step, you can append the LaTeX directly as an argument to the CodeCogs LaTeX editor URL as shown ...
Get Essential Math for Data Science now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.