4
votes

I'm using RStudio 0.99.896 on Windows 10.

I am trying to use knitr to convert a RMarkdown file to PDF using the XeLaTex engine.

I have set the Global Options and the Project Options in RStudio to build with XeLaTex

You can see that I have selected XeLaTeX in the global options (see pic) enter image description here

and I have also selected it in the project options (see pic) enter image description here

Here is a minimal reproducible example of a markdown file that fails when I call knitr:

---
header-includes:
    - \usepackage{fontspec}
output:
    pdf_document
---


```{r}
data(mtcars)
```

When I call Knit PDF on the file above, I get the following output:

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS sample1.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output sample1.pdf --template "C:\Users\xxxx\Documents\R\win-library\3.2\rmarkdown\rmd\latex\default-1.14.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" output file: sample1.knit.md

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! ! Fatal fontspec error: "cannot-use-pdftex" ! ! The fontspec package requires either XeTeX or LuaTeX to function. ! ! You must change your typesetting engine to, e.g., "xelatex" or "lualatex" ! instead of plain "latex" or "pdflatex".

You can see in the bold part of the output shown above that RStudio is still calling pdflatex instead of xelatex.

I'm not sure why that is. Any thoughts on a setting that I have missed?

1
As stated in the Rstudio preferences, the settings are for Rnw files ; here, you are working with Rmarkdown. You need to add the latex_engine: xelatex setting in the YAML front matter in every document.scoa

1 Answers

3
votes

I had this problem too.

Try this:

output:
   pdf_document:
     latex_engine: xelatex

In addition to everything else you did. This fixed it for me!