mwe

LaTeX MWEs in Jupyter Notebook for faster prototyping and sharing

An MWE for mwe

I've written mwe as a python package, so currently you'll have to use it from the python API. A Jupyter "magic" API is coming soon.

First we have to import the package

In [1]:
import mwe

Then, we think about what we want in our preamble. For this MWE, we only need the package lipsum, which provides some latin dummy text to make it easy to write long documents.

In [2]:
preamble = "\usepackage{lipsum}"

Then, we'll have to define the actual body of the document. I want to show some of the possible things you could show.

In [3]:
body = """
    Hi!
    $$\\frac{x^{2}}{\int x dx}$$
    \lipsum[2-4]
"""

Note that we had to double escape the \\frac command because \f is a unicode character. This is one of the current problems with the package, and I'm looking for a solution.

Finally, we create our mwe object using the preamble and body, and we show it.

In [4]:
test = mwe.mwe(body, preamble=preamble)

test.show()
                    
\documentclass[letterpaper]{article}
%% preamble here 
\usepackage{lipsum}
\begin{document}


    Hi!
    $$\frac{x^{2}}{\int x dx}$$
    \lipsum[2-4]


\end{document}
Out[4]:
<mwe.mwe at 0x105dd9e90>

And that's it! The .pdf resides at {cwd}/mwe.pdf, and the .svg resides at {cwd}/mwe.svg. If you're interested in the TeX code, it's in /tmp/mwe.tex, but it may get deleted, depending on your system. All of the rest of the files should be cleaned up. Oh, and the html uses inline svg, so you can delete {cwd}/mwe.svg and it should still show up just fine.

Class Documentation

class mwe.mwe(text=None, texcls=’article’, texclsopts={‘letterpaper’: None}, preamble=None)[source]

Bases: object

mwe initializes a minimum working example for LaTeX.

mwe starts a latex document with changeable class, options, and premble. The text is then passed into the body of the document.

Parameters:
  • text – Valid LaTeX source code (be careful of escape characters)
  • texcls – LaTeX class name, default ‘article’
  • texclsopts – LaTeX options passed to the LaTeX class, as a dict. For keyword arguments, use {“keyword”: “arg”}, and for singular arguments, use {“argument”: None}.
  • preamble – Valid LaTeX source code
Returns:

An mwe object for additional editing or exporting.

add_to_body(text)[source]

adds valid LaTeX source into the body

Parameters:text – valid LaTeX source to add to the end of the current document body text.
export()[source]

compiles the LaTeX into an .svg

export calls pdflatex to convert the mwe into a .pdf and then uses pdf2svg to convert this into an svg. The LaTeX source is written into the /tmp directory, but the mwe.pdf and mwe.svg files will be compiled in the current directory.

show(alone=False)[source]

exports and shows the result of the mwe.

show will export the LaTeX mwe into an svg and show it below in the Jupyter notebook.

Parameters:alone – Whether to show only the result (True), or show it side by side with the LaTeX source (False). Default False.
show_alone()[source]

some docstring here.

show_side_by_side()[source]

shows the source code and the result of the mwe

show_side_by_side exports the mwe into an .svg, and then shows it in the Jupyter notebook as a side-by-side of the source code, highlighted with Pygments, and the .svg resulting page.

Indices and tables

Contents: