# how to get mathematical equations into image in latex?

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
• 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):

\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{imageeqn}[2014/11/30 v0.1 Image equations]

\RequirePackage{graphicx}

\newwrite\@out
\newcounter{imageeqn}

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

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

\def\@imageeqn[#1]{%
\stepcounter{imageeqn}
\immediate\openout\@out=\jobname-eqn-\arabic{imageeqn}.tex
\immediate\write\@out{\noexpand\documentclass[convert={density=288,outext=.png}]{standalone}}
\immediate\write\@out{\noexpand\usepackage{amsmath}}
\immediate\write\@out{\noexpand\begin{document}}
\immediate\write\@out{\noexpand$} \newlinechar='15 \begingroup \catcode\^^M=12 % \let\do\@makeother\dospecials\obeyspaces% \@ximageeqn} \def\endimageeqn{% \endgroup% \immediate\write\@out{\noexpand$}
\immediate\write\@out{\noexpand\end{document}}
\immediate\closeout\@out
\immediate\write18{pdflatex --enable-write18 -interaction=nonstopmode \jobname-eqn-\arabic{imageeqn}.tex}
\begin{center}
\includegraphics[scale=0.25]{\jobname-eqn-\arabic{imageeqn}.png}
\end{center}}


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):

\documentclass{article}

\usepackage{imageeqn}

\begin{document}
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}
\end{document}


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 $, work Making \[, $, , 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{imageeqn}|g' \ | sed 's|$/extract_tex]|\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.] $$\Gamma(t) = \int_0^\infty x^{t-1} e^{-x} \, dx.$$ \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. \NeedsTeXFormat{LaTeX2e}[1994/12/01] \ProvidesPackage{imageeqn}[2014/11/30 v0.2 Image equations] \RequirePackage{graphicx} \RequirePackage{environ} \RequirePackage{pgf} \newcommand\ie@resolution{288} \pgfmathsetmacro\ie@scale{72/\ie@resolution} \newwrite\@out \newcounter{ie@subjob} % -- open subjob and write preamble \def\ie@open{% \stepcounter{ie@subjob}% \immediate\openout\@out=\jobname-eqn-\arabic{ie@subjob}.tex% \immediate\write\@out{\noexpand\documentclass[convert={density=\ie@resolution,outext=.png}]{standalone}}% \immediate\write\@out{\noexpand\usepackage{amsmath}}% \immediate\write\@out{\noexpand\begin{document}}% \immediate\write\@out{\noexpand$}%
}

% -- close current subjob
\def\ie@close{%
\immediate\write\@out{\noexpand$}% \immediate\write\@out{\noexpand\end{document}}% \immediate\closeout\@out% } % -- compile and insert current subjob \def\ie@emit{% \immediate\write18{pdflatex --enable-write18 -interaction=nonstopmode \jobname-eqn-\arabic{ie@subjob}.tex}% \includegraphics[scale=\ie@scale]{\jobname-eqn-\arabic{ie@subjob}.png}% } % -- define \inlineimageeqn command \newcommand\inlineimageeqn[1]{% \ie@open% \immediate\write\@out{\unexpanded{#1}}% \ie@close% \ie@emit% } % -- redefine 'equation' environment \RenewEnviron{equation}{% \begin{center} \ie@open% \immediate\write\@out{\BODY}% \ie@close% \ie@emit% \end{center}} % -- redefine '' environment \def$#1${\begin{center}\inlineimageeqn{#1}\end{center}} % -- redefine '' environment \def$$#1$${\begin{center}\inlineimageeqn{#1}\end{center}} % -- refine inline math \catcode\$=13
\def$#1${\inlineimageeqn{#1}}


The following simple example illustrates its use:

\documentclass{article}
\usepackage{imageeqn}

\begin{document}
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.$$
\end{document}


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