Skip to main content

Batch render LaTeX files to cropped SVG images

Project description

LaTeXclippings

Batch render LaTeX files to cropped SVG images.

Rendered LaTeX: I love LaTeX!

Rendered LaTeX: sum from 1 to n

Features

LaTeXclippings takes multiple LaTeX files, and an optional preamble, as input. Using your computer's LaTeX installation, it renders each input file as an SVG image or embeddable HTML <img> tag. All relevant LaTeX features are supported, including regular text, inline and display math, and tables.

Each rendered image is cropped, allowing it to be easily embedded in other content. When generating HTML <img> tags, additional CSS style rules are added, which adjust the SVG's scale and baseline to match the surrounding text. No more misaligned equations!

LaTeXclippings also parses the pdflatex log, matching up each input file with the corresponding log output. If any error messages are found, they are converted into informative Python exceptions, which identify the file and line responsible for an error.

Installation

Install from PyPI:

pip install latexclippings

Alternatively, simply download latexclippings.py.

Dependencies

LaTeXclippings requires Python 3.6+, and also uses:

  • pdflatex to generate PDFs from LaTeX source
  • inkscape for PDF cropping and SVG conversion

Usage

Command Line

The latexclippings command reads LaTeX files (or standard input) and outputs SVG or embeddable HTML. (If you did not install LaTeXclippings from PyPI, you can use ./latexclippings.py or python latexclippings.py instead.)

Using standard input and output:

$ # Kinetic energy formula (in classical mechanics).
$ echo '$ E_k = \frac{1}{2}mv^2 $' | latexclippings > kinetic-energy.svg

Rendered LaTeX: kinetic energy

Rendering multiple LaTeX files to HTML, using a custom preamble:

$ latexclippings --format html --preamble my-preamble.tex \
>         apple.tex banana.tex grape.tex
$ ls
apple.html  banana.html  grape.html  my-preamble.tex
apple.tex   banana.tex   grape.tex

The contents of apple.html (note the inline CSS styles for scaling and alignment):

<img style="display: inline-block; width: 21.38669ex;
height: 2.08727ex; vertical-align: -0.45000ex;"
alt="I enjoy eating apples." title="I enjoy eating apples."
src="data:image/svg+xml;base64, PHN2ZwogICB4bWxuczp...">

Rendered LaTeX: apples

Python API

For more sophisticated usage (e.g. integration into a static website generator), you can use the Python API.

from latexclippings import LatexFile

lf = LatexFile([r"This is some \LaTeX code.", r"Math: $ \sin(x) $"])
lf.render()

c = lf.clippings[0]
print(f"The first clipping is {c.width} by {c.height} ex, "
        + f"with {c.depth} ex below the baseline.")

with open("math.svg", "w") as f:
    f.write(lf.clippings[1].svg)

License

LaTeXclippings is licensed under the MIT License.

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

latexclippings-0.1.3.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

latexclippings-0.1.3-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file latexclippings-0.1.3.tar.gz.

File metadata

  • Download URL: latexclippings-0.1.3.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for latexclippings-0.1.3.tar.gz
Algorithm Hash digest
SHA256 cae583aee1928ff219fb75c9ff5957306acb2cd020f0b5040d9bd0c6a0fbcab1
MD5 9704cc92203e93dfede6e59386a18294
BLAKE2b-256 e8e920051b0d136cd1fd0fe270ccab1e91ff7e6fd272dfbcf7bae30d70e36586

See more details on using hashes here.

File details

Details for the file latexclippings-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: latexclippings-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for latexclippings-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1eddffa950c75574f8b2bbed8dc50f21d47013b34fc36801b82a49cd58ce56c8
MD5 f5c486859cced881c42a81ca7269ef0f
BLAKE2b-256 7be1babcd7c33ca84b4aa3ed4ce7d9e3b8eead436c445600b58f760ebe1ea7f4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page