A simple python library for converting latex snippets to images.
Project description
tex2image
This is a simple library to generate images from tex snippets.
It uses the standalone latex package to
produce PDFs that are already set to an appropriate size, and then uses
poppler to convert the PDF to an image.
You must make sure that pdflatex (available from TeX Live) and pdftoppm
(available in many package managers, and usually distributed as part of
poppler-utils) are available for python to execute.
Quick start
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tex2image-1.0.0.tar.gz.
File metadata
- Download URL: tex2image-1.0.0.tar.gz
- Upload date:
- Size: 351.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
881bb42679f63b5cc40442c4b917c664b9364d6134dcbe4186f580a57b708c88
|
|
| MD5 |
0cf667228241457998f7307eef44a814
|
|
| BLAKE2b-256 |
11da4afdb522bfa9410c3ad6caf3aff86511844f3631002ce80afbd2a7d92917
|
File details
Details for the file tex2image-1.0.0-py3-none-any.whl.
File metadata
- Download URL: tex2image-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1cfa2d397444ffd73311be2385fabf29730ae1588164a5b06477d4625bc341e2
|
|
| MD5 |
91bc9c86dddd32d231cf9c7931ad4d8a
|
|
| BLAKE2b-256 |
dc52c58a784edef8888b1029c68133a40ef8148cad48a9193f3978e83d802a42
|