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:


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


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

3x+5 &= 0 \\
3x+5-5 &= 0-5 \\
3x &= -5

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

\frac{3x}{3} &= \frac{-5}{3} \\
x &= -\frac{5}{3}

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


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:


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.



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

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


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

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

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

pdflatex -jobname=temp <<EOT > /dev/null 2>&1

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

rm temp.aux temp.log temp.pdf

Which you can then call like,

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


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