问题描述
我已经成功地在 R/exams .Rmd
文件中包含了几个在 TikZ 中制作的图形。当我尝试使用 pgfplots
在 include_tikz()
下包含绘图时,不会发生同样的情况。每当包含 \begin {axis}
和 \end {axis}
时,请注意错误“!LaTeX 错误:环境轴未定义”。
在 RStudio 控制台中,图例出现:“这是 pdfTeX,版本 3.14159265-2.6-1.40.21(TeX Live 2020)(预加载格式 = pdflatex)受限 \ write18 enabled.entering extended mode”,即使在 TexStudio 中启用写-18。当我包含除 pgfplots
之外的其他 TikZ 图表时,这些消息都不会出现。
任何 TikZ 图形在 TexMaker 或 TexStudio 中运行时都可以工作,这表明它不是缺少 LaTeX 库或包的问题。
我包含了一部分改编自 https://www.latex4technics.com/?note=1HCT 的代码:
```{r datos1,echo = FALSE,results = "hide"}
library(exams)
typ <- match_exams_device()
image01<-'
\\begin{tikzpicture}
\\begin{axis}[legend pos=south east]
\\addlegendimage{empty legend}
\\addplot {sqrt(x)};
\\addplot {ln(x)};
\\addlegendentry{\\hspace{-.6cm}\\textbf{A title}}
\\addlegendentry{$\\sqrt{x}$}
\\addlegendentry{$\\ln{x}$}
\\end{axis}
\\end{tikzpicture}
'
```
```{r grafica01,results = "asis"}
include_tikz(image01,name = "grafiko1",markup =
"markdown",format = typ,library = c("arrows"),packages =
"booktabs",width = "7cm",header = "\\usepackage{/home/r-
exams/Documentos/NuevoRStudio/Rmarkdowns/
esqueleto/exercises/schoice/
LaboratorioTikZ/3dplot}")
```
解决方法
答案就在您的问题标题中。您需要包含 pgfplots
包:
include_tikz(image01,packages = "pgfplots",...)
不需要您问题中调用的其他 packages
、library
和 header
参数。
原因是,对于 include_tikz()
,您只需在链接的完整 .tex 文件中使用 {tikzpicture}
代码:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
...
\end{tikzpicture}
\end{document}
注意第二行中的 \usepackge{pgfplots}
!