# Exporting all equations from a document as individual pdf files in macOS or Linux

Usually one has a report, paper, draft, letter, etc in tex and needs to make presentations from them. Then, exporting all the equations in individual cropped nice pdf's is very useful. It's rather bash script topic but it's relevant only to tex users and is about tex files manipulation.

Very similar to Exporting all equations from a document as individual svg files but macOS-compatible and exporting pdf equations instead of svg (minor modifications would be needed to get them as svg files).

This questions might be related:

Extract equations from a pdf that is compiled using Latex

Generate external images for all equations in a document

This is strongly based in Damien's question and answer: Exporting all equations from a document as individual svg files

I basically put in a rather dirty bash script all the commands but the OSX-compatible version. (uses plenty: pdfcrop, pdftk, sed, latexmk, awk)

I used this solution to latex be more quiet: Compiler-style output for latex errors and the awk solution to insert lines into the .tex file, since sed in OSX seems to have some issues. (https://www.linuxquestions.org/questions/programming-9/sed-error-command-c-expects-%5C-followed-by-text-under-os-x-but-works-in-linux-730997/ pretty old but might be still not solved (?))

To use this, save the code in a file called scriptName and just run ./scriptName file.tex

#!/bin/bash

if [ $# -le 0 ] then echo "Usage:$0 filename"
exit
fi
file=$1 if [ ! -e$file ] || [ ! -f $file ] then echo$file not found or is not a regular file.
exit
fi

outDir=splitEquations

if [ ! -e $outDir ]; then mkdir$outDir
if [ $? -ne 0 ] ; then echo "Output directory$outDir does not exist and you have not enough permissions to create it."
exit
fi
fi

# Create a copy of the existing file
newFile=${file%.tex}-stripped.tex cp$file $newFile echo "Making a copy of$file, $newFile" echo "Stripping:$newFile"

sed -i '' 's/begin{equation\*}/begin{align*}/g'   newFile sed -i '' 's/end{equation\*}/end{align*}/g'newFile

sed -i '' 's/begin{equation}/begin{align*}/g'   newFile sed -i '' 's/end{equation}/end{align*}/g'newFile

sed -i '' 's/begin{align}/begin{align*}/g'   newFile sed -i '' 's/end{align}/end{align*}/g'newFile

awk '/\\begin{document}/{print "\\usepackage[active,tightpage]{preview}\n\\PreviewEnvironment{align*}"}1' $newFile > auxFile$newFile > auxFile

mv auxFile $newFile latexmk -pdf -pdflatex="pdflatex -file-line-error -interaction=nonstopmode"$newFile 2>&1 | grep "^.*:[0-9]*: .*$" pdfcrop${newFile%.tex}.pdf
cd $outDir pdftk "../${newFile%.tex}-crop.pdf" burst
cd ..
echo 'Done.'


Also, in my case what I really need to make presentations it's not only the equations under  but all the equations that are inserted in the text as well. This I guess is pretty hard to be done in general, but at least in my case works pretty well just adding these extra sed replacements (I found that through many files I always follow these rules :P):

sed -i '' 's/ \/\\begin{align*}/g'newFile
sed -i '' 's/\/\\end{align*}/g'newFile

sed -i '' 's/\\,/\\end{align*}/g'newFile
sed -i '' 's/\\./\\end{align*}/g'newFile

sed -i '' 's/(\/\\begin{align*}/g'newFile
sed -i '' 's/\)/\\end{align*}/g'newFile

sed -i '' 's/^\/\\begin{align*}/g'newFile
sed -i '' 's/\/\\end{align*}/g'   $newFile sed -i '' 's/\$:/\\end{align*}/g'   \$newFile