如何在xaringan中运行tikz绘图?报错问题求助
解决xaringan中tikz代码显示异常及报错问题
问题成因
你遇到的两个问题:代码直接显示在幻灯片、报错Error in if (to_svg) tinytex::latexmk(texf, "latex") else tinytex::latexmk(texf) : argument is of length zero,主要源于:
- tikz代码块缺少必要参数,导致knitr未正确编译代码,直接输出原内容
- LaTeX环境缺失tikz相关依赖组件,或TinyTeX配置异常
解决步骤
1. 安装依赖包与LaTeX组件
在R控制台执行以下命令,确保所需工具安装完整:
install.packages(c("knitr", "tikzDevice")) tinytex::tlmgr_install(c("tikz", "pgf", "xcolor"))
若未安装TinyTeX,先运行tinytex::install_tinytex()完成基础安装。
2. 修正tikz代码块参数
原代码块仅指定了engine='tikz',需补充参数让knitr正确编译并隐藏代码:
将代码块开头修改为:
```{r, engine='tikz', echo=FALSE, fig.align='center'}
echo=FALSE: 禁止在幻灯片中显示tikz代码engine='tikz': 指定用tikz引擎编译图形代码fig.align='center': 让生成的图形居中显示(可选,按需添加)
3. 确认tikz代码转义正确性
在Rmarkdown代码块中,tikz命令前的反斜杠需用双反斜杠\\转义,你的原代码这部分是正确的,无需修改。
修正后的完整代码
--- title: "Econ" subtitle: "Lecture 1" author: "Instructor" institute: "College" date: "`r Sys.Date()`" output: xaringan::moon_reader: css: [default, metropolis, metropolis-fonts, "styles.css"] lib_dir: libs nature: highlightStyle: arta highlightLines: true countIncrementalSlides: false --- ```{r, engine='tikz', echo=FALSE, fig.align='center'} \\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt \\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1] %uncomment if require: \\path (0,379); %set diagram left start at 0, and has height of 379 %Rounded Rect [id:dp1857476422681199] \\draw [fill={rgb, 255:red, 241; green, 192; blue, 192 } ,fill opacity=1 ] (228,123) .. controls (228,114.16) and (235.16,107) .. (244,107) -- (343,107) .. controls (351.84,107) and (359,114.16) .. (359,123) -- (359,171) .. controls (359,179.84) and (351.84,187) .. (343,187) -- (244,187) .. controls (235.16,187) and (228,179.84) .. (228,171) -- cycle ; %Shape: Rectangle [id:dp08440493370073976] \\draw [fill={rgb, 255:red, 226; green, 233; blue, 201 } ,fill opacity=1 ] (415,115) -- (525,115) -- (525,179) -- (415,179) -- cycle ; %Rounded Single Corner Rect [id:dp01343887372328001] \\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.05 ] (64,129.52) .. controls (64,122.27) and (69.87,116.4) .. (77.12,116.4) -- (170,116.4) -- (170,182) -- (64,182) -- cycle ; %Straight Lines [id:da9440635149995973] \\draw (171,144) -- (218,144) ; \\draw [shift={(221,144)}, rotate = 180] [fill={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.08] [draw opacity=0] (10.72,-5.15) -- (0,0) -- (10.72,5.15) -- (7.12,0) -- cycle ; %Straight Lines [id:da7316344569835018] \\draw (361,145) -- (405,145) ; \\draw [shift={(408,145)}, rotate = 180] [fill={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.08] [draw opacity=0] (10.72,-5.15) -- (0,0) -- (10.72,5.15) -- (7.12,0) -- cycle ; %Curve Lines [id:da512763546270846] \\draw (415,179) .. controls (380.35,241.37) and (217.31,242.97) .. (171.36,183.81) ; \\draw [shift={(170,182)}, rotate = 54.2] [fill={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.08] [draw opacity=0] (10.72,-5.15) -- (0,0) -- (10.72,5.15) -- (7.12,0) -- cycle ; \\draw (280.56,238.98) -- (311.32,217.88)(282.27,217.6) -- (309.61,239.25) ; % Text Node \\draw (420,125) node [anchor=north west][inner sep=0.75pt] [align=left] {{{\fontfamily{ptm}\selectfont \textit{\textbf{y} }}}\{{\fontfamily{ptm}\selectfont \textit{variables}}}}; % Text Node \\draw (75,129) node [anchor=north west][inner sep=0.75pt] [align=left] {{{\fontfamily{ptm}\selectfont \textit{\textbf{x}}}}\{{\fontfamily{ptm}\selectfont \textit{variables}}}}; % Text Node \\draw (271,138.4) node [anchor=north west][inner sep=0.75pt] {$function$}; \\end{tikzpicture}
## 额外排查步骤 若仍报错,可尝试: - 重启R会话后重新编译幻灯片 - 运行`tinytex::tlmgr_update()`更新LaTeX组件 - 检查`styles.css`文件是否存在,若不存在可移除该CSS配置项 内容的提问来源于stack exchange,提问作者jck21
相关产品推荐
相关产品推荐

