Rmarkdown 中的 Tikz `正多边形`

Rmarkdown 中的 Tikz `正多边形`

我想用节点三角形制作 tikz 图形。我在网上找到的大多数资源都使用\usetikzlibrary{shapes}及其regular polygon。Rmarkdown 似乎无法使用它。

这将在 overleaf 中起作用:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
\begin{figure}
    \centering
\begin{tikzpicture}
\node [draw, circle,minimum size=1cm,thick] (cir) at (0,0) {Test};
\node [draw,regular polygon, regular polygon sides=3, inner sep=0pt, minimum size=1cm,thick] (tri) at (0, -2) {Test};
\end{tikzpicture}
\end{figure} 
\end{document}

但在 Rmarkdown 中却不行。

---
title: "Untitled"
author: "My name"
output: pdf_document
header-includes: 
  - \usepackage{tikz}
  - \usetikzlibrary{shapes}
---

```{tikz}
\begin{tikzpicture}
\node [draw, circle,minimum size=1cm,thick] (cir) at (0,0) {Test};
\node [draw,regular polygon, regular polygon sides=3, inner sep=0pt, minimum size=1cm,thick] (tri) at (0, -2) {Test};
\end{tikzpicture}
```

我收到了这个错误。

processing file: tests.Rmd

! Package pgfkeys Error: I do not know the key '/tikz/regular polygon' and I am
 going to ignore it. Perhaps you misspelled it.

Quitting from lines 11-15 (tests.Rmd) 
Erreur : LaTeX failed to compile tikz2b045722257b.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See tikz2b045722257b.log for more info.

答案1

通过查看邮政,我已经尝试写入\usetikzlibrary{shapes}并且\begin{tikzpicture}成功\end{tikzpicture}了!

解决方案如下:

title: "Untitled"
author: "My name"
output: pdf_document


{tikz}
\begin{tikzpicture}
\usetikzlibrary{shapes}
\node [draw, circle,minimum size=1cm,thick] (cir) at (0,0) {Test};
\node [draw,regular polygon, regular polygon sides=3, inner sep=0pt, minimum size=1cm,thick] (tri) at (0, -2) {Test};
\end{tikzpicture}

相关内容