Skip to content

Logo of Jupyter-TikZ

IPython Magics for rendering TeX/TikZ in Jupyter Notebooks

Read the Docs PyPI - Version Pypi - Downloads License


%%tikz
\begin{tikzpicture}
    \draw[help lines] grid (7, 5);
     \filldraw [color=red, opacity=0.3] (2.5,2.5) circle (1.5);
     \filldraw [color=cyan, opacity=0.3] (4.5,2.5) circle (1.5);
\end{tikzpicture}
A dot and a gridline

pip install jupyter-tikz

Getting started

Jupyter TikZ is an IPython Cell and Line Magic for rendering TeX/TikZ outputs in Jupyter Notebooks.

Basic Usage

To begin, load the jupyter_tikz extension:

%load_ext jupyter_tikz

Use it as cell magic, it executes the TeX/TikZ code within the cell:

%%tikz
\begin{tikzpicture}
    \draw[help lines] grid (5, 5);
    \draw[fill=black!10] (1, 1) rectangle (2, 2);
    \draw[fill=black!10] (2, 1) rectangle (3, 2);
    \draw[fill=black!10] (3, 1) rectangle (4, 2);
    \draw[fill=black!10] (3, 2) rectangle (4, 3);
    \draw[fill=black!10] (2, 3) rectangle (3, 4);
\end{tikzpicture}

Conway example

Or use it as line magic, where the TeX/TikZ code is passed as an IPython string variable:

%tikz "$ipython_string_variable_with_code"

Additional options can be passed to the magic command:

%%tikz -i -t=pgfplots -nt -S=docs/assets/quadratic -r --dpi=150
\begin{axis}[
  xlabel=$x$,
  ylabel={$f(x) = x^2 + 4$}
]
    \addplot [red] {x^2 + 4};
\end{axis}

Quadratic formula

Going further, it is also possible to use it as a Python package:

from jupyter_tikz import TexFragment

tikz_code = tex_template_code = r"""\begin{tikzpicture}
    \draw[help lines] grid (5, 5);
     \filldraw [color=orange, opacity=0.3] (2.5,2.5) circle (1.5);
\end{tikzpicture}"""

tikz = TexFragment(tikz_code)  # Create the tex template object

tikz.run_latex()  # Run LaTeX and shows the output

Orange dot in a grid

Next steps

Choose the following links to continue your journey:

Explore additional resources and related links for this project:

Contribute

Contributions are welcome from everyone! Whether you're reporting bugs, submitting feedback, or actively improving the codebase, your involvement is valuable. Here's how you can contribute:

  1. If you encounter any issues or have suggestions for improvements, please report them using the issues page.
  2. If you're interested in developing the software further, please refer to development guide.

Thanks

I had been using ITikZ for years. However, it doesn't update often and relies on the outdated pdf2svg for converting PDFs to images, which causes problems in Windows environments. Inspired by ITikZ and IPython TikZ Magic, I decided to create my own package, adding new features such as support for preambles, new Jinja syntax, and the ability to save the LaTeX result to IPython variables. I also switched from pdf2svg to Poppler, which works perfectly on all plataforms, including Windows.

License

Copyright 2024 © Lucas Lima Rodrigues.

Distributed under the terms of the MIT License, Jupyter-TikZ is free and open-source software.