So, it’s very simple to generate the LaTeX we want, now we just need a way of displaying that LaTeX in a dexy document. A recommended way of sharing content between dexy documents is to use JSON. JSON is widely available and is human readable for debugging. JSON objects map nicely to Python dictionaries, and dexy defines a from_json() method which you can use to easily parse JSON into their corresponding Python objects (dictionaries, lists, or whatever the JSON happens to map to).

So, in our Python script, let’s define a simple dict which contains the latex we want, referenced by a memorable name, and save this to a JSON file:

Now, we want to see how to generate the derivative and integral tables we started with. Instead of a dictionary letting us pick out individual examples, we’ll generate a list and store this list in JSON, then iterate over and display each element in the list in order.

We use two list comprehensions, the first generates a list of functions, the second returns the LaTeX for the expressions:

That’s how you can use SymPy, JSON and Dexy to get automated math into your documents. We’ve been displaying the LaTeX within a HTML document and using MathJax to render it, but of course you can just insert the LaTeX content into a .tex file and render it to a PDF.

Another option, besides using JSON, is to print the LaTeX content to STDOUT within your python scripts and then use dexy to insert the output into a document directly. For example: