Formulas in Google Slides

Several weeks ago, I had to prepare a presentation. So as I have been running Linux distributions for the last few years, which usually do not have native support of the Microsoft Office packages, I have been using Google Slides to prepare slides for my presentations. In general, I was happy with the functionality provided by this service, but, in the last presentation, I had to add a lot of formulas, and I found out that there is no equation editor provided by Google Slides. Although recently Google has introduced markdown support in its office tools, unfortunately, MathJax, a JavaScript-based display engine, which is usually used to render equations, has not been added as a part of this update.

Of course, as this is such a common task, I thought that it should have been solved. Indeed, there are several Chrome extensions that allow rendering LaTeX equations into figures. However, when I tried to install some of them, I was shocked by the number and criticality of the permissions they requested. Moreover, these extensions do not usually allow you to choose the resolution of a final image. That can be crucial if you try to produce hi-res adapted presentations. Therefore, I developed a custom tool for transforming LaTeX equations into figures. In this article, I describe how to do this. In the Internet, you can find similar solutions, and mine is not a perfect one. However, it fits my needs, and I am writing this article as a memo for future me. Still, I will be glad if my instructions are helpful to somebody else.

Prerequisites

If you follow my articles, you probably already know that I use Kubuntu 20.04 as my operating system. Therefore, all instructions should work on this operating system. If you use other operating systems, you may need to adapt the approach correspondingly.

In Google Slides, you can add formulas if they are rendered to one of the supported image formats. Personally, I prefer png format, therefore, all the instructions will be for this image format. To write equations, we will use LaTeX notation.

To create a formula image from LaTeX, I rely on three utilities:

  • pdflatex - to build a pdf document from LaTeX code;
  • pdfcrop - to crop white margins around a formula;
  • convert - to convert a formula from pdf to png format.

Therefore, you need to install these utilities to use the approach described in this article. pdflatex and pdfcrop utilities are usually a part of a LaTeX distribution. Personally, I use MiKTeX, but you can make use of other LaTeX distributions as well (e.g., TeX Live). convert is a part of the ImageMagick package.

The following two instructions should prepare your machine for using the approach described in this article:

$ sudo apt install texlive-full
$ sudo apt install imagemagick

Approach

There are two constituents of the approach: 1) the LaTeX template document where you write a formula; and 2) a bash script that makes use of the utilities described in the previous section to render a formula written in LaTeX into a png image.

Converter

At first, let us consider a bash script that is used to do the magic. The source code of the script is provided below:

#!/bin/bash
# Checks
command -v pdflatex >/dev/null 2>&1 || { echo >&2 "Cannot find pdflatex. Try: 'sudo apt install texlive-full'. Aborting."; exit 1; }
command -v pdfcrop >/dev/null 2>&1 || { echo >&2 "Cannot find pdfcrop. Try: 'sudo apt install texlive-full'. Aborting."; exit 1; }
command -v convert >/dev/null 2>&1 || { echo >&2 "Failed, Try: apt install texlive-full. Aborting."; exit 1; }
# Prepare
filename="${1%.*}"
temp_dir=$(cat /proc/sys/kernel/random/uuid | sed 's/[-]//g' | head -c 20; echo;)
temp_dir="./$temp_dir" 
if [ ! -d $temp_dir ]; then mkdir $temp_dir; fi
# Conversions
pdflatex --interaction=batchmode -jobname "$temp_dir/latex.temp" $1 > /dev/null
pdfcrop "$temp_dir/latex.temp.pdf" "$temp_dir/crop.pdf" > /dev/null
convert -density 300 "$temp_dir/crop.pdf" "$filename.png"  > /dev/null
# Cleaning
rm -r "$temp_dir"

The code is pretty well-documented. In the “Checks” block, I check if the required utilities are available. Then, in the “Prepare” block, I get the path to the tex file with the formulas, provided as an argument, and store it without extension in the filename variable. Then, we create a temporary directory with a random name where we will store the results of the intermediate conversions.

In the “Conversions” block, we perform the job. First, using pdflatex we convert a file with the formulas into a pdf document. Then, with pdfcrop we remove all whitespace around the formulas. Finally, the convert utility is used to convert the pdf file with the cropped formulas images into a png format. Note that I add -a density option to make the resulting image of high quality.

In the “Cleaning” section, we remove the created temporary repository.

I store this script in the tex2png file, put it into the directory defined in the PATH variable (~/bin/), and make it executable.

LaTex Document

Now, let us consider how to write formulas. Below, you can find a LaTeX template that you modify with your formulas:

\documentclass[border=0.1cm]{minimal}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{align*}

\begin{document}

\begin{align*}
F=ma
\end{align*}

\begin{align*}
E = mc^2
\end{align*}

\begin{equation*}
a^2 -b^2 = (a-b)(a+b)    
\end{equation*}

\end{document} 

This template is based on a minimal document class and uses three packages: amsmath and amssymb - to write math formulas and use different math symbols, and the preview package - to extract “certain environments (most notably displayed formulas) from LATEX sources as graphics.” The latter is loaded with two options: active - to activate the package (in this case, it will produce previews) and tightpage is necessary to output every formula to a new page so that all formulas in a document can be processed with one shot.

The preamble command \PreviewEnvironment{align*} of the preview package defines an environment used to generate a preview. Note that in this case, only formulas inside the align* environment will be rendered (the formula in the equation* environment will not be rendered). It is possible to configure preview package so that it renders every math formula defined in the equation-related environments (see displaymath option) and even text (see textmath option). For instance, we can modify the previous example to output all the formulas (probably, this LaTeX template will be used the most often):

\documentclass[border=0.1cm]{minimal}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[active,tightpage,displaymath]{preview}

\begin{document}

\begin{align*}
F=ma
\end{align*}

\begin{align*}
E = mc^2
\end{align*}

\begin{equation*}
a^2 -b^2 = (a-b)(a+b)    
\end{equation*}

\end{document}

Stitching Bits Together

Now, using the tex2png script, you can render the formulas from the formulas.tex file into png images. Just run the following command from the directory where you’ve stored formulas.tex file.

$ tex2png formulas.tex

As a result, in the same directory, you should get every formula rendered in a separate png file.

Conclusion

In the repository accompanying this blog, you can find the sources of the script and LaTeX template you can find.

Related