Skip to Content
Essential Math for Data Science
book

Essential Math for Data Science

by Thomas Nield
May 2022
Intermediate to advanced content levelIntermediate to advanced
352 pages
9h 15m
English
O'Reilly Media, Inc.
Content preview from Essential Math for Data Science

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)

print(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
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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Data Science Handbook, 2nd Edition

Python Data Science Handbook, 2nd Edition

Jake VanderPlas

Publisher Resources

ISBN: 9781098102920Errata PageSupplemental Content