![在文本中引用 TikZ 图形的自定义标签](https://linux22.com/image/424261/%E5%9C%A8%E6%96%87%E6%9C%AC%E4%B8%AD%E5%BC%95%E7%94%A8%20TikZ%20%E5%9B%BE%E5%BD%A2%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A0%87%E7%AD%BE.png)
我使用该包绘制了下图tikz
。我需要在箭头上添加标签并在文本中引用它们。我该怎么做?如果有人能就此问题提供反馈,我将不胜感激。
同时,相应的代码是
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\tikzset{every picture/.style={line width=0.75pt}}
\begin{document}
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw (140,145) -- (238.59,243.59) ;
\draw [shift={(240,245)}, rotate = 225] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
\draw (264,237) -- (377.23,98.55) ;
\draw [shift={(378.5,97)}, rotate = 489.28] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
\draw (404,82) .. controls (444,52) and (422.5,383) .. (554.5,244) ;
\draw [shift={(554.5,244)}, rotate = 493.52] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
\draw (122,127) node [anchor=north west][inner sep=0.75pt] [align=left] {$ $};
\draw (122,124) node [anchor=north west][inner sep=0.75pt] [align=left] {$A$};
\draw (248,241) node [anchor=north west][inner sep=0.75pt] [align=left] {$B$};
\draw (122,133) node [anchor=north west][inner sep=0.75pt] [align=left] {$ $};
\draw (382,74) node [anchor=north west][inner sep=0.75pt] [align=left] {$C$};
\draw (561,226) node [anchor=north west][inner sep=0.75pt] [align=left] {$D$};
\end{tikzpicture}
\end{document}
答案1
我稍微整理了一下你的代码并标记了箭头,但是,我目前不确定如何在文本中引用箭头。我会看看我是否能想出解决这个问题的方法。在此之前,这是你的图形的清理版本。请注意,我已将你的坐标更改为比当前更小的坐标,因为 Ti钾Z 通常用作cm
单位。这就是为什么您需要将其x=0.75pt,y=0.75pt
作为参数添加到您的tikzpicture
环境中。如果出于某种原因,您仍然需要这些巨大的坐标,请随意更改提供的代码。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node at (0,0) (A) {$A$};
\node at (2,-1) (B) {$B$};
\node at (4,2) (C) {$C$};
\node at (8,0) (D) {$D$};
\draw[->] (A) -- (B) node [midway, above, sloped] (AtoB) {$A\rightarrow B$};
\draw[->] (B) -- (C) node [midway, above, sloped] (BtoC) {$B\rightarrow C$};
\draw[->] (C) .. controls ++(1,1) and ++(-2,-2) .. (D) node [midway, below, sloped] (CtoD) {$C\rightarrow D$};
\end{tikzpicture}
\end{document}
编辑
我添加了一种方法,通过将内容写入文件来引用文本中的标签aux
。感谢 Henrik Bøgelund Lavstsen 提出这个想法,这线。
\documentclass{article}
\usepackage{tikz}
\usepackage{hyperref}
\makeatletter
\newcommand{\customlabel}[2]{%
\protected@write \@auxout {}{%
\string \newlabel {#1}{{#2}{\thepage}{#2}{#1}{}}%
}%
\hypertarget{#1}{#2}%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node at (0,0) (A) {$A$};
\node at (2,-1) (B) {$B$};
\node at (4,2) (C) {$C$};
\node at (8,0) (D) {$D$};
\draw[->] (A) -- (B) node [midway, above, sloped] (AtoB) {\customlabel{node:AtoB}{$A\rightarrow B$}};
\draw[->] (B) -- (C) node [midway, above, sloped] (BtoC) {\customlabel{node:BtoC}{$B\rightarrow C$}};
\draw[->] (C) .. controls ++(1,1) and ++(-2,-2) .. (D) node [midway, below, sloped] (CtoD) {\customlabel{node:CtoD}{$C\rightarrow D$}};
\end{tikzpicture}
Let's reference the arrow with the identifyer \texttt{AtoB}, shall we?\\
Here it is: \ref{node:AtoB}
\end{document}