title: How to use Python filter with Pandoc to convert md with tikz to html on Windows 8.1
body: <p>I am trying to use a Pandoc filter to convert a markdown file with a tikz picture to html. I am on Win 8.1 (and I have all the dependencies -- pdflatex, Python 2.7, ImageMagick, and the pandocfilters Python package). I am using the tikz.py script that John MacFarlane provides on <a href="https://github.com/jgm/pandocfilters" rel="nofollow">github</a>.</p>
  
  <p>I found a similar <a href="https://github.com/jgm/pandoc/issues/1096" rel="nofollow">question</a> on the Pandoc Google Group and John MacFarlane suggests wrapping the filter in a Windows batch script (the filter must be an executable). Here is my command line input (I'll provide the file contents below).</p>
  
  <pre><code>pandoc -o temp.html --filter .\tikz.bat -s temp.md
  </code></pre>
  
  <p>But I keep getting the following error.</p>
  
  <pre><code>pandoc: Failed reading: satisfyElem
  </code></pre>
  
  <p>The script generates the "tikz-images" subfolder, but it is empty, as is the resulting output file temp.html.</p>
  
  <p>How can I get this to work? FWIW, the bigger goal is for the input files to be <a href="http://rmarkdown.rstudio.com/" rel="nofollow">R Markdown</a>, but I want to understand the Pandoc Markdown to HTML process first.</p>
  
  <p>Here are the file contents.</p>
  
  <p>tikz.bat</p>
  
  <pre><code>python tikz.py %*
  </code></pre>
  
  <p>temp.md</p>
  
  <pre><code>\begin{tikzpicture}
  
  \draw [&lt;-&gt;](-3,0)--(3,0);
  \draw (-2,-.2)--(-2,.2);
  \draw (-1,-.2)--(-1,.2);
  \draw(0,-.2)--(0,.2);
  \draw (1,-.2)--(1,.2);
  \draw (2,-.2)--(2,.2);
  \node[align=left,below] at (-4.5,-0.2) {Cash flow};
  \node[align=left,above] at (-4.5,0.2) {Time period};
  \node[align=left,above] at (-2,0.2) {-2};
  \node[align=left,above] at (-1,0.2) {-1};
  \node[align=left,above] at (0,0.2) {0};
  \node[align=left,above] at (1,0.2) {+1};
  \node[align=left,above] at (2,0.2) {+2};
  \node[align=left,below] at (1,-0.2) {\$100};
  \node[align=left,below] at (2,-0.2) {\$100};
  
  \end{tikzpicture}
  
  Can this work?
  </code></pre>
  
  <p>tikz.py</p>
  
  <pre><code>#!/usr/bin/env python
  
  """
  Pandoc filter to process raw latex tikz environments into images.
  Assumes that pdflatex is in the path, and that the standalone
  package is available.  Also assumes that ImageMagick's convert
  is in the path. Images are put in the tikz-images directory.
  """
  
  import hashlib
  import re
  import os
  import sys
  import shutil
  from pandocfilters import toJSONFilter, Para, Image
  from subprocess import Popen, PIPE, call
  from tempfile import mkdtemp
  
  imagedir = "tikz-images"
  
  
  def sha1(x):
      return hashlib.sha1(x.encode(sys.getfilesystemencoding())).hexdigest()
  
  
  def tikz2image(tikz, filetype, outfile):
      tmpdir = mkdtemp()
      olddir = os.getcwd()
      os.chdir(tmpdir)
      f = open('tikz.tex', 'w')
      f.write("""\\documentclass{standalone}
               \\usepackage{tikz}
               \\begin{document}
               """)
      f.write(tikz)
      f.write("\n\\end{document}\n")
      f.close()
      p = call(["pdflatex", 'tikz.tex'], stdout=sys.stderr)
      os.chdir(olddir)
      if filetype == 'pdf':
          shutil.copyfile(tmpdir + '/tikz.pdf', outfile + '.pdf')
      else:
          call(["convert", tmpdir + '/tikz.pdf', outfile + '.' + filetype])
      shutil.rmtree(tmpdir)
  
  
  def tikz(key, value, format, meta):
      if key == 'RawBlock':
          [fmt, code] = value
          if fmt == "latex" and re.match("\\\\begin{tikzpicture}", code):
              outfile = imagedir + '/' + sha1(code)
              if format == "html":
                  filetype = "png"
              elif format == "latex":
                  filetype = "pdf"
              else:
                  filetype = "png"
              src = outfile + '.' + filetype
              if not os.path.isfile(src):
                  try:
                      os.mkdir(imagedir)
                      sys.stderr.write('Created directory ' + imagedir + '\n')
                  except OSError:
                      pass
                  tikz2image(code, filetype, outfile)
                  sys.stderr.write('Created image ' + src + '\n')
              return Para([Image([], [src, ""])])
  
  if __name__ == "__main__":
      toJSONFilter(tikz)
  </code></pre>
  
  <hr>
  
  <p><strong>Update</strong> I mention in the comments that the caps.py filter also fails with the same symptoms. Maybe I should also add the symptoms from <code>python caps.py temp.md</code>, which is invoking the filter outside of pandoc. My understanding is that this should print the caps.py file to the screen in all caps.</p>
  
  <p>However, when I run <code>python caps.py temp.md</code> from the Windows command prompt it hangs. I kill the command with <code>CTRL-C</code>, then I get the following.</p>
  
  <pre><code>C:\Users\Richard\Desktop\temp&gt;python caps.py temp.md
  Traceback (most recent call last):
    File "caps.py", line 15, in &lt;module&gt;
      toJSONFilter(caps)
  </code></pre>
  
  <p>The same occurs with <code>python tikz.py temp.md</code>. A hang, followed by:</p>
  
  <pre><code>C:\Users\Richard\Desktop\temp&gt;python tikz.py temp.md
  Traceback (most recent call last):
    File "tikz.py", line 70, in &lt;module&gt;
      toJSONFilter(tikz)
  </code></pre>
  
  <hr>
  
  <p><strong>Update 2</strong> I tried to run the Windows debugger on the command prompt, but I'm not sure that it worked. Sometime the command prompt would hang. And it seems like the debugger hangs, too. Here is the output from the debugger.</p>
  
  <pre><code>*** wait with pending attach
  Symbol search path is: *** Invalid ***
  ****************************************************************************
  * Symbol loading may be unreliable without a symbol search path.           *
  * Use .symfix to have the debugger choose a symbol path.                   *
  * After setting your symbol path, use .reload to refresh symbol locations. *
  ****************************************************************************
  Executable search path is: 
  ModLoad: 00007ff7`0d920000 00007ff7`0d97d000   C:\windows\system32\cmd.exe
  ModLoad: 00007fff`b7c20000 00007fff`b7dcc000   C:\windows\SYSTEM32\ntdll.dll
  ModLoad: 00007fff`b5c90000 00007fff`b5dce000   C:\windows\system32\KERNEL32.DLL
  ModLoad: 00007fff`b4e40000 00007fff`b4f55000   C:\windows\system32\KERNELBASE.dll
  ModLoad: 00007fff`b7b70000 00007fff`b7c1a000   C:\windows\system32\msvcrt.dll
  ModLoad: 00007fff`b3070000 00007fff`b307e000   C:\windows\SYSTEM32\winbrand.dll
  (1c7c.29a0): Break instruction exception - code 80000003 (first chance)
  *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\windows\SYSTEM32\ntdll.dll - 
  ntdll!DbgBreakPoint:
  00007fff`b7cb2cf0 cc              int     3
  </code></pre>
  
  <hr>
  
  <p><strong>Update 3</strong> Here are the files in a <a href="https://www.dropbox.com/sh/e6xgwtgfv395phr/AAD69Mse11M5IFXwBymh53Xxa?dl=0" rel="nofollow">Dropbox folder</a>. This folder has the same files that I pasted above, plus the <code>caps.py</code> file which is direct from the Pandoc filters github repo.</p>