code: def latex2png(snippet, outfile):
      
      pngimage = os.path.join(IMAGEDIR, outfile + '.png')
      texdocument = os.path.join(IMAGEDIR, 'tmp.tex')
      with open(texdocument, 'w') as doc:
          doc.write(LATEX_DOC % (snippet))
      environment = os.environ
      environment['shell_escape_commands'] = \
          "bibtex,bibtex8,kpsewhich,makeindex,mpost,repstopdf," + \
          ','.join(
              os.path.basename(n) for n in chain.from_iterable(
                  iglob(os.path.join(chemin, 'gregorio*'))
                  for chemin in os.environ["PATH"].split(os.pathsep)
              )
          )
      proc = Popen(
          ["lualatex", '-output-directory=' + IMAGEDIR, texdocument],
          stdin=PIPE,
          stdout=STDERR,
          env=environment
      )
      proc.communicate()
      proc.stdin.close()
      call(["pdfcrop", os.path.join(IMAGEDIR, "tmp.pdf")], stdout=STDERR)
      call(
          [
              "gs",
              "-sDEVICE=pngalpha",
              "-r144",
              "-sOutputFile=" + pngimage,
              os.path.join(IMAGEDIR, "tmp-crop.pdf"),
          ],
          stdout=STDERR,
      )
docs: Compiles a LaTeX snippet to png