Skip to content

Instantly share code, notes, and snippets.

@aji
Created April 17, 2013 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aji/5408755 to your computer and use it in GitHub Desktop.
Save aji/5408755 to your computer and use it in GitHub Desktop.
#!/bin/sh
die() {
echo "$*" 1>&2
exit 1
}
export TMPDIR=${TMPDIR:=/tmp}
export PDFLATEX=$(which pdflatex)
export PDF2PS=$(which pdf2ps)
export PS2EPS=$(which ps2eps)
export CONVERT=$(which convert)
eqn="$*"
texify_dir="$TMPDIR/texify"
if [ -z "$eqn" ]; then
echo "usage: $0 <equation>"
echo "creates a .png of the equation"
exit 1
fi
[ -n "${PDFLATEX}" ] || die "Don't know where pdflatex is!"
[ -n "${PDF2PS}" ] || die "Don't know where pdf2ps is!"
[ -n "${PS2EPS}" ] || die "Don't know where ps2eps is!"
[ -n "${CONVERT}" ] || die "Don't know where convert is!"
if [ ! -d "${texify_dir}" ] && ! mkdir -p "${texify_dir}"; then
die "Could not create ${texify_dir}!"; fi
name=$(mktemp --dry-run "${texify_dir}/XXXXXXX")
input="$(echo "${eqn}" | sed 's/\\end{equation\*}//g')"
input="\documentclass[12pt]{article}
\batchmode
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{array}
\begin{document}
\pagenumbering{gobble}
\begin{equation*}
${input}
\end{equation*}
\end{document}"
echo "${input}" > "${name}.tex"
"${PDFLATEX}" "-output-directory=${texify_dir}" "${name}.tex" >/dev/null
"${PDF2PS}" "${name}.pdf" "${name}.ps" >/dev/null
"${PS2EPS}" < "${name}.ps" > "${name}.eps" 2>/dev/null
"${CONVERT}" -density 500x500 -border 50x50 "${name}.eps" "${name}.png" >/dev/null
for ext in tex aux log pdf ps eps; do
rm -f "${name}.${ext}"; done
echo "${name}.png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment