I'd like to generate an image with math formulas with TeX from command line. Something like:

$ tex-executable "E=\frac{m_1v^2}{2}" e2.png

I have MacTex 2009 installed and I'm pretty sure that something like that already exists in this distribution. If not please suggest me a tool.

  • Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question.
    – Werner
    Nov 8, 2011 at 2:17

6 Answers 6


Update: 2012-01:

The standalone class now has a varwidth option, so with the current release, one would simply use the [varwidth] package option:

    \def\formula{E = m c^2}
\[ \formula \]

Save the following as formula.tex:

    \def\formula{E = m c^2}
\[ \formula \]

The standalone class is used for cropping. The varwidth environment is similar to the minipage environment, but sets its width to match the narrower natural width based on the content.

The ifdefined...\fi is only necessary so that this file can be compiled by itself, which is useful to test to ensure that there are no errors in it, in case you decide to make changes to it.

Then you can process this file as:

pdflatex "\def\formula{E=\frac{m_1v^2}{2}}\input{formula.tex}"
convert -density 300 formula.pdf -quality 90 formula.png

where convert is part of ImageMagick. This yields the following PNG file:

enter image description here

I have not used it, but the current development version of standalone can generate the PNG directly if you use


Alternatively you could also use GIMPas per this link from Wikibooks.

  • when using standalone for direct png production, -shell-escape option must be supplied to the pdflatex command. Mar 28, 2021 at 8:57

There are a few tools called tex2png. You should be able to use one of them for this purpose:


If you run into portability issues and require an online version, you could always use the Online LaTeX Equation Editor. It allows for exporting equations (display or inline) in a variety of formats and resolutions:

  • .gif
  • .png
  • .pdf
  • .swf
  • .emf
  • .svg

None of the answers above satisfied me, so I wrote pnglatex. It works on Linux and internally uses latex and dvipng programs. It is as easy as:

./pnglatex -f "(a+b)^2=a^2+b^2+2ab"

Other options are -b <color> to change background color (valid colors are White, Emerald, Transparent, ...); -d for display math environment; -o <output-file-name> to specify the name of the output image; -p <package1:package2:...:packagen> to use LaTeX packages (packages are separated by colons); -s <font-size> to set the font size.

A complex example could be

./pnglatex -b Transparent -d -p amsmath -o ~/formula.png -s 10 -f "\mathit{ws}_i\cap\mathit{ws}_j\neq\emptyset"

Since it is a script remember to set the execution bits before use, as in:

sudo chmod +x pnglatex

As you have explicitly asked for a command-line solution this is basically a non-answer. However, with respect to others looking for a "how to quickly get a PNG out of a LaTeX equation" solution it belongs here:

For Mac OSX there is the LaTeXit LaTeXit tool, which provides quick typesetting, a history (very useful for later edits) and lets you copy the result in various formats (including PNG) into the clipboard.

enter image description here


From the question it appears that the wanted end result would be a PNG raster image.

To that end, Mac OS X command sips (Scriptable Image Processing System) can be of help in converting the XeTeX (or, xdvipdfmx) generated PDF file into a PNG file. sips comes with OS X so you don't need to install anything extra. Because of formatting concerns in this here webpage, I'll use a shell heredoc, but it could be just as well all be on the same line:

xetex <<EOF
\advance\hoffset by-1in % default is one inch away, so remove that
\advance\voffset by-1in % ditto
\setbox0\hbox{$\displaystyle % set into a box so we can measure its dimensions
E={m_lv^2\over2} % <<< your equation here
\special{papersize=\the\wd0,\the\dimexpr\ht0+\dp0} % set the papersize to the box size
\box0 % actually typeset the box
sips -s format png texput.pdf --out texput.png

Or as a one-liner, this time outputting a JPG file (you will need to remove the line-breaks manually):

xetex '\advance\hoffset by-1in\advance\voffset by-1in\setbox0
       \hbox{$\displaystyle E={m_1v^2\over2}$}\special{papersize=\the\wd0,
       \the\dimexpr\ht0+\dp0}\box0\bye' && 
       sips -s format jpeg texput.pdf --out texput.jpg

As Peter mentions in a comment, both of the above will crop the resulting PDF file (but not the PNG file), so if you need the PDF file as well, you could change the \voffset reduction to -.99in and add to the papersize height calculation a +.01in.

  • In the PDF version I notice that the formula is cut off (Also, you missed the =). Feb 7, 2012 at 16:58
  • @Peter: Welp, I see no cut off in the png, but thanks for the missing =
    – morbusg
    Feb 7, 2012 at 21:02
  • Problem is in the PDF (not png). Also as it is now formatted it is "cut-and-pastable" into a Unix shell. Feb 7, 2012 at 21:13

You must log in to answer this question.