how to get mathematical equations into image in latex? I want the code that convert all equations into image in the pdf file after compile the tex file.

  • Are you saying that you want to extract all equations from the PDF into separate image files? Can you give a concrete example of what you would like to be able to do? – Sean Allred Nov 30 '14 at 5:56
  • When we compile a tex file, the out put file normally pdf file and in that pdf file all equations becomes images and rest of them normal text. – Suresh Nov 30 '14 at 6:01
  • Why do you want that? At the end do you want single pdf file (still)? – user11232 Nov 30 '14 at 6:03
  • Just to rephrase to make sure I understand: when you compile your document, some equations are output as raster images and some are output 'normally' as pure PDF? – Sean Allred Nov 30 '14 at 6:03
  • 3
    Can you post some code that illustrates this? How are you compiling? Using pdflatex? – user11232 Nov 30 '14 at 6:09

I suppose you could write a package that compiles equations in subjobs, like so (call this package imageeqn.sty):

\ProvidesPackage{imageeqn}[2014/11/30 v0.1 Image equations]



\begingroup \catcode `|=0 \catcode `[=1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=12 |gdef|@ximageeqn#1\end{imageeqn}[|immediate|write|@out[#1]|end[imageeqn]]

\def\imageeqn{\kernel@ifnextchar [{\@imageeqn}{\@imageeqn[]}}

\begingroup \catcode`\^^M=12 %

\immediate\write18{pdflatex --enable-write18 -interaction=nonstopmode \jobname-eqn-\arabic{imageeqn}.tex}

This package (which I wrote by modifying python.sty) defines an environment imageeqn, which takes the equation inside of it, generates a tex file, compiles it to png, and then includes the resulting png using \includegraphics. This can be used as follows (let's call this file image.tex):



Pythagorean theorem:
\begin{imageeqn}x^2 + y^2 = z^2.\end{imageeqn}
Some integral:
\begin{imageeqn}\Gamma(t) = \int_0^\infty x^{t-1} e^{-x} \, dx.\end{imageeqn}

Compile this with the command line:

pdflatex --enable-write18 image.tex

The result is a pdf file that has the two equations embedded as rasterized images. I'm not sure why you would ever want to do this, but I enjoyed myself trying to figure out how to do this anyway :)

This implementation is pretty limited by the way: first of all, it won't compile if you put \end{imageeqn} on a new line. Second of all, it turns equations into text-style math (in dollar signs), so equation numbering, aligning, etc., are not supported.

Solving the newline problem

The first problem can be fixed, at least on Linux, by preprocessing the subjobs with sed. This can be done by replacing:

\immediate\write18{pdflatex --enable-write18 -interaction=nonstopmode \jobname-eqn-\arabic{imageeqn}.tex}

in the above code by:

\immediate\write18{sed -i '/^$/d' \jobname-eqn-\arabic{imageeqn}.tex; pdflatex --enable-write18 -interaction=nonstopmode \jobname-eqn-\arabic{imageeqn}.tex}

Using preprocessing to make \[, \begin{equation} work

Making \[, \], \begin{equation}, \end{equation} work can be done by preprocessing the input with sed:

cat input.tex | sed 's|\\\[|\begin{imageeqn}|g' \
              | sed 's|\\\]|\end{imageeqn}|g' \
              | sed 's|\\begin{equation}|\begin{imageeqn}|g' \
              | sed 's|\\end{equation}|\end{imageeqn}|g' \
              > input2.tex
pdflatex --enable-write18 input2.tex

Perhaps lualatex may be of help here, but I don't know anything about that.

  • consider the following code: \documentclass{report} \begin{document} [x^2 + y^2 = z^2.] \begin{equation} \Gamma(t) = \int_0^\infty x^{t-1} e^{-x} \, dx. \end{equation} $\mathbb{U}$ \end{document} I need that when compile the above, output in the form of pdf file and all the equations are as images in the pdf file. – Suresh Dec 1 '14 at 18:30
  • @Suresh: The above code provides a starting point. Since your question is quite specific (and to be honest: I don't understand why you would want this), I'm afraid you're going to have to tweak the code to make it work for your particular situation. – yori Dec 1 '14 at 19:03

Alright, I cooked up another answer. The following second version of the package imageeqn.sty reimplements the equation environment, the \[, \], \(, \) environments, and inline math between dollar signs.

\ProvidesPackage{imageeqn}[2014/11/30 v0.2 Image equations]




% -- open subjob and write preamble

% -- close current subjob

% -- compile and insert current subjob
  \immediate\write18{pdflatex --enable-write18 -interaction=nonstopmode \jobname-eqn-\arabic{ie@subjob}.tex}%

% -- define \inlineimageeqn command

% -- redefine 'equation' environment

% -- redefine '\[ \]' environment

% -- redefine '\( \)' environment

% -- refine inline math

The following simple example illustrates its use:


Pythagorean theorem for $x$, $y$, and $z$:
x^2 + y^2 = z^2.
Some integral:
\Gamma(t) = \int_0^\infty x^{t-1} e^{-x} \, dx.

Thanks to barbara beeton whose answer in Remove all math for spelling and grammar checking inspired this approach.

  • I got the pdf file for the above code, but only the image box is displayed and no equation is inside. – Suresh Dec 2 '14 at 4:47
  • @Suresh: It works fine on my system. Are you compiling with the --enable-write18 flag? Are you using the latest texlive? Do the generated png files look ok? – yori Dec 2 '14 at 9:29

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.