将tikz图像添加到Quarto并进行交叉引用。

huangapple go评论61阅读模式
英文:

Adding tikz image to Quarto with cross-reference

问题

I'm providing the Chinese translation of the text you provided without code:

我正在尝试将一个TikZ图片添加到Quarto文档中。我可以成功地做到这一点,但当我尝试添加交叉引用时,它失败了。这必须编织成PDF而不是HTML。

这是一个没有交叉引用的可工作的图。请告诉我是否有任何建议!

英文:

I'm trying to add a tikz picture to a Quarto document. I can do this successfully but when I try to add a cross-reference it fails. This has to knit to PDF not HTML.

Here is a working figure without the cross-reference. Please let me know if you have any advice!

---
title: "Testing"
format: pdf
editor: visual
---

See @fig-my-figure for example.

```{r, engine = 'tikz'}
#| echo: false
#| fig.align: center
#| 
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathreplacing}

\begin{tikzpicture}
\draw (0,0)node(a){} -- (10,0) node (b) {} ;
\foreach \x in  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} % edit here for the vertical lines
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
\foreach \x in {0, 0.2, 0.4, 0.6, 0.8, 1} % edit here for the numbers
\draw[shift={(\x*10,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below]
{$\x$};
\node at (8, 0.5) (eq1) {$\textcolor{red}{\boldsymbol{SQ}}$};
\node at (4, 0.5) (eq2) {$\textcolor{purple}{\boldsymbol{G_i(0)}}$}; 
\node at (7, 0.5) (eq2) {$\textcolor{purple}{\boldsymbol{G_i(1)}}$}; 
\node at (3, 0.5) (eq3) {$\textcolor{blue}{\boldsymbol{P}}$};
\node at (0, 0.5) (eq4) {$\textcolor{black}{\boldsymbol{x_i}}$};
\draw[decorate, decoration={brace, amplitude=6pt, mirror},] ([yshift=0.5cm]4,0.5)-- node[above=0.25cm]
{\shortstack{Text}}([yshift=0.5cm]3,0.5);
\draw[decorate, decoration={brace, amplitude=6pt},] ([yshift=-1cm]7,0)-- node[below=0.25cm]
{\shortstack{Text}}([yshift=-1cm]3,0);
\end{tikzpicture}
```

答案1

得分: 1

以下是已翻译的内容:

要从代码块进行图表交叉引用,您需要提供两个内容:(i) label(ii) fig-cap

label 是您的交叉引用标识符,对于图表,它必须以 fig- 为前缀,例如 fig-my-figure。而 fig-cap 则是您的图表标题,如果您不提供图表标题,交叉引用也将无法工作。因此,您需要同时提供它们。我建议阅读文档

请参见示例 @fig-my-figure。

#| label: fig-my-figure
#| echo: false
#| fig-align: center
#| fig-cap: "Some Caption"

\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathreplacing}

\begin{tikzpicture}
\draw (0,0)node(a){} -- (10,0) node (b) {} ;
\foreach \x in  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} % 编辑此处以添加垂直线
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
\foreach \x in {0, 0.2, 0.4, 0.6, 0.8, 1} % 编辑此处以添加数字
\draw[shift={(\x*10,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below]
{$\x$};
\node at (8, 0.5) (eq1) {$\textcolor{red}{\boldsymbol{SQ}}$};
\node at (4, 0.5) (eq2) {$\textcolor{purple}{\boldsymbol{G_i(0)}}$}; 
\node at (7, 0.5) (eq2) {$\textcolor{purple}{\boldsymbol{G_i(1)}}$}; 
\node at (3, 0.5) (eq3) {$\textcolor{blue}{\boldsymbol{P}}$};
\node at (0, 0.5) (eq4) {$\textcolor{black}{\boldsymbol{x_i}}$};
\draw[decorate, decoration={brace, amplitude=6pt, mirror},] ([yshift=0.5cm]4,0.5)-- node[above=0.25cm]
{\shortstack{文本}}([yshift=0.5cm]3,0.5);
\draw[decorate, decoration={brace, amplitude=6pt},] ([yshift=-1cm]7,0)-- node[below=0.25cm]
{\shortstack{文本}}([yshift=-1cm]3,0);
\end{tikzpicture}
英文:

For figure cross-reference to work from a code chunk, you need to provide two things, (i) label and (ii) fig-cap.

The label is your cross reference identifier and for figure it must be prefixed by fig-, for example, fig-my-figure. And fig-cap is just your figure caption and if you do not provide figure caption, cross reference will not work either. So you need them both. I would recommend reading the documentation.

---
title: "Testing"
format: pdf
---
    
See @fig-my-figure for example.
    
```{r, engine = 'tikz'}
#| label: fig-my-figure
#| echo: false
#| fig-align: center
#| fig-cap: "Some Caption"


\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathreplacing}

\begin{tikzpicture}
\draw (0,0)node(a){} -- (10,0) node (b) {} ;
\foreach \x in  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} % edit here for the vertical lines
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
\foreach \x in {0, 0.2, 0.4, 0.6, 0.8, 1} % edit here for the numbers
\draw[shift={(\x*10,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below]
{$\x$};
\node at (8, 0.5) (eq1) {$\textcolor{red}{\boldsymbol{SQ}}$};
\node at (4, 0.5) (eq2) {$\textcolor{purple}{\boldsymbol{G_i(0)}}$}; 
\node at (7, 0.5) (eq2) {$\textcolor{purple}{\boldsymbol{G_i(1)}}$}; 
\node at (3, 0.5) (eq3) {$\textcolor{blue}{\boldsymbol{P}}$};
\node at (0, 0.5) (eq4) {$\textcolor{black}{\boldsymbol{x_i}}$};
\draw[decorate, decoration={brace, amplitude=6pt, mirror},] ([yshift=0.5cm]4,0.5)-- node[above=0.25cm]
{\shortstack{Text}}([yshift=0.5cm]3,0.5);
\draw[decorate, decoration={brace, amplitude=6pt},] ([yshift=-1cm]7,0)-- node[below=0.25cm]
{\shortstack{Text}}([yshift=-1cm]3,0);
\end{tikzpicture}
```

答案2

得分: 0

根据Quarto文档,如果你要渲染成PDF,应该可以直接引用外部的.tex文件,而无需使用R。

但我自己似乎无法使其工作。

英文:

According to the Quarto documentation, you should be able to reference an external .tex file directly if you are rendering to pdf, removing the need for R.

I can't seem to get it to work myself though.

huangapple
  • 本文由 发表于 2023年5月18日 12:30:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定