Li Hang
Li Hang

Reputation: 95

Garbled or missing LaTeX math formulas in RMarkdown->PDF knitr-converted document

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.

My .Rmd file YAML header:

---
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
---

In the text I have

...parameter $\lambda=0.2$ with...

and I get "parameter = 0 2 with" -- both the lambda and even the dot are missing.

This line

mean of $\frac{1}{\lambda}=5$ of the

turns into

enter image description here


EDIT: When I try to add as an experiment (I'm on Windows)

mainfont: Arial

to the YAML header I get a pandoc error

! 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

UPDATE:

Running pdflatex report1.pdf produces a correct PDF.

RStudio runs "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

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.

Upvotes: 2

Views: 1526

Answers (1)

PavoDive
PavoDive

Reputation: 6496

I'm posting this answer to increase the visibility of @user1983395 's comment regarding this error.

I was facing the same error as OP. pdflatex just produced the mentioned error, and xelatex produced an error whenever there was a call to a math formula ( $X_i$ or $$X_i$$ ).

After running C:\Program Files\MiKTeX 2.9\miktex\bin\x64\updmap.exe I was able to generate a pdf both with pdflatex and xelatex. Please notice that you may need to run it as administrator.

Upvotes: 2

Related Questions