Using tikz graphdrawing library within RMarkdown … Need to use lualatex engine, but can't get it to work

亡梦爱人 提交于 2019-11-27 08:34:30

问题


I have the following code in an rmd file which leverages tikz for diagrams:

---
title: "TestNonTufteLua"
author: "Me"
output:
  pdf_document :
    latex_engine: lualatex
---

Prove tikz works:
```{r tikTest1, engine = "tikz"}
\usetikzlibrary{shapes}
\begin{tikzpicture}
    \node[ellipse, draw=black, align = center] (Data) {Data $y_{n}$};
\end{tikzpicture}
```

Then, when you set `eval = TRUE` in the below code, it will not work. 
```{r tikTest2, eval = FALSE, engine = "tikz"}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
    % A and F are horizontally aligned if you also set weight=0.5 for A -- C.
    A -- [minimum layers=2] C -- F,
    { [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
```

When changing to eval=TRUE in the second chunk, I get the following error:

Quitting from lines 24-29 (testNonTufteLua.Rmd) Error: running 'texi2dvi' on '.\tikz36747a021b22.tex' failed

LaTeX errors: rarygraphdrawing.code.tex:22: Package pgf Error: You need to run LuaTeX to use the graph drawing library.

This error occurs when using the knit button from RStudio or using render("testNonTufteLua.Rmd", output_format = pdf_document(keep_tex = TRUE, latex_engine = "lualatex"). I have also experimented with setting options(tikzDefaultEngine = "luatex") to get tikzDevice to handle it properly, but it still does not work. I just can't seem to get the graphdrawing library to work even though the tikz-shapes library can be loaded and also that the rest of the document seems to be compiled with lualatex. Thanks for any help!!


回答1:


Update: Meanwhile knitr no longer uses tools::texi2dvi but tinytex::latexmk. One therefore has to use options(tinytex.engine = 'lualatex') in a set-up chunk.


This is rather tricky, since you are not using tikzDevice but the tikz engine, which uses tools::texi2dvi to convert to PDF. You can change this using options(texi2dvi = "lualatex"). However, the default template does not work with LuaLaTeX. I have therefore created a modified template:

\RequirePackage{luatex85}
\documentclass{article}
\usepackage[luatex,active,tightpage]{preview}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{preview}
%% TIKZ_CODE %%
\end{preview}
\end{document}

And specify that file with engine.opts = list(template = "tikz2pdf.tex"). Putting it all together here my working file:

---
title: "TestNonTufteLua"
author: "Me"
output:
  pdf_document :
    latex_engine: lualatex
---

```{r}
options(texi2dvi = "lualatex")
```

```{r tikTest2, eval = TRUE, engine = "tikz", engine.opts = list(template = "tikz2pdf.tex")}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
    % A and F are horizontally aligned if you also set weight=0.5 for A -- C.
    A -- [minimum layers=2] C -- F,
    { [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
```

Result:

References:

  • how to set engine options
  • preview and LuaLaTeX
  • knitr using texi2pdf


来源:https://stackoverflow.com/questions/49212269/using-tikz-graphdrawing-library-within-rmarkdown-need-to-use-lualatex-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!