我找不到很多关于在 pgfkes 中正确使用参数模式的例子,但我能够想到以下内容:
\documentclass{standalone}
\usepackage{tikz}
\tikzset{dimen/.style={<->,>=latex,thin,
every rectangle node/.style={fill=white,midway}
}}
\tikzset{measuring south/.style args={from #1 to #2 is #3}{
append after command={
draw (#1.south west) -- ++(0,-1.0)
coordinate (A1) -- ++(0,-10pt)
(#2.south east) -- ++(0,-1.0) coordinate (A2) -- ++(0,-10pt)
[dimen] (A1) -- (A2) node {#3}
}
}
}
\begin{document}
\begin{tikzpicture}
\node(a) [draw,rectangle,text=teal] at (0,0) {here};
\node(b) [draw,rectangle,text=olive] at (3,0) {there};
\path[measuring south=from here to there is far,fill=red];
\node(x) [draw,rectangle,text=blue] at (0,-2) {Heaven};
\node(y) [draw,rectangle,text=red] at (3,-2) {Hell};
\draw (x.south west) -- ++(0,-1.0)
coordinate (A1) -- ++(0,-10pt)
(y.south east) -- ++(0,-1.0) coordinate (A2) -- ++(0,-10pt)
[dimen] (A1) -- (A2) node {sin};
\end{tikzpicture}
\end{document}
在我的输出中,我希望看到从“这里”到“那里”的尺寸线,但这些线是不可见的。
答案1
来自 TikZ/PGF 手册(重点添加):
一些以下部分中描述的路径命令带有可选参数。对于这些命令,当您在这些选项中使用此键时,路径将在路径命令完成后插入。
这里的关键是“一些”。append after command
需要在后面附加一个命令。可以使用的有、、node
和。您的没有命令,因此不可能执行。这里您需要的是在该点停留在路径中的键。一旦您更改了它,您就会得到一些输出,但您还会发现其他一些(小)错误:您没有命名节点和,命令有点奇怪,并且需要用括号括起来才能解释为样式。edge
to
\path
append after command
insert path
here
there
fill=red
draw
\documentclass{article}
%\url{http://tex.stackexchange.com/q/154155/86}
\usepackage{tikz}
\tikzset{dimen/.style={<->,>=latex,thin,
every rectangle node/.style={fill=white,midway}
}}
\tikzset{measuring south/.style args={from #1 to #2 is #3}{
insert path={
[draw] (#1.south west) -- ++(0,-1.0)
coordinate (A1) -- ++(0,-10pt)
(#2.south east) -- ++(0,-1.0) coordinate (A2) -- ++(0,-10pt)
[dimen] (A1) -- (A2) node {#3}
}
}
}
\begin{document}
\begin{tikzpicture}
\node(a) [draw,rectangle,text=teal] at (0,0) (here) {here};
\node(b) [draw,rectangle,text=olive] at (3,0) (there) {there};
\path[measuring south=from here to there is far];
\node(x) [draw,rectangle,text=blue] at (0,-2) {Heaven};
\node(y) [draw,rectangle,text=red] at (3,-2) {Hell};
\draw (x.south west) -- ++(0,-1.0)
coordinate (A1) -- ++(0,-10pt)
(y.south east) -- ++(0,-1.0) coordinate (A2) -- ++(0,-10pt)
[dimen] (A1) -- (A2) node {sin};
\end{tikzpicture}
\end{document}
答案2
所呈现的 MWE 示例包含一些概念错误。
我认为最相关的是您希望通过文本而不是名称来引用节点。这就是我读到的内容:
\node(a) [draw,rectangle,text=teal] at (0,0) {here};
\node(b) [draw,rectangle,text=olive] at (3,0) {there};
\path[measuring south=from here to there is far,fill=red];
语法\path
应该包含a
and b
,而不是here
and there
。
其次,此处后期选项的使用方式错误:路径一旦结束,就结束了,您无法再添加内容。因此,measuring south
您愿意使用insert path
而不是append after command
。此外,由于您要将路径选项从主路径更改为插入的路径,因此您可能需要使用\pgfextra
。
我应该这样做:
\documentclass[border=3pt]{standalone}
\usepackage{tikz}
\tikzset{dimen/.style={<->,>=latex,thin,
every rectangle node/.style={fill=white,midway}
}}
\tikzset{measuring south/.style args={from #1 to #2 is #3}{
insert path={
\pgfextra{
\draw[red] (#1.south west) -- ++(0,-1.0)
coordinate (A1) -- ++(0,-10pt);
\draw[blue](#2.south east) -- ++(0,-1.0)
coordinate (A2) -- ++(0,-10pt);
\draw[dimen] (A1) -- (A2) node[midway,fill=white] {#3};
}
}
}
}
\begin{document}
\begin{tikzpicture}
\node(a) [draw,rectangle,text=teal] at (0,0) {here};
\node(b) [draw,rectangle,text=olive] at (3,0) {there};
\path[measuring south=from a to b is far];
\node(x) [draw,rectangle,text=blue] at (0,-2) {Heaven};
\node(y) [draw,rectangle,text=red] at (3,-2) {Hell};
\draw (x.south west) -- ++(0,-1.0)
coordinate (A1) -- ++(0,-10pt)
(y.south east) -- ++(0,-1.0) coordinate (A2) -- ++(0,-10pt)
[dimen] (A1) -- (A2) node {sin};
\end{tikzpicture}
\end{document}
结果: