在Quarto中添加带交叉引用的Tikz图片问题求助
Quarto中Tikz图片交叉引用失败的解决方法
你的问题核心是Tikz代码块未设置对应的交叉引用标签,Quarto实现图的交叉引用需要给代码块添加特定格式的label属性,且引用标识要与标签匹配。
以下是修改后可正常实现交叉引用的完整代码:
--- title: "Testing" format: pdf editor: visual --- See @fig-my-figure for example. ```{r, engine = 'tikz'} #| echo: false #| fig.align: center #| label: fig-my-figure # 新增标签,与引用标识@fig-my-figure对应 #| fig.cap: "自定义图表标题" # 可选:添加标题后PDF会自动生成带编号的图注 \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}
关键修改说明:
- 必须在Tikz代码块的YAML选项中添加
#| label: fig-my-figure,标签名需以fig-开头,且和引用时的@fig-my-figure完全一致,Quarto才会识别为图表引用标签。 - 可选添加
#| fig.cap属性设置图标题,编译PDF时会自动生成带编号的图注,交叉引用会关联这个编号。
重新编译为PDF后,交叉引用即可正常生效。
内容的提问来源于stack exchange,提问作者Ryan Baxter-King
相关产品推荐
相关产品推荐

