23

I am familiar with using LaTeX on online websites (like MathOverflow or Physicsforums). My input is rendered using MathJax. However, I do not understand the LaTeX environment in its native OS form. I have downloaded MacTeX, and all I would like to do is make tiny little .png or .jpg files that each contain one equation.

After some Googling, I have tried something along the lines of:

\documentclass[preview]{standalone}
\begin{document}
\begin{equation}
\frac{1}{2} = 0.5
\end{equation}
\end{document}

followed by

pdflatex equation.tex
convert -density 300 equation.pdf -quality 95 equation.png

(using ImageMagick's convert program), but the output contains a number next to my equation that I can't seem to get rid of. Also, this seems like a lot of work, and I don't need a whole documentclass just for one equation. Not to mention pdflatex produces a bunch of other auxiliary files that clutter up my directory during the conversion process. Is there some way I can just easily go from a file containing

\frac{1}{2} = 0.5

to equation.png? Better yet, is there a simple way I can take a file containing a list of equations and have each of them converted into their own little .png image?

  • 8
    replace the equation env with \[and \] – percusse Jan 13 '16 at 18:55
  • Thanks! That helps a lot. Is there also a way to shrinkwrap the generated pdf around the equation? I have huge margins consisting only of whitespace. – Nick Jan 13 '16 at 19:03
  • You could use the geometry package to reduce the size of the page with \usepackage[paperwidth=3in,paperheight=1.5in]{geometry}. It might be better done at the convert stage, though, with the -trim option, which strips all pixels from the outside edges which match the corner pixels in color. – Brian Jan 13 '16 at 19:32
  • I would use a simple math group $ a+b$, or if needed the diplayed mode: $\sum_{0}^{k}$. – Johannes_B Jan 13 '16 at 19:40
23

standalone class is sufficient. You can enable the multi option, so that the contents of each (math) environment is cropped to its own page in a PDF file. Then you can either save each page directly as a .png image (Acrobat Pro, and perhaps Adobe Reader, allows you to do it in one go), or you can use the convert functionality (need Image Magick) provided with standalone. For the latter method, see Section 4.6 of the standalone documentation.

In the example below I define a new environment mymath, which is basically inline math with display style.

MWE

\documentclass[multi={mymath},border=1pt]{standalone}
% \usepackage{amsmath}
\newenvironment{mymath}{$\displaystyle}{$}

\begin{document}

\begin{mymath}
  \frac{1}{2}=0.5
\end{mymath}

\begin{mymath}
  a^2 + b^2 = c^2
\end{mymath}

\end{document}

Output

enter image description here

  • 11
    The standalone using convert route would look something like: \documentclass[convert={density=300,size=800x800,outext=.png}]{standalone} in case anybody wants a quick example. – tpg2114 Jan 14 '16 at 4:23
  • Probably section 5.6 in manual now? – Matifou May 14 at 23:46
  • Minor addition: I needed to set varwidth=true to get rid of some unnecessary horizontal space. – Max Nov 10 at 15:56
9

Here's what I do to go from a file of equations to png files using asymptote. You'll need the following 3 files in a directory.

sample.bat - change the "5" to alter the png resolution

asy sample -noV -render 5 -f png
pause

sample.dat - this file holds your equations.

\frac{1}{2} = 0.5
y = \sin{x}
a^2 + b^2 = c^2

sample.asy - this is the asymptote code

file fin = input("sample.dat").line();
int index = 1;
while (!eof(fin))
{
    string s = fin;
    if (s == "") { continue; }
    label("$\displaystyle " + s + "$", (0,0));
    shipout("eq"+string(index));
    erase();
    ++index;
}

Running the batch file will create the following 3 png files.

enter image description here

enter image description here

enter image description here

7

Since you have MacTeX, use LaTeXiT, which lets you convert equations to images in several formats, including PDF and PNG. There is no need to write LaTeX support code: just enter the equation in the input box and press "LaTeX it!".

If you want to convert equations in batch, you can write a simple script that adds the equation to a template based on the standalone class, runs latex, and then dvipng.

There are also web services, such as Roger's Online Equation Editor.

5

The problem appears to be that latex automatically numbers equations in the equation environment. This default behavior of latex's is likely because authors often need to refer to equations. An easy solution is to remove numbering by using

\begin{equation*}
YOUR EQUATION GOES HERE
\end{equation*}
2

I want to add my two cents here. I wasn't satisfied with the current solutions so I wrote a script. You can find it here, pnglatex. In its simplest form you can use it like this:

pnglatex -f '\frac{1}{2}'

To get a full list of options use pnglatex -h.

1

I am missing the reputation to add a comment to Herr K.'s answer...

In order to generate individual PNG files on Windows, you may need to specify the actual convert executable on your path (standalone assumes this to be imgconvert on Windows, instead of convert).

MWE:

\documentclass[
  multi={mymath},
  border=2pt,
  convert={convertexe={convert},density=300,outext=.png}
  ]{standalone}
% \usepackage{amsmath}
\newenvironment{mymath}{$\displaystyle}{$}

\begin{document}

\begin{mymath}
  \frac{1}{2}=0.5
\end{mymath}

\begin{mymath}
  a^2 + b^2 = c^2
\end{mymath}

\end{document}

This generates to following two PNG files:

1st example

2nd example

0

If you use DMelt (http://jwork.org/dmelt) version 2 and above, you can make PNG file from equation using Python syntax:

from jhplot import *
eq="\\frac{\sin(x*2)}{\int^{100}_{i=0} F(x) dx}"
image="/tmp/equation.png" # output file
# create equation 
q1=HLatexEq(eq, 64)       # font size 64 
q1.export(image)
print "Created :",image
IView(image)              # View this image 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.