请考虑我的最小示例。我想在节点 A 和 B 之间的线中间放置一个附加元素(在本例中为文本字段)。这如何实现?
以下哪个命令是正确的?
append after command={...}
,node contens={...}
,code={...}
或者path picture={...}
最简示例:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning, calc, fit, shadings, shadows, shapes.arrows, arrows.meta, matrix, mindmap}
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}[
textfield extension/.style ={
rounded corners,
arrows={-Straight Barb[angle=60:9pt 3]},
append after command={
node[circle, fill=red] {\TextField[name=1,width=3cm,height=1em,multiline=false,bordercolor=black, charsize=9pt]{}}
}
}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\draw[solid, black, textfield extension] (A) -- (B);
\end{tikzpicture}
\end{document}
答案1
您可以尝试将该insert path
选项与calc
库一起,但从语义上讲,使用 会更好to node
。
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, calc}
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}[
textfield extension/.style args={#1 and #2}{
rounded corners,
arrows={-Straight Barb[angle=60:9pt 3]},
insert path={
node[circle, fill=red] at ($(#1)!.5!(#2)$) {\TextField[name=1,width=3cm,height=1em,multiline=false,bordercolor=black, charsize=9pt]{}}
}
}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\draw[solid, black, textfield extension={A and B}] (A) -- (B);
\end{tikzpicture}
\end{document}
\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}[
extension line/.style={
rounded corners,
arrows={-Straight Barb[angle=60:9pt 3]},
}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\draw[solid, black, extension line]
(A) --
node[midway, circle, fill=red] {\TextField[name=1,width=3cm,height=1em,multiline=false,bordercolor=black, charsize=9pt]{}}
(B);
\end{tikzpicture}
\end{document}
答案2
我不确定我是否正确理解了你的问题。对我来说不清楚为什么你需要textfield extension
。只需在节点 A 和 B 之间箭头中间的节点中插入文本即可。
在我看来,按照问题标题所说的做最简单的方法是:
\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}[
C/.style = {circle, fill=red, minimum size=\nodewidth,
append after command = {
node[draw, semithick, fill=white, font=\small,
minimum width=\nodewidth-8pt] {#1}
}
}
]
\def\nodewidth{22mm}
\node (A) [circle, fill=red] {A};
\node (B) [circle, fill=red] at (10,0) {B};
\draw[-{Straight Barb[angle=60:9pt 3]}, semithick]
(A) -- node[C=text] {} (B);
\end{tikzpicture}
\end{document}
如果我误解了您的问题,请编辑它并更清楚地解释您的问题是什么。
答案3
我将使用普通节点命令:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning, calc, fit, shadings, shadows, shapes.arrows, arrows.meta, matrix, mindmap}
\usepackage{hyperref}
\begin{document}
\Form
\begin{tikzpicture}[
textfield extension/.style ={
rounded corners,
arrows={-Straight Barb[angle=60:9pt 3]},
}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\draw[solid, black, textfield extension] (A) -- (B);
\node[circle,fill=red,above] at ($(A)!0.5!(B)$) {\TextField[name=1,width=3cm,height=1em,multiline=false,bordercolor=black, charsize=9pt]{}};
\end{tikzpicture}
\end{document}
答案4
您无法自动沿路径放置节点。这只能通过直接在路径中--
写入来实现。node
有insert path
,但是你不能在任何地方使用它,尤其是在之后--
:
\tikz\draw (0,0) -- (2,1) [insert path={node[midway]{a}}];
如果要通过样式放置节点,则需要to
或edge
路径运算符。然后,您可以使用 沿该路径设置节点edge node
。(quotes
扩展该功能以便于输入。)
为了使以下示例更简短,我定义了:
\tikzset{
every textfield extension/.style={
rounded corners, arrows={-Straight Barb[angle=60:9pt 3]}},
my edge node/.style={
edge node={node[midway,circle,fill=red]{
\TextField[name=1,width=3cm,height=1em,multiline=false,bordercolor=black, charsize=9pt]{}
}
}
}
}
然后的定义textfield extension
取决于您想如何使用该样式。
如果您想要使用to
和\path[textfield extension]
声明,您需要将该边缘节点附加到every to
:
\begin{tikzpicture}[
textfield extension/.style={every textfield extension, every to/.append style=my edge node}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\draw[solid, black, textfield extension] (A) to (B) to[bend left=90] (A);
\end{tikzpicture}
对于edge
和\path[textfield extension]
声明,您需要将该边缘节点附加到every edge
:
\begin{tikzpicture}[
textfield extension/.style={every textfield extension,every edge/.append style=my edge node}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\path[solid, black, textfield extension] (A) edge (B)
(B) edge[bend left=90] (A);
\end{tikzpicture}
这两种解决方案都将节点沿着两个to
s/ edge
s 放置,同时to
将两个连接视为一条路径,只放置一个箭头并edge
构建两个单独的路径,从而构建两个箭头。
但是,由于这看起来像是一种只能用于节点之间连接的样式,即edge
,我将以这种方式使用它:
\begin{tikzpicture}[
textfield extension/.style={every textfield extension, my edge node}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\path[solid, black] (A) edge[textfield extension] (B)
(B) edge[bend left=90] (A);
\end{tikzpicture}
如果您仍然希望将此样式应用于一条路径上的多个边,我建议定义edges/.style={every edge/.append style={#1}}
然后:
\begin{tikzpicture}[
edges/.style={every edge/.append style={#1}},
textfield extension/.style={every textfield extension, my edge node}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\node(C) [circle, fill=red] at (0,10){C};
\path[solid, black, edges=textfield extension] (A) edge (B)
edge (C);
\end{tikzpicture}
我还添加了一个insert path
解决方案,但我不知道这是否是一个更好的用户界面。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{hyperref}
\tikzset{
every textfield extension/.style={
rounded corners, arrows={-Straight Barb[angle=60:9pt 3]}},
my edge node/.style={
edge node={node[midway,circle,fill=red]{
\TextField[name=1,width=3cm,height=1em,multiline=false,bordercolor=black, charsize=9pt]{}
}
}
}
}
\begin{document}
\begin{tikzpicture}[
textfield extension/.style={every textfield extension, every to/.append style=my edge node}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\draw[solid, black, textfield extension] (A) to (B) to[bend left=90] (A);
\end{tikzpicture}
\begin{tikzpicture}[
textfield extension/.style={every textfield extension, every edge/.append style=my edge node}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\path[solid, black, textfield extension] (A) edge (B)
(B) edge[bend left=90] (A);
\end{tikzpicture}
\begin{tikzpicture}[
textfield extension/.style={every textfield extension, my edge node}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\path[solid, black] (A) edge[textfield extension] (B)
(B) edge[bend left=90] (A);
\end{tikzpicture}
\begin{tikzpicture}[
edges/.style={every edge/.append style={#1}},
textfield extension/.style={every textfield extension, my edge node}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\node(C) [circle, fill=red] at (0,10){C};
\path[solid, black, edges=textfield extension] (A) edge (B)
edge (C);
\end{tikzpicture}
\begin{tikzpicture}[
textfield extension path/.style args={#1to#2}{
insert path={#1 edge[textfield extension] #2}},
edges/.style={every edge/.append style={#1}},
textfield extension/.style={every textfield extension, my edge node}]
\node(A) [circle, fill=red] at (0,0){A};
\node(B) [circle, fill=red] at (10,0){B};
\node(C) [circle, fill=red] at (0,10){C};
\path[solid, black] [textfield extension path=(A) to (B), textfield extension path=(C) to (B)];
\end{tikzpicture}
\end{document}