TeX2img (export to image)

Turning a formula or a figure into an image file is the thing LaTeX is worst at, for a blunt reason: LaTeX's unit of output is a page. Measure it and an A4 sheet is 595 × 842 pt, while the line of Gaussian integral sitting on it is 86 × 28 pt. More than 99 % of the difference is blank paper you do not want. All you meant to do was paste an equation into PowerPoint, Keynote, Word or a web page, and instead a fight with margins begins. This page covers the three tools that win that fight — the standalone class, which crops inside LaTeX; pdfcrop, which crops a finished PDF; and dvisvgm, which converts to SVG — plus TeX2img, the GUI application that wraps all of it.

The standalone class: cropping to the content inside LaTeX

The most direct answer is the standalone class. Write \documentclass{standalone} instead of \documentclass{article} and the page shrinks to the bounding box of the body, producing a one-page PDF cropped exactly to the content. Setting the Gaussian integral from the top of this page with border=2pt gives 85.8 × 28.0 pt — the paper you did not want is simply gone from the A4's 595 × 842 pt. The author is Martin Scharrer; TeX Live 2024 carries v1.3b (2022/10/10).

figure.tex
% pdflatex figure.tex  ->  a one-page PDF cropped to the formula
\documentclass[border=2pt]{standalone}
\usepackage{amsmath}
\begin{document}
$\displaystyle \int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}$
\end{document}

One widely copied option deserves to be retired here: \documentclass[preview]{standalone} changes nothing, because preview is standalone's default. Read the class file and preview is already true before option processing begins; the meaningful setting is the opposite one, crop. (Asking for crop turns preview off and switches to a cropping method that does not use the preview package — which is the escape hatch when preview misbehaves under XeLaTeX.) Margins go in border=: one value applies to all four sides, two mean "left-right / top-bottom", four mean left, bottom, right, top in that order. The default is 0.50001 bp, so even by default there is a hair of margin.

Where that preview comes from is quietly interesting. standalone does its cropping by loading the preview package — and preview.sty is part of AUCTeX / preview-latex, the very code that lets Emacs display a rendered formula inline in the buffer you are editing. Machinery built to cut a formula out of a document for an editor turns out to be exactly the machinery for cutting one out for a slide. TeX Live 2024 carries v13.3 (2024/01/17).

Two more standalone options are worth knowing. varwidth=<width> lets the body wrap onto more than one line, so you can render a multi-line caption (by default the content just keeps stretching along a single line). multi=<environment> cuts every tikzpicture in one file onto its own page, which is how people keep a whole figure collection in a single source and include only the page they need. And loading the standalone package — same name, in the main document — gives you \includestandalone{figure}, which can pull a figure in either as source or as an already-built PDF, so you can use whichever is faster while drafting and whichever is safest at submission.

pdfcrop: trimming a PDF you already have (mind the page number)

When you cannot touch the source, pdfcrop is the tool. Give it a finished PDF and it measures the margins on each page, trims them off and writes a new PDF. Use it for a co-authored draft whose \documentclass you are not allowed to change, or for a figure PDF someone else sent you. It ships with TeX Live; the author is Heiko Oberdiek and the version in TeX Live 2024 is 1.42 (2023/04/15). To leave a little air around the content, pass --margins "2", in big points.

terminal
pdfcrop page.pdf out.pdf              # trim all margins
pdfcrop --margins "2" page.pdf out.pdf # leave 2 bp on every side
pdfcrop --margins "5 2 5 2" a.pdf b.pdf  # left bottom right top
pdfcrop --hires --clip a.pdf b.pdf     # HiResBoundingBox, and clip the content

Knowing how it works makes its behaviour predictable. pdfcrop does not parse the PDF itself. It asks Ghostscript for the bounding box and then runs pdfTeX again (or XeTeX / LuaTeX, with --xetex / --luatex) to re-impose the page at the new paper size. Cropping a PDF, in other words, runs TeX. It will not work without Ghostscript installed, which is also why there is a --gscmd option to name the command.

And here is the mine everyone steps on. pdfcrop treats every mark on the page as content, so if a page number is sitting at the foot — as it is by default in article — the crop box stretches all the way down to include it. Crop an A4 page containing nothing but \[ E = mc^2 \] and you get 43 × 559 pt: the formula at the top, the folio at the bottom, and emptiness in between. Add \pagestyle{empty} and repeat, and it becomes 43 × 13 pt. Nine times out of ten, "I cropped it and it came out weirdly tall" is this.

Exporting SVG with dvisvgm, and whether to outline the text

For the web, or for a drawing application that understands SVG, dvisvgm is the main event. It converts DVI, EPS and PDF into SVG, ships with TeX Live, and has been developed by Martin Gieseking since 2005 (TeX Live 2024 carries 3.2.2). The most reliable route is to make a DVI with latex and hand that to dvisvgm. Feed it a single formula and, as it converts, it reports the real sizegraphic size: 81.5pt x 23.4pt — so you know on the spot how far you will need to scale it at the destination.

terminal
latex figure.tex                            # produce figure.dvi
dvisvgm --no-fonts --exact-bbox figure.dvi  # glyphs as paths, tight box
dvisvgm --font-format=woff2 figure.dvi      # keep real text, web font
dvisvgm --eps figure.eps                    # EPS input
dvisvgm --pdf figure.pdf                    # PDF input: needs gs or mutool

The fork in the road is whether to outline the text. With --no-fonts, glyphs are drawn as path elements, so the appearance reproduces perfectly even where the fonts are missing; the cost is that the text in the SVG can no longer be searched or selected, and the file grows somewhat. Keep it as text instead and you embed a font, with --font-format=woff2 for example. For mathematics on the web, outlining is the safe default; if the formula should be searchable along with the surrounding prose, embed the font. And when you want the crop to be exact, --exact-bbox computes the bounding box from the actual glyph outlines.

One caveat. The --pdf input mode does not work with dvisvgm alone. Interpreting a PDF needs either Ghostscript or MuPDF's mutool, and with neither available it stops at ERROR: can't retrieve number of pages from file. TeX Live does not bundle mutool, so if you want that route, install one of them — or simply take the DVI route instead. (The DVI converters page is the home for dvisvgm itself.)

When you need PNG: resolution and transparency

If the destination accepts a vector format — PDF, SVG, EPS — always choose it. The outlines survive any enlargement, so the strokes stay crisp in print and on a projector alike. You need raster (PNG, JPEG) when the destination will not take vectors, or when you want to guarantee the same appearance on someone else's machine. If you do go raster, the rule is to decide the resolution first: for something that will fill a projected slide, budget the equivalent of 300–600 dpi. Enlarging afterwards adds no information.

standalone has a convert option aimed at exactly this, producing the raster image as part of the same compile. Because it calls an external converter, -shell-escape is required. When you want a transparent background, choose PNG, and rather than trimming the margin to zero, leave a little border — that way the image settles naturally into the slide's background colour. JPEG uses compression designed for photographs, so on formulas and line art it leaves noise around the strokes; do not pick JPEG for mathematics.

TeX2img: compile, crop and export from one window

TeX2img is all of the above gathered into one application. Paste a fragment of mathematics or a figure into its window, choose an engine and an output format, press the button, and the compile, the crop and the export run as one motion. The tool came out of the Japanese TeX community: Yusuke Terada developed it through version 1.2, after which the Windows edition passed to Noriyuki Abe while Terada continued the macOS edition, the two developed independently. It is an application that runs on your own machine, not a web service, so whatever style files and fonts you have installed locally are available to it.

What it does inside is a combination of everything above. It compiles the fragment with the engine you pick (pdflatex, platex, uplatex, lualatex, xelatex and so on); for DVI-based routes like platex / uplatex it inserts dvipdfmx or dvips; and finally it uses Ghostscript (version 9 or later) to cut to the bounding box and write the chosen format. So a working TeX installation and Ghostscript are prerequisites. On Windows, producing SVG needs MuPDF as well — the same situation as dvisvgm --pdf asking for mutool in the previous section. Because you choose the engine yourself, a fragment containing Japanese mathematics or Japanese fonts comes out at your usual quality.

The range of output formats is wide: vector EPS, PDF, SVG (and SVGZ), EMF, and raster PNG, JPEG, GIF, TIFF, BMP. EMF is the vector format for pasting into Office on Windows and is available only in the Windows edition. Margins, resolution and background transparency are each configurable, and the GUI lets you adjust them against a live preview. For batch-converting many equations or wiring the tool into a build, there is a command-line version; on Windows, TeX2imgc.exe runs the GUI in CUI mode. Its form is: options first, then input and output files listed in pairs.

terminal
TeX2imgc.exe /transparent /resolution=600 equation.tex equation.png
TeX2imgc.exe /latex=uplatex /gs=gswin64c formula.tex formula.svg

Three details matter once you are actually using it. First, you can paste just the body, leaving out \documentclass — TeX2img wraps it in a template before compiling, so trying one line of mathematics does not require writing the scaffolding around it. You can equally hand it a complete fragment with its own preamble, an existing .tex file, or an already-finished PDF, PS or EPS to be turned into an image. Second, for PDF and SVG you choose whether to outline the text or keep it as real text — the same decision as dvisvgm --no-fonts in the previous section. Third, the macOS edition can paste the result straight into Illustrator, Word or PowerPoint, putting it on a slide without a file ever touching disk.

Let the destination decide: Word, PowerPoint, Keynote, the web, print

The choice of format is not a matter of taste but of what the destination will take. The table below maps that out. As a rule, try a vector format first and fall back to raster only where vectors are refused. And one thing that is easy to forget: an equation turned into an image can no longer be corrected. Anything that will keep changing — the equations in a paper you are still writing — should stay as LaTeX, and exporting should be reserved for what is settled, or for places LaTeX cannot go. Always keep the original .tex fragment.

FormatKindWhere it belongs
PDFvectorre-including into LaTeX, print, Keynote; the first choice
SVGvectorweb pages, and drawing apps such as Figma or Illustrator
EPSvectorlegacy print workflows and publishers that ask for EPS
EMFvectorWord and PowerPoint on Windows; TeX2img offers it only there
PNGrasterslides in general, chat, anywhere transparency matters; set the resolution first
JPEGrasteronly where photographs are involved; never for formulas or line art

To close, a division of labour. To stay inside a LaTeX document, use the standalone class — keeping each figure in its own file and pulling it in with \includestandalone is the standard practice. To trim a PDF whose source you cannot touch, use pdfcrop. When SVG is required, use dvisvgm. And when you want to try several versions and settle the format by eye, or to handle a Japanese-language setup through a GUI, TeX2img is the shortest path. For drawing the figures themselves, the TikZ page is the place; for putting the resulting images back into a document, the page on including images is.