Created
July 28, 2022 01:16
-
-
Save tim-tx/8304836c92f50e212526e9953196b68b to your computer and use it in GitHub Desktop.
Example of building an entire latex document in a jupyter notebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "present-transsexual", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import tempfile\n", | |
"import os.path\n", | |
"import subprocess\n", | |
"from IPython.display import Image, display" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"id": "confidential-glass", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def latex(document, dpi=100):\n", | |
" with tempfile.TemporaryDirectory(prefix=\"texinpy_\") as tmpdir:\n", | |
" path = os.path.join(tmpdir, \"document.tex\")\n", | |
" with open(path, 'w') as fp:\n", | |
" fp.write(document)\n", | |
" subprocess.run([\"lualatex\", path], cwd=tmpdir)\n", | |
" subprocess.run([\"pdftocairo\", \"-singlefile\", \"-transp\", \"-r\", str(dpi), \"-png\", \"document.pdf\", \"document\"], cwd=tmpdir)\n", | |
" im = Image(filename=os.path.join(tmpdir, \"document.png\"))\n", | |
" display(im)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"id": "tropical-geneva", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAADIAAAAkCAYAAADLsGk3AAAAAXNSR0IB2cksfwAAAAlwSFlzAAAXEQAAFxEByibzPwAAAbFJREFUWMPt2c9LFGEYB/DPpBAlKQR1CCLCYA0L+guq2166BEngtWPgwUsXwbuIVxPPUVDHbkEdOnWIJTp0ShBS8KKgqXRIuzzCsOyu7exMOes8l3nneZ/vy3x5fs0zk+BQ+WVPnxDZPaNPpCJSEamInBIigxlx5/AQV/AOX8rokRoaOMAHfMKDMhJ5gY94hcsYwNBJCK9uOvvVsH+c0p0tY2cfi+t6SverjFXryP7gtJTfwahsRWO6Lr/jmMeluF/AZqwnsRXrOh5hFb8jpxpY7nB2FkzPyV4P+3qLvdkoxyMp3QCe43Ub72fBtEz2vIjcD/2tFpjz+ImpHDCFE/mMrx1wL7HdlANZMIUOVkO4g+8dbFZwAbd7wBRetWpxzv5xM3UqjLJgCieylorrdnK096MHTOFENqLTj3awuRG51egB80/K79PQ19r0qi0s5YDJrWpNhP2TJn2Ct3jTwssz+IbhHDBtiSTxYMkxhtewGIl3ETsxTL3HXCpMp3E3mtwu7sUbwLNU8jaHdreYXDzyt3IdN7vMwSyYwomc+Hmk+opSEamIVET+ryT66I9VX8gfE3Z32AQrzzAAAAAASUVORK5CYII=\n", | |
"text/plain": [ | |
"<IPython.core.display.Image object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"doc = r\"\"\"\n", | |
"\\documentclass[12pt,tikz]{standalone}\n", | |
"\\begin{document}\n", | |
"\\begin{tikzpicture}\n", | |
"\\node [draw] (A) {foo};\n", | |
"\\end{tikzpicture}\n", | |
"\\end{document}\n", | |
"\"\"\"\n", | |
"latex(doc, dpi=150)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "configured-opposition", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment