使用 fontspec 包时编织包含 tikz 块的 rmarkdown 文件

Knit rmarkdown files containing tikz chunks while using fontspec package

这是关于 then one. I am trying to include someTikZ code into an rmarkdown file. The solved the problem about using bullets in node label whereas the 的 follow-up 问题添加了使用 varwidth 而不是 minipage 环境。

添加 fontspec 包引发了另一个我似乎无法解决的问题。每当我在列表中添加包时,都会出现以下错误:

!致命包 fontspec 错误:fontspec 包需要 XeTeX 或 (字体规范)LuaTeX。 (字体规范)<br> (fontspec) 你必须改变你的排版引擎, (fontspec) 例如 "xelatex" 或 "lualatex" 而不是 (fontspec) "latex" 或 "pdflatex".

根据前面的答案,代码由以下 2 个文件组成:

---
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}
```
\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}

从上面的错误消息中,我了解到我处于 pdflatex 模式,而 fontspec 要求我处于 xelatexlualatex 模式。鉴于我的 header 中有 latex_engine: xelatex,这让我有以下问题:

我一直在搜索,一个建议(对我来说并不太复杂)是确保全局/项目设置设置为 weave rnw files using: knitrTypeset LateX into pdf using: XeLaTex。但是,这并没有真正帮助解决我的问题。

如 中所述,您必须通过在 set-up 中使用 options(tinytex.engine = 'xelatex') 来告诉 tinytex::latexmk 使用 xelatex 而不是 pdflatex大块。

此外,您应该更新 tikz2pdf.tex 以与 XeTeX 兼容:

  • 从包 preview
  • 中删除选项 pdftex
  • 删除包 inputenc

经过这些更改,您的文档对我有用。