5

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

\begin{equation} \frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C \end{equation}

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

Current pdf output: equation contains text

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

Desired pdf output: equation is printed as image

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. – JansthcirlU Apr 28 '21 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. – Steven B. Segletes Apr 28 '21 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. – JansthcirlU Apr 28 '21 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. – Steven B. Segletes Apr 28 '21 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? – Marijn Apr 28 '21 at 12:48
  • @Marijn Images are easier to share with others if you don't want to share the entire pdf. The pdf to doc thing is only a secondary concern, but having images for equations would make the conversion a lot more reliable. – JansthcirlU Apr 28 '21 at 12:54
  • @JansthcirlU if you just want all the equations as image files then make4ht yourfile.tex (as mentioned in a comment to your previous question) will do that, it generates a html page (that you don't need) and a set of .png files, one file for each equation. – Marijn Apr 28 '21 at 13:04
  • So there's no way to automatically render a pdf with the equations embedded as images? I'd have to manually replace all the equations by the images generated by make4ht? – JansthcirlU Apr 28 '21 at 13:18
  • There might be a way, just that you said you wanted to share the images with others and not the rest of the pdf, for that scenario make4ht is a quick solution. – Marijn Apr 28 '21 at 13:21
  • And if you do want to share the pdf, then you can just share the pdf with regular equations, I presume? – Marijn Apr 28 '21 at 13:22
  • Well, I just gave it a shot and the HTML has some weird combinations of text and images. For example, the equation $V = \frac{m}{\rho}$ renders "V =" as text but the fraction as an image. Maybe that's something I can tweak but I'd have to consult the documentation. – JansthcirlU Apr 28 '21 at 13:23
  • Regardless, my train of thought was to get a pdf with embedded images. If I then wanted to share an equation, I could just scroll to the right image in my pdf and copy it over. And frankly, I shouldn't justify why I'm curious about whether or not it's possible to convert equations to images when rendering a document. – JansthcirlU Apr 28 '21 at 13:30
  • You don't have to justify anything, it is just that finding the exact reason somebody has for wanting some functionality usually helps in giving advice, and might prevent people from doing unnecessary complicated things. In this case maybe you consider using tools that make screenshots, for every platform there are tools that you can configure to select an area of the screen and copy that to the clipboard (or save it as a new file if you want to keep it). – Marijn Apr 28 '21 at 13:40
  • That's a fair point, my bad. But from what I gather this simply isn't possible using tex or Texmaker alone? – JansthcirlU Apr 28 '21 at 13:44

1 Answers1

5

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}} \begin{equation} #1 \end{equation} \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:

enter image description here


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}}
\begin{equation}
#1
\end{equation}
\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}} \begin{equation} #1 \end{equation} \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}} \begin{equation} #1 \end{equation} \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}% \begin{equation} \frac{dy}{dx} = k_i \iff \int dy = \int k_i \cdot dx \iff y = k_i \cdot x + C \end{equation} \end{minipage} }; \end{tikzpicture}

Result:

enter image description here

Marijn
  • 37,699
  • I'll try this out as soon as possible, thanks for looking into it! – JansthcirlU Apr 28 '21 at 15:00
  • What's the most straightforward way to compiling the document using -shell-escape from the Texmaker GUI? Or how can I do it manually from the terminal? – JansthcirlU Apr 28 '21 at 15:13
  • 1
    @JansthcirlU see https://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. – Marijn Apr 28 '21 at 15:49
  • Thanks, I'll look into it. – JansthcirlU Apr 28 '21 at 17:26
  • It looks like I get pdf outputs of each equation image, rather than png files that can be linked. – JansthcirlU Apr 29 '21 at 09:38
  • @JansthcirlU did you make sure to include \tikzset{use png} in the right place? Did you try this MWE, or you tried your own real document? Does the convert system call work? I seem to remember that convert (which is part of the ImageMagick programs) has a different name on Windows. You should get errors in the logs if this fails, you can also try to run the convert command with the appropriate arguments in a terminal to see if the conversion actually works. – Marijn Apr 29 '21 at 09:56
  • I blindly copied over your MWE to a file named ImageEquations.tex and compiled it using pdflatex -enable-write18 ImageEquations.tex. I'm not very familiar with Latex overall, so I find it hard to navigate the terminal output. – JansthcirlU Apr 29 '21 at 09:59
  • @JansthcirlU I just realized that the delete command at the end (rm -f "\image.pdf") is also OS-specific, i.e., this works only on Linux and Mac. For Windows substituting this by del "\image.pdf" might work I think (cannot test that now). – Marijn Apr 29 '21 at 10:02
  • It looks like the png files doesn't get created. For everything ImageEquations-figure0 related files, I get the following file extensions: pdf, md5, txt and dpth. – JansthcirlU Apr 29 '21 at 10:10
  • @JansthcirlU then it is probably a problem with the convert command. In Windows probably using either only magick instead of convert or magick convert (so the two commands in a row) is supposed to work. Install via https://imagemagick.org/script/download.php#windows if you don't have it yet. – Marijn Apr 29 '21 at 10:19
  • @Marijn Very helpful ! Could you also help to suggest how to handle subequations environment using your solution ? For example, some equations 1 to 30, then 31a, 31b, then 32 to 40, then 41a, 41b ? Many thanks ! – SOUser Nov 27 '23 at 11:17
  • 1
    @SOUser I have added subequations to the answer. – Marijn Nov 27 '23 at 13:53
  • @Marijn Is it possible to support 1 to 30, 31a, 31b, 32a, 32b ? Currently the 32a, 32b appear as 31c, 31d.... – SOUser Nov 28 '23 at 04:15
  • 1
    @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. – Marijn Nov 28 '23 at 06:33
  • @Marjin Many thanks ! Would you help to suggest if it is possible to match the name of the generated images as the subequations ? :D Without subequations there is offset of 1 which is not so hard to pin down. With subequations the offset is a bit hard to compute. – SOUser Nov 28 '23 at 07:19
  • @Marijn Would it be possible to name the files as *** figure1.png,...., figure 32a.png, figure 32b.png for equations 1.... 32a, 32b ? – SOUser Nov 29 '23 at 14:22
  • 1
    @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. – Marijn Nov 29 '23 at 15:53
  • @Marijn Based on your mwe, if I add \tikzsetnextfilename{\jobname-eq-\arabic{pngeq}} and \tikzsetnextfilename{\jobname-eq-\arabic{pngeq}\alph{pngsubeq}} before \tikzset{use png}, respectively, the issue of image names could be solved, which is nice enough. The whole file is shown in https://pastebin.com/3X6DYMLP. However, there appear two new gitches. One is I have to manually run pdflatex twice (the first run gives a parent pdf without equations). Two is the first run seems to run two rounds. – SOUser Nov 30 '23 at 07:30
  • I tried @Marijn code and it worked perfectly on Overleaf. However, I couldn't manage to make it work on my local installation (MikTek + TexMaker), even after configuring my pdflatex with the following command inside TexMaker : ``` pdflatex -synctex=1 -interaction=nonstopmode -enable-write18 %.tex
    rror -interaction=batchmode -jobname "main-figure1" "\def\tikzexternalrealjob{m
    ain}\input{main}";convert -density 300 -transparent white "main-figure1.pdf"
    
    – Chakrastyl Feb 26 '24 at 00:21