Preparation
We will use this file named texfile.tex
as the .tex to convert:
1 2 3 4 5 6 |
\documentclass[12pt]{article} \pagenumbering{gobble}% Remove page numbers (and reset to 1) \begin{document} My first \TeX~document. $s_n = (Ta)_n = \displaystyle\sum_{k=0}^\infty T_{nk} a_k$ \end{document} |
Result file

Generating PNG images
Packages you need:
- texlive (or any other TeX program, I guess)
- dvipng
Step by step
- In the Terminal, go to the directory containing the .tex file.
- Execute this:
1$ latex texfile.tex
- Output:
123456789101112This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2015/dev/Debian) (preloaded format=latex)restricted \write18 enabled.entering extended mode(./texfile.texLaTeX2e <2014/05/01>Babel <3.9l> and hyphenation patterns for 2 languages loaded.(/usr/share/texlive/texmf-dist/tex/latex/base/article.clsDocument Class: article 2014/09/29 v1.4h Standard LaTeX document class(/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (./texfile.aux) [1] (./texfile.aux) )Output written on texfile.dvi (1 page, 584 bytes).Transcript written on texfile.log.
- Output:
- Convert to PNG at 150dpi (to get a larger image than default):
1$ dvipng texfile.dvi -D 150 -o texfile.png
- Output:
12This is dvipng 1.14 Copyright 2002-2010 Jan-Ake Larsson[1]
- Output:
- Remove the border white spaces of the page and add some borders using imagemagick:
1$ convert texfile.png -trim -bordercolor white -border 5x5 texfile_trimmed.png
- Your file is ready as
texfile_trimmed.png
.
Script
I have made a little script to automatize the conversion, with auto-remove of intermediary files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
if latex $1 then BASENAME=${1%%.*} echo Base name is: $BASENAME dvipng $BASENAME.dvi -D 150 -o $BASENAME.png OUTPUT_FILENAME="${BASENAME}_trimmed.png" echo Output file name is: $OUTPUT_FILENAME convert $BASENAME.png -trim -bordercolor white -border 5x5 $OUTPUT_FILENAME if test $# = 2 then if test $2 != "-keeptempfiles" then rm ${BASENAME}.{aux,dvi,log,png} fi else rm ${BASENAME}.{aux,dvi,log,png} fi fi exit 0 |
Usage
- Save the above script as
tex2png
, anywhere you want, usually near your .tex files for convenience. - Make it executable:
$ chmod +x tex2png
. - Syntax:
tex2png filename [-keeptempfiles]
.- Example:
$ tex2png texfile.tex
. - Alternatively, you can omit the .tex extension:
$ tex2png texfile
but I would not recommend it. - If you want to keep the intermediary files (dvi, log, etc.), add the
-keeptempfiles
argument at the end:$ tex2png texfile.tex -keeptempfiles
.
- Example:
- The final image is named
texfile_trimmed.png
Generating SVG images
Packages you need:
- texlive
Step by step
- In the Terminal, go to the directory containing the .tex file.
- Execute this:
1$ latex texfile.tex
- Output:
123456789101112This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2015/dev/Debian) (preloaded format=latex)restricted \write18 enabled.entering extended mode(./texfile.texLaTeX2e <2014/05/01>Babel <3.9l> and hyphenation patterns for 2 languages loaded.(/usr/share/texlive/texmf-dist/tex/latex/base/article.clsDocument Class: article 2014/09/29 v1.4h Standard LaTeX document class(/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (./texfile.aux) [1] (./texfile.aux) )Output written on texfile.dvi (1 page, 584 bytes).Transcript written on texfile.log.
- Output:
- Convert to a SVG image with a 1.5 scale factor:
1$ dvisvgm texfile.dvi -TS1.5 --no-fonts - Your file is ready as
texfile.svg
.
Remarks
There is no padding in the SVG method. I have not found yet a method to add a little padding around the figure.
Also, the --no-fonts
is necessary to prevent rendering issues about SVG fonts, especially with Firefox and the default SVG viewer of Debian. So the generated formula will not be selectable as a text, but at least, it will render correctly on every device with SVG support.
Background
I needed to include some nice looking formulas on my new game design wiki. I am using MediaWiki but the math add-on was not working on my server (web hosting service, not mine). So I decided to create myself the images offline and upload them as regular image files, so that I don’t need to think too much about trying to set the add-on without having all the privileges I could have on a personal server.