title: knitr pandoc: "cannot produce pdf output with pdf writer"
body: <p><strong>Up front</strong>: using <code>pandoc()</code> in knitr, it complains when trying to compile .md or .Rmd into a PDF.</p>
  
  <p>I'm streamlining the process for <em>reproducible research</em>, as has been documented in many places. I'm using <code>pandoc</code> and <code>knitr</code> and producing great documents. I'm also trying to streamline for some co-workers who are not as adept with programming, yet we're trying to use similar files. There are several options for "user friendly" markdown-centric editors, and for several reasons I'm leaning on RStudio (for them, emacs/ess for me, but that's different).</p>
  
  <p>My workflow: give them a markdown (.md or .Rmd) file and have them be able to make changes and optionally re-render it into a PDF. Unfortunately, RStudio does not (AFAICT) allow setting templates or other arbitrary pandoc configuration parameters (e.g., chapters, number-sections), so using <code>pandoc()</code> in R/knitr makes a lot of sense here.</p>
  
  <p>Using <code>whitepaper.Rmd</code> as the input file, I run <code>pandoc('whitepaper.Rmd', 'pdf')</code> in R and immediately get:</p>
  
  <pre><code>&gt; pandoc('whitepaper.Rmd', 'pdf')
  executing pandoc  -t latex --standalone --smart --number-sections --template=report.tex -f markdown -t pdf -o whitepaper.pdf "whitepaper.Rmd"
  pandoc.exe: cannot produce pdf output with pdf writer
  Error in (function (input, format, ext, cfg)  : conversion failed
  </code></pre>
  
  <p>I explicitly have "t:latex" in my knitr-specific header, though without it, <code>pandoc()</code> is still adding "-t pdf" to the system call, something that <code>pandoc.exe</code> does not accept.</p>
  
  <p>With troubleshooting, the command works just fine if I remove <code>'-t pdf'</code>, so it seems that there is nothing wrong with the input file itself:</p>
  
  <pre><code>&gt; system('pandoc  -t latex --standalone --smart --number-sections --template=report.tex -f markdown -o whitepaper.pdf "whitepaper.Rmd"')
  </code></pre>
  
  <p>There have been numerous other conversations regarding this topic: <a href="https://stackoverflow.com/questions/14586177/knitr-to-produce-pdf-output-file">14586177</a>, <a href="https://stackoverflow.com/questions/14508429/unable-to-run-knitr-to-produce-a-pdf-output-file">14508429</a>, <a href="https://stackoverflow.com/questions/15258233/using-table-caption-on-r-markdown-file-using-knitr-to-use-in-pandoc-to-convert-t">15258233</a>, and the heavily-discussed <a href="https://stackoverflow.com/questions/11025123/how-to-convert-r-markdown-to-pdf">11025123</a>. They all resolve to solutions that require command-line work, extra middle-steps, external Makefiles, or <code>knit2pdf()</code> (which uses texi2pdf, not desired).</p>
  
  <p>The constraints as I see them:</p>
  
  <ul>
  <li>operate easily within the R environment;</li>
  <li>take advantage of Yihui's "<code>&lt;!--pandoc ... --&gt;</code>" in-file configuration (which allows
  me to continue to switch arbitrarily between my templates, for one of several examples);</li>
  <li>preferably, execute this with a single "standardized" command (i.e., "<code>pandoc('whitepaper.Rmd', 'pdf')</code>").</li>
  </ul>
  
  <p>... so that, once the parameters are set in-file, editing and re-rendering is relatively brain-dead.</p>
  
  <p>I can patch and overwrite Yihui's <code>knitr:::pandoc_one()</code> to remove the offending addition of '-t' and format, but I wonder what side-effects that might have elsewhere. This solution isn't sustainable nor "The Right Way (tm)".</p>
  
  <p>Suggestions for "Right Ways (tm)" to solve this problem? Am I missing an easy/obvious solution?</p>
  
  <p>BTW: thanks, Yihui Xie, for knitr, and John MacFarlane for pandoc. Awesomeness!
  (Perhaps I could submit patch suggestions to either or both to work around for my use-case, though if it's just me then it might not be worthwhile.)</p>