Skip to content

Instantly share code, notes, and snippets.

@jinwei233
Forked from wx13/latex2png.sh
Created November 15, 2017 07:33
Show Gist options
  • Save jinwei233/9b58b6814808c3861caa93008135be0d to your computer and use it in GitHub Desktop.
Save jinwei233/9b58b6814808c3861caa93008135be0d to your computer and use it in GitHub Desktop.
bash script to convert a latex equation into a png image
#!/bin/bash
alias latex='/usr/bin/latex'
alias dvipng='/usr/bin/dvipng'
alias convert='/usr/bin/convert'
#choose a number from 75-300
density=300
if [ $# != 2 ]
then
echo "Usage: $0 output_file latex_code"
exit
fi
outfile=$1
code=$2
tempfile=latex2png_temp.tex
# write latex file
echo "\documentclass[fleqn]{article}" > ${tempfile}
echo "\usepackage{amssymb,amsmath,bm}" >> ${tempfile}
echo "\usepackage[latin1]{inputenc}" >> ${tempfile}
echo "\begin{document}" >> ${tempfile}
echo "\thispagestyle{empty}" >> ${tempfile}
echo -n "\$\$ \displaystyle \mathindent0cm \parindent0cm " >> ${tempfile}
echo ${code} " \$\$" >> ${tempfile}
echo "\end{document}" >> ${tempfile}
latex -interaction=nonstopmode ${tempfile}
dvipng -o ${outfile} -q -T tight -bg transparent -D 300 ${tempfile%.tex}.dvi
rm -f ${tempfile%.tex}.aux ${tempfile%.tex}.dvi ${tempfile%.tex}.log ${tempfile}
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment