[latex] How to write LaTeX in IPython Notebook?

Yet another solution for when you want to have control over the document preamble. Write a whole document, send it to system latex, convert the pdf to png, use IPython.display to load and display.

import tempfile
import os.path
import subprocess
from IPython.display import Image, display

with tempfile.TemporaryDirectory(prefix="texinpy_") as tmpdir:
    path = os.path.join(tmpdir, "document.tex")
    with open(path, 'w') as fp:
        fp.write(r"""
        \documentclass[12pt]{standalone}
        \begin{document}
        \LaTeX{}
        \end{document}
        """)
    subprocess.run(["lualatex", path], cwd=tmpdir)
    subprocess.run(["pdftocairo", "-singlefile", "-transp", "-r", "100", "-png", "document.pdf", "document"], cwd=tmpdir)
    im = Image(filename=os.path.join(tmpdir, "document.png"))
    display(im)

enter image description here

Examples related to latex

How to write LaTeX in IPython Notebook? latex tabular width the same as the textwidth Add "Appendix" before "A" in thesis TOC Tools for making latex tables in R Beamer: How to show images as step-by-step images multiple figure in latex with captions Two statements next to curly brace in an equation Latex Remove Spaces Between Items in List How to change font size on part of the page in LaTeX? Inserting code in this LaTeX document with indentation

Examples related to jupyter-notebook

How to prevent Google Colab from disconnecting? Jupyter Notebook not saving: '_xsrf' argument missing from post How do I install Python packages in Google's Colab? What is the difference between Jupyter Notebook and JupyterLab? Importing .py files in Google Colab Display all dataframe columns in a Jupyter Python Notebook How to open local file on Jupyter? how to open Jupyter notebook in chrome on windows Change the Theme in Jupyter Notebook? Jupyter notebook not running code. Stuck on In [*]

Examples related to ipython

How to increase image size of pandas.DataFrame.plot in jupyter notebook? Importing .py files in Google Colab Selection with .loc in python IOPub data rate exceeded in Jupyter notebook (when viewing image) Purpose of "%matplotlib inline" Installing a pip package from within a Jupyter Notebook not working convert json ipython notebook(.ipynb) to .py file In which conda environment is Jupyter executing? How to make inline plots in Jupyter Notebook larger? %matplotlib line magic causes SyntaxError in Python script