在RMarkdown中使用tikz graphdrawing库...需要使用lualatex引擎,但无法使其正常工作 [英] Using tikz graphdrawing library within RMarkdown ... Need to use lualatex engine, but can't get it to work
问题描述
我在rmd
文件中有以下代码,该文件利用tikz
来绘制图表:
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 } }
};
```
在第二个块中更改为eval=TRUE
时,我得到以下内容
错误:
When changing to eval=TRUE
in the second chunk, I get the following
error:
从第24-29行退出(testNonTufteLua.Rmd)错误:正在运行 '.\ tikz36747a021b22.tex'上的'texi2dvi'失败
Quitting from lines 24-29 (testNonTufteLua.Rmd) Error: running 'texi2dvi' on '.\tikz36747a021b22.tex' failed
LaTeX错误:rarygraphdrawing.code.tex:22:软件包pgf错误:您 需要运行LuaTeX才能使用图形绘图库.
LaTeX errors: rarygraphdrawing.code.tex:22: Package pgf Error: You need to run LuaTeX to use the graph drawing library.
使用RStudio中的knit
按钮或使用render("testNonTufteLua.Rmd", output_format = pdf_document(keep_tex = TRUE, latex_engine = "lualatex")
时,会发生此错误.我也尝试过将options(tikzDefaultEngine = "luatex")
设置为使tikzDevice
正确处理,但仍然无法正常工作.即使可以加载tikz-shapes
库,我似乎也无法使graphdrawing
库正常工作,而且该文档的其余部分似乎都是使用lualatex
编译的.谢谢您的帮助!
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!!
推荐答案
更新:同时knitr
不再使用tools::texi2dvi
,而是使用tinytex::latexmk
.因此,必须在设置块中使用options(tinytex.engine = 'lualatex')
.
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.
这很棘手,因为您不是使用tikzDevice
而是使用tikz
引擎,该引擎使用tools::texi2dvi
转换为PDF.您可以使用options(texi2dvi = "lualatex")
进行更改.但是,默认模板不适用于LuaLaTeX.因此,我创建了一个修改后的模板:
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}
并使用engine.opts = list(template = "tikz2pdf.tex")
指定该文件.将所有内容放到我的工作文件中:
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 } }
};
```
结果:
参考文献:
- how to set engine options
- preview and LuaLaTeX
- knitr using texi2pdf
这篇关于在RMarkdown中使用tikz graphdrawing库...需要使用lualatex引擎,但无法使其正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!