LaTeX MWEs in Jupyter Notebook for faster prototyping and sharing
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
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.
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.
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.
test = mwe.mwe(body, preamble=preamble)
test.show()
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.
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: |
|
---|---|
Returns: | An |
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.