Skip to content

Instantly share code, notes, and snippets.

@opieters
Created February 26, 2016 17:59
Show Gist options
  • Save opieters/ad7abe0cc6a1f15b88a1 to your computer and use it in GitHub Desktop.
Save opieters/ad7abe0cc6a1f15b88a1 to your computer and use it in GitHub Desktop.
import os, subprocess, shutil
file_dir = "./_includes/latex-figures"
tmppath = subprocess.Popen("echo $TMPDIR", shell=True, stdout=subprocess.PIPE).stdout.read().replace('\n','')
# find all files
files = []
for f in os.listdir(file_dir):
if f.endswith(".tex"):
files += [f]
else:
# just copy all other file types to tmppath
shutil.copyfile(os.path.join(file_dir, f), os.path.join(tmppath, f))
documentpreamble = """\\documentclass{standalone}
\\usepackage{standalone}
\\usepackage[utf8]{inputenc}
\\usepackage{tikz}
\\usepackage{pgfplots}
\\pgfplotsset{compat=newest}
\\usepackage{circuitikz}
\\usepackage{SIunits}
\\usepackage{longtable}
\\usepackage{pgfplotstable}
\\usepackage{booktabs}
\\usepackage{dynamicnumber}
"""
for lfile in files:
# read document content
with open (os.path.join(file_dir,lfile), "r") as f:
documentcontent=f.readlines()
documentcontent = ''.join(documentcontent)
# create latex source
document = documentpreamble + "\\begin{document}\n" + documentcontent + "\\end{document}\n"
print("Writing to file...")
with open(os.path.join(tmppath, "latex-figure.tex"), 'w') as f:
f.write(document)
print("Done")
# create pdf
os.system("pushd $TMPDIR; pdflatex latex-figure.tex; popd")
# create svg image
os.system("pushd $TMPDIR; pdf2svg latex-figure.pdf latex-figure.svg; popd")
# copy to correct dicrectory
lfile, file_extension = os.path.splitext(lfile)
subprocess.Popen("cp " + os.path.join(tmppath,"latex-figure.svg") + " ./assets/images/latex/"+lfile+".svg", shell=True, stdout=subprocess.PIPE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment