Skip to content

Quickstart

Direct usage

Once you have installed pdflatex and pdftoppm, you can use the library like so:

from pathlib import Path
from tempfile import TemporaryDirectory

from tex2image import latex_to_png

with TemporaryDirectory() as temp_dir:
    image_file_path = latex_to_png("Pythagorean Theorem: $a^2 + b^2 = c^2$.", Path(temp_dir))
    # Do something with the image here, before temp_dir gets deleted...

Client / server method

If you would like to install the pdflatex and pdftoppm dependencies in a container, then you may wish to use the simple FastAPI server included in this library. To launch the server, run

pip install tex2image[server]
tex2image-server
# Or run `tex2image-server --help` to see available options.

Alternatively, this library distributes an official Docker image:

docker run --rm -it ghcr.io/olympiad-bot/tex2image

Both methods will serve a FastAPI server: navigate to [host]:[port]/docs (localhost:8000/docs by default) for OpenAPI documentation for the server.

The server can be easily interacted with from python by using the tex2image.client module. For example:

from tex2image import TexRenderingClient

c = TexRenderingClient()
image_bytes = c.latex_to_png("Pythagorean Theorem: $a^2 + b^2 = c^2$.")

For some more useful examples, see the examples folder; in particular, for examples of using the client, see the Discord bot example and the Flask app example.