title: Garbled or missing LaTeX math formulas in RMarkdown->PDF knitr-converted document
body: <p>I'm using Rstudio to write an RMarkdown document that I turn into a PDF using MiKTeX on Windows through knitr and pandoc. LaTeX engine is xelatex.</p>
  
  <p>My .Rmd file YAML header:</p>
  
  <pre><code>---
  title: "My Title"
  author: "Me"
  geometry: margin=2cm
  output:
    pdf_document:
      fig_caption: no
      fig_height: 4
      fig_width: 6
      latex_engine: xelatex
    html_document:
      css: styles.css
      keep_md: no
  fontsize: 10pt
  ---
  </code></pre>
  
  <p>In the text I have</p>
  
  <pre><code>...parameter $\lambda=0.2$ with...
  </code></pre>
  
  <p>and I get "parameter   = 0 2 with" -- both the lambda and even the dot are missing.</p>
  
  <p>This line</p>
  
  <pre><code>mean of $\frac{1}{\lambda}=5$ of the
  </code></pre>
  
  <p>turns into </p>
  
  <p><img src="https://i.stack.imgur.com/Ay7p6.png" alt="enter image description here"></p>
  
  <hr>
  
  <p>EDIT: When I try to add as an experiment (I'm on Windows)</p>
  
  <pre><code>mainfont: Arial
  </code></pre>
  
  <p>to the YAML header I get a pandoc error</p>
  
  <pre><code>! Undefined control sequence.
  \fontspec_calc_scale:n ...ec_tmpb_dim }\fp_div:Nn 
                                                    \l_fontspec_tmpa_fp {\l_fo...
  l.18     \setmainfont{Arial}
  
  pandoc.exe: Error producing PDF from TeX source
  Error: pandoc document conversion failed with error 43
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Running <code>pdflatex report1.pdf</code> produces a correct PDF.</p>
  
  <p>RStudio runs <code>"C:/Program Files/RStudio/bin/pandoc/pandoc" report1.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output report1.pdf --template "D:\Documents\R\win-library\3.2\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine xelatex</code></p>
  
  <p>So it seems the elsewhere recommended engine "xelatex" produces faulty output, while "pdflatex" works. Unfortunately I forgot the reasons given by some sources incl. SO that I followed a few days ago when switching to xelatex, I only remember it was highly recommended to use that with knitr in RStudio rather than pdflatex.</p>