# Converting equations to images

I am looking to convert LaTeX equations to images in the pdf output. After searching around here I've found some sources to websites and software that can convert individual equations to images, but it would be really nice if I could run such a converter on an entire tex file and have it convert the equations to images automatically. It's tedious to manually convert every equation only to plug them back into the document as an image.

\subsection{Linear equation through a point given its gradient}
A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$ (or more commonly used for lines, its slope).

$$\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C$$

Plug in the initial conditions to find the particular value of the constant of integration $C$.


Here's the default output where equations contain selectable text (in Acrobat Reader):

But I was wondering if it was possible to output a pdf where the equations are converted to images like this (in Acrobat Reader):

Note: Initially, I had asked a different question but then I decided to reword it significantly. As a result, the comments and answer aren't actually relevant to that older question anymore. I decided to re-post the revised version as a new question.

• Please do not mark this question as a duplicate of this one. Instead, please go to the older question and flag it to get it deleted. Commented Apr 28, 2021 at 10:30
• You should be able to go to your prior question and hit the delete button at the bottom of the question. That would be the easiest way. Or else you could edit the former question (since it does not yet have answers) to reflect the content of this question. That way the former comments would be preserved. Commented Apr 28, 2021 at 10:33
• @StevenB.Segletes I understand, but someone took the time and effort to post an answer to the original question before I edited it, so I'm not sure if it's justified if I delete the question myself. Commented Apr 28, 2021 at 10:35
• Got it. I hadn't seen that initially. If that answer answered the question as written (even if it was not what you meant), you should consider "accepting" the answer, with this question as a follow up. If that answer misunderstood the question as written, then deleting it is OK. Commented Apr 28, 2021 at 10:35
• So is your goal to be able to open the pdf in Word without messing up the equations (as implied by your previous question)? If not, then what is the reason that you want to do this? Commented Apr 28, 2021 at 12:48

One possibility is to put each equation inside a tikzpicture and then use tikzexternalize to automatically convert each equation to png and include that png, similar to tikz external use png as default instead of pdf. The implementation below requires that you replace \begin{equation}\end{equation} with the new command \pngequation.

This needs to be compiled with -shell-escape. Note that the compiler to create the images is hardcoded, in this case pdflatex, which can be modified if you want another compiler.

Note that this is only a proof on concept. It is rather roundabout, very likely has limitations, problems and/or side effects, and is therefore not recommended - for most use cases there will be easier solutions as mentioned in the comments.

MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize

\tikzset{%
% Add size information to the .dpth file (png is in density not size)
/pgf/images/external info,
% Use the png export AND the import
use png/.style={png export,png import},
png export/.style={
external/system call=%
{pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource";%
convert -density 300 -transparent white "\image.pdf" "\image.png"; rm -f "\image.pdf"},
},
png import/.code={%
\tikzset{%
/pgf/images/include external/.code={%
% Here you can alter to whatever you want
% \pgfexternalwidth is only available if /pgf/images/external info
% is set
\includegraphics%
[width=\pgfexternalwidth,height=\pgfexternalheight]%
{{##1}.png}%
}%
}%
}%
}
\newcounter{pngeq}
\setcounter{pngeq}{0}
\newcommand{\pngequation}[1]{%
\tikzset{use png}
\begin{tikzpicture}
\node (eq) {
\hspace{-1.1\parindent}\begin{minipage}{\textwidth}%
\setcounter{equation}{\number\value{pngeq}}
$$#1$$
\end{minipage}
};
\end{tikzpicture}%
\stepcounter{pngeq}
}

\begin{document}
\section{Linear equation through a point given its gradient}
A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$ (or more commonly used for lines, its slope).

\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C
}

Plug in the initial conditions to find the particular value of the constant of integration $C$.

\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}
\end{document}


Result:

For subequations you can extend the solution above along the same lines. First add an extra subequation counter that resets with the main counter:

\newcounter{pngsubeq}[pngeq]


Then define a command \pngsubequation that steps this counter and uses it in the numbering.

\newcommand{\pngsubequation}[1]{%
\tikzset{use png}
\ifnum\value{pngsubeq}=0\stepcounter{pngeq}\fi% first subequation, step main counter
\stepcounter{pngsubeq}
\begin{tikzpicture}
\node (eq) {
\hspace{-1.1\parindent}\begin{minipage}{\textwidth}%
\renewcommand{\theequation}{\arabic{pngeq}\alph{pngsubeq}}
$$#1$$
\end{minipage}
};
\end{tikzpicture}%
}


To step the main counter for the subequations a hook can be added to the subequations environment:

\BeforeBeginEnvironment{subequations}{\stepcounter{pngeq}}


Then you can use \begin{subequations} with \pngsubeqation inside:

\begin{subequations}
\pngsubequation{...}

\pngsubequation{...}
\end{subequations}


Full code, with a feq sets of normal equations and subequations:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize

\tikzset{%
% Add size information to the .dpth file (png is in density not size)
/pgf/images/external info,
% Use the png export AND the import
use png/.style={png export,png import},
png export/.style={
external/system call=%
{pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource";%
convert -density 300 -transparent white "\image.pdf" "\image.png"; rm -f "\image.pdf"},
},
png import/.code={%
\tikzset{%
/pgf/images/include external/.code={%
% Here you can alter to whatever you want
% \pgfexternalwidth is only available if /pgf/images/external info
% is set
\includegraphics%
[width=\pgfexternalwidth,height=\pgfexternalheight]%
{{##1}.png}%
}%
}%
}%
}
\newcounter{pngeq}
\setcounter{pngeq}{0}
\newcounter{pngsubeq}[pngeq]
\newcommand{\pngequation}[1]{%
\tikzset{use png}
\stepcounter{pngeq}
\begin{tikzpicture}
\node (eq) {
\hspace{-1.1\parindent}\begin{minipage}{\textwidth}%
\renewcommand{\theequation}{\arabic{pngeq}}
$$#1$$
\end{minipage}
};
\end{tikzpicture}%
}
\BeforeBeginEnvironment{subequations}{\stepcounter{pngeq}}
\newcommand{\pngsubequation}[1]{%
\tikzset{use png}
\stepcounter{pngsubeq}
\begin{tikzpicture}
\node (eq) {
\hspace{-1.1\parindent}\begin{minipage}{\textwidth}%
\renewcommand{\theequation}{\arabic{pngeq}\alph{pngsubeq}}
$$#1$$
\end{minipage}
};
\end{tikzpicture}%
}

\begin{document}
\section{Linear equation through a point given its gradient}
A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$.

\pngequation{$$\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C$$}

Plug in the initial conditions to find the particular value of the constant of integration $C$.

\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}

A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$.

\begin{subequations}
\pngsubequation{$$\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C$$}

Plug in the initial conditions to find the particular value of the constant of integration $C$.

\pngsubequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}
\end{subequations}

A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$.

\pngequation{$$\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C$$}

Plug in the initial conditions to find the particular value of the constant of integration $C$.

\pngequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}

A very simple differential equation is to find a linear equation through a particular point $P(x_i,y_i)$ given its gradient $k_i$.

\begin{subequations}
\pngsubequation{$$\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C$$}

Plug in the initial conditions to find the particular value of the constant of integration $C$.

\pngsubequation{
\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C'
}
\end{subequations}
\end{document}

\tikzset{use png}
\begin{tikzpicture}
\node (eq) {
\hspace{-\parindent}\begin{minipage}{\textwidth}%
$$\frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C$$
\end{minipage}
};
\end{tikzpicture}


Result:

• I'll try this out as soon as possible, thanks for looking into it! Commented Apr 28, 2021 at 15:00
• @JansthcirlU see tex.stackexchange.com/a/236648. -enable-write18 is the MikTeX equivalent of -shell-escape, on Mac or Linux you may need -shell-escape. For the terminal: open a terminal, navigate to the folder where your document is (using the cd command), then type pdflatex -shell-escape yourfile.tex where you schould replace yourfile with the actual name of your file. Commented Apr 28, 2021 at 15:49
• @SOUser I have added subequations to the answer. Commented Nov 27, 2023 at 13:53
• @SOUser that is a situation I did not think about indeed, when you immediately start a new set of subequations then the subequation counter is not zero so my check to increase the main counter did not work. I changed it into a hook for the subequations environment and removed the original check (see edit), this is more robust and also covers the multiple sets of subequations in a row. Commented Nov 28, 2023 at 6:33
• @SOUser this might be possible, I'm not sure if the equation counters are available at the time the files are written but that can probably be made to work. I don't have time now but I will try to take a look over the next couple of days. Commented Nov 29, 2023 at 15:53