How can I render equations in an API response as an image file

I would like to make an API call, that returns a mixture of text and mathematical equations, and save the rendered output as an image file.

So far I’ve been trying pyplot library in Python, but results are not consistent. It seems the response from the API needs some pre-processing before pyplot can render it (such as replacing [ with $) even then it only gets rendered correctly on some of the cases. Simple formatting such as wrapping breaks the rendering.

Is there a known, consistently working way of rendering equations? Python is preferred but I don’t have to use it. Ideally I would like to generate PNGs, but can be other formats as well.

Intention is to run the rendering algorithm for 100s of prompts hence I am looking a solution that can be repeated without human intervention.

Thanks in advance

Edit: See below post for a more robust solution.

1 Like

Hi @elmstedt ,

Thanks for your reply. Is this actually working for you ? I may be doing something wrong.

I use this prompt:
Explain solution of 3x+5=0, use latex notation and create a full latex document

It outputs:

\begin{document}

\title{Solution of the Equation $3x+5=0$}
\author{Your Name}
\date{}

\maketitle

We want to solve the equation $3x+5=0$ for $x$. To do this, we first subtract 5 from both sides of the equation:

\begin{align*}
3x+5 &= 0 \\
3x+5-5 &= 0-5 \\
3x &= -5
\end{align*}

Next, we divide both sides by 3 to solve for $x$:

\begin{align*}
\frac{3x}{3} &= \frac{-5}{3} \\
x &= -\frac{5}{3}
\end{align*}

Therefore, the solution to the equation $3x+5=0$ is $x = -\frac{5}{3}$.

\end{document}

When I pass this to latex, I get many errors, undefined control sequence, environment align* not defined, misplaced alignment character & and more.

Not familiar with LaTex so I might be doing something silly.

Ok, it works. Needed adding following on top of the document:

\documentclass[12pt]{article}
\usepackage{amsmath}
\oddsidemargin=0pt
\topmargin=0pt
\textwidth=6.5in
\textheight=8.5in

I think the first two lines were required, rest doing some additional formatting.

Resultant pgn doesn’t look very nice but I suppose it needs further formatting. Perhaps outside the scope of this forum.

Thanks a lot for the help.

1 Like

Yeah, I was going to note that you’re missing the preamble.

Anyway, for standalone equations I put this together which works nicely.

tex2img.sh

#!/bin/bash

output_filename="eq.png"

while getopts e:o: flag
do
    case "${flag}" in
        e) latex_equation=${OPTARG};;
        o) output_filename=${OPTARG};;
    esac
done

if [ -z "$latex_equation" ]; then
  echo "Usage: $0 -e '<LaTeX equation>' [-o <output filename>]"
  exit 1
fi

output_format="${output_filename##*.}"

if ! command -v pdflatex &> /dev/null; then
  echo "pdflatex could not be found. Please install it using:"
  echo "sudo apt-get install texlive"
  exit 1
fi

if ! command -v convert &> /dev/null; then
  echo "ImageMagick's convert could not be found. Please install it using:"
  echo "sudo apt-get install imagemagick"
  exit 1
fi

if [ "$output_format" == "svg" ]; then
  if ! command -v pdf2svg &> /dev/null; then
    echo "pdf2svg could not be found. Please install it using:"
    echo "sudo apt-get install pdf2svg"
    exit 1
  fi
fi

pdflatex -jobname=temp <<EOT > /dev/null 2>&1
\documentclass[border=2mm]{standalone}
\usepackage{amsmath}
\begin{document}
\($latex_equation\)
\end{document}
EOT

if [ "$output_format" == "svg" ]; then
    pdf2svg temp.pdf "$output_filename" > /dev/null 2>&1
else
    convert -density 300 temp.pdf -quality 90 "$output_filename" > /dev/null 2>&1
fi

rm temp.aux temp.log temp.pdf

Which you can then call like,

png
./tex2img.sh -e 'e^{i\pi} + 1 = 0' -o euler.png -d 800

euler

webp
./tex2img.sh -e 'e^{i\pi} + 1 = 0' -o euler.webp -d 800

The image displays the famous mathematical equation  e^{ii} + 1 = 0 , known as Euler's identity. (Captioned by AI)

etc…