Jupytexmagic#

This is a python package for rendering LaTeX in Jupyter notebook with native rendering capabilities. Jupyter uses MathJax to render LaTeX inside of Markdown cells, which is fine for most use cases; however, this does not provide the full set of tools that LaTeX has to offer (e.g. tikzpictures, complex figures). What we want is the ability to write content using Jupyter for dynamic content while not sacrificing the beauty of native LaTeX.

Here are a couple of links related to the project:

How Does it Work?#

This is a custom ipython magic plugin which allows users to create a new code cell, placing %%tex at the top and then put all of their LaTeX source code afterwards. Running the cell compiles the code into .svg format which is displayed following the cell.

Jupytex uses pdflatex to compile your cell’s code into PDF format, then pdfcrop to crop the content, and pdf2svg to convert the PDF to .svg format. Then this is displayed using the IPython module. The compilation is fast, and has the upside of your LaTeX not needing to be rendered with Javascript when viewed on the web.

The .svg format is great because it doesn’t lose any definition upon zooming in on the compiled LaTeX and you can highlight text with your cursor, which you cannot do with normal image formats like PNG/JPEG.

See the Getting_Started section of this chapter to see example runs!

Usage#

  1. run pip install jupytexmagic

  2. create a new cell with import jupytexmagic and %load_ext jupytexmagic

  3. for tex cells, place %%tex at the top of your cell

  4. Currently the document class is standalone with variable height \documentclass[varwidth, margin=1in]{standalone} and has page numbers turned off \pagenumbering{gobble} but other than that, you can fully customize the output. This is needed so that the rendering of the svg is continous similar to how Jupyter renders markdown.

If your tex compilation fails, you can use the %%texdebug magic command to display the output of pdflatex when compiling for debugging purposes.

Current Limitations#

For some reason, the compilation does not work properly with JupyterNotebook; however, it works just fine with JupyterLab. Currently uploading the source .ipynb files ruins the view on GitHub, but displaying and building inside a Jupyter Book works properly (see images attached in [example run screenshots](#Example Run Screenshots)

Writing LaTeX that compiles to less than one line of text will be scaled to maximum width. This is a fair trade for having the output svg output be standardized for the majority of use cases.