Search code examples
latexr-markdownknitrtikzfontspec

Knit rmarkdown files containing tikz chunks while using fontspec package


This is a follow-up question on this then this one. I am trying to include someTikZ code into an rmarkdown file. The first question solved the problem about using bullets in node label whereas the second question added the use of varwidth instead of minipage environment.

Adding the fontspec package raised another problem I can't seem to solve. The following error is raised whenever I added the package in my list:

! Fatal Package fontspec Error: The fontspec package requires either XeTeX or (fontspec) LuaTeX. (fontspec)
(fontspec) You must change your typesetting engine to, (fontspec) e.g., "xelatex" or "lualatex"instead of (fontspec) "latex" or "pdflatex".

Following the previous answers, the code is composed of the following 2 files:

  • Title.Rmd:
---
title: "Title"
author: "Me"
output:
  bookdown::pdf_document2:
    keep_tex: yes
    latex_engine: xelatex
---


```{tikz tikz-ex, echo=FALSE, fig.cap = "Funky tikz", fig.ext = 'pdf', cache=FALSE, eval=TRUE, engine.opts = list(template = "tikz2pdf.tex")}
\usetikzlibrary{
shapes,
arrows
}
\begin{tikzpicture}

\node[stile] (a){
\begin{minipage}{2.5cm}
p
\begin{myBullets}
\item first item
\item second item
\end{myBullets}
\end{minipage}
};

\end{tikzpicture}
```
  • tikz2pdf.tex:
\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\usepackage[utf8]{inputenc}
\usepackage[skins]{tcolorbox}
\usepackage{
tikz,
enumitem,
xcolor
}

\usepackage{fontspec} % new adding to previous answers
\setromanfont{Times New Roman}

\begin{document}

\definecolor{BulletsColor}{rgb}{0, 0, 0.9}
\definecolor{myColor}{rgb}{0.98, 0.94, 0.9}
\newlist{myBullets}{itemize}{1}

\setlist[myBullets]{
  label=\textcolor{BulletsColor}{\textbullet},
  leftmargin=*,
  topsep=0ex,
  partopsep=0ex,
  parsep=0ex,
  itemsep=0ex,
  before={\color{BulletsColor}\itshape}
}

\tikzstyle {stile} = [
ellipse,
draw=BulletsColor,
fill=myColor,
thick,
inner sep=0pt,
text centered, 
align=center
]

\begin{preview}
%% TIKZ_CODE %%
\end{preview}

\end{document}

From the error message above, I understand I am in pdflatex mode while fontspec requires me to be in xelatex or lualatex mode. Given that I have latex_engine: xelatex in my header, this torments me with the following questions:

  • why does setting latex_engine: xelatex in the header is unsufficient here?
  • Does the knitr chunk not care about the latex_engine: xelatex in the header? If so, how can I solve that?
  • What am I missing?

I have been searching around and one suggestion (that is not overly complicated to me) is to make sure the global / project settings are set to weave rnw files using: knitr, Typeset LateX into pdf using: XeLaTex. But, this has not really helped solving my problem.


Solution

  • As explained in https://stackoverflow.com/a/51143900/8416610 you have to tell tinytex::latexmk to use xelatex instead of pdflatex by using options(tinytex.engine = 'xelatex') in a set-up chunk.

    In addition, you should update your tikz2pdf.tex for compatibility with XeTeX:

    • Remove option pdftex from package preview
    • Remove package inputenc.

    With these changes your document works for me.