我尝试跟随如何用 tikz 绘制双色线并在包中合并一个具有交替颜色的节点forest
。在经典tikzpicture
和具有node
属性的情况下,它可以工作。但是,对于forest
包,我使用以下 MWE 得到了奇怪的结果:
\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}
\tikzset{%
/forest,
rbcirc/.style={circle,fill=white,draw=black,dash pattern= on 2pt off 2pt,very thick,postaction={draw,red,dash pattern= on 2pt off 2pt,dash phase=2pt,very thick},text width=1.5em}
}
\begin{document}
\begin{forest}
[A, rbcirc]
\end{forest}
\end{document}
如果我注释掉后续操作,则会显示黑色虚线图案边框。
答案1
只需将样式定义为 Ti钾Z 样式,它将正常工作。您希望将这些键传递给 Ti钾Z。
\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}
\tikzset{%
rbcirc/.style={circle,fill=white,draw=black,dash pattern= on 2pt off 2pt,very thick,postaction={draw,red,dash pattern= on 2pt off 2pt,dash phase=2pt,very thick},text width=1.5em}
}
\begin{document}
\begin{forest}
[A, rbcirc
]
\end{forest}
\end{document}
编辑
我不太明白forest
这里发生了什么。要知道,你必须问熟悉forest
内部代码的人。但我可以告诉你发生了什么。
考虑以下序列:
\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}
\forestset{%
rbcirc/.style={%
circle,
fill=white,
draw=black,
dash pattern= on 2pt off 2pt,
very thick,
postaction={%
draw,
red,
dash pattern= on 2pt off 2pt,
dash phase=2pt,
very thick,
},
text width=1.5em
},
rbcirc 2/.style={%
circle,
fill=white,
draw=black,
dash pattern= on 2pt off 2pt,
very thick,
postaction={%
draw=red,
dash pattern= on 2pt off 2pt,
dash phase=2pt,
very thick,
},
text width=1.5em
},
}
\begin{document}
\begin{forest}
[A, rbcirc
]
\end{forest}
\begin{forest}
[A, rbcirc 2
]
\end{forest}
\end{document}
在第一种情况下,就好像我们已经传递red
到了节点;在第二种情况下,就好像我们根本没有传递它。
现在考虑
\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}
\forestset{%
rbcirc 3/.style={%
circle,
fill=white,
draw=black,
dash pattern= on 2pt off 2pt,
very thick,
postaction={%
dash pattern=on 2pt off 2pt,
dash phase=2pt,
very thick,
draw,
red,
},
text width=1.5em
},
}
\begin{document}
\begin{forest}
[A, rbcirc 3
]
\end{forest}
\end{document}
失败并出现错误
! Package pgfkeys Error: The key '/tikz/dash pattern' requires a value. I am go
ing to ignore this key.
See the pgfkeys package documentation for explanation.
Type H <return> for immediate help.
...
l.729 \end{forest}
?
当forest
遇到未知密钥时,它会将其传递给 Ti钾是通过 unknown to
定义为等于node options
。如果rbcirc|rbcirc 2|rbcirc 3
定义为forest
样式,rbcirc|rbcirc 2|rbcirc 3
则已知forest
,因此forest
考虑构成样式的键值列表。似乎发生的事情是forest
将此键值列表解析为逗号分隔的列表,而不考虑内部分组。我怀疑这不太正确,或者更多的事情会失败,但这似乎是效果。所以,对于rbcirc
,Ti钾Z 获取列表
anchor=base,circle=,fill=white,draw=black,dash pattern=on 2pt off 2pt,very thick=,postaction=draw, red, dash pattern= on 2pt off 2pt, dash phase=2pt, very thick,,text width=1.5em
这本质上
anchor=base,circle=,postaction=draw, red, dash pattern= on 2pt off 2pt, dash phase=2pt, very thick,,text width=1.5em
我们得到一个带有红色虚线边框的红色实心圆圈。
对于rbcirc 2
,的列表node options
是
anchor=base,circle=,fill=white,draw=black,dash pattern=on 2pt off 2pt,very thick=,postaction=draw=red, dash pattern= on 2pt off 2pt, dash phase=2pt, very thick,,text width=1.5em
这基本上
anchor=base,circle=,fill=white,postaction=draw=red, dash pattern= on 2pt off 2pt, dash phase=2pt, very thick,,text width=1.5em
我们得到一个带有黑色虚线边框的白色节点。
但是rbcirc 3
我们得到
anchor=base,circle=,fill=white,draw=black,dash pattern=on 2pt off 2pt,very thick=,postaction=dash pattern=on 2pt off 2pt, dash phase=2pt, very thick, draw, red,,text width=1.5em
因此,第二个dash pattern
被解释为传递给的值,postaction
而不是作为获取值的键on 2pt off 2pt
。
相反,如果rbcirc|rbcirc 2|rbcirc 3
定义为 Ti钾Z 风格,它们是未知的forest
,因此直接作为node options
Ti钾node options
Z.在这种情况下,
anchor=base,rbcirc=
anchor=base,rbcirc 2=
和
anchor=base,rbcirc 3=
索蒂钾Z 按预期解析样式的定义。
编辑2
我们forest.sty
发现
\pgfqkeys{/forest}{%
.unknown/.code={%
\expandafter\pgfutil@in@\expandafter.\expandafter{\pgfkeyscurrentname}%
\ifpgfutil@in@
\expandafter\forest@relatednode@option@setter\pgfkeyscurrentname=#1\forest@END
\else
\edef\forest@marshal{%
\noexpand\pgfkeysalso{\forest@unknownto={\pgfkeyscurrentname=\unexpanded{#1}}}%
}\forest@marshal
\fi
},
}
\pgfkeyscurrentname=\unexpanded{#1}
有问题,因为它基本上将值传回键而不使用外部括号。如果我们在输入中加倍括号,我们最终会=
在末尾得到一个 ie,{<key>=<value>}=
这可以工作,但可能不是官方认可的 Ti钾Z 语法。
在序言中添加以下内容可修复此问题但我不知道是否安全和不建议使用它。
\makeatletter
\pgfqkeys{/forest}{% DO NOT TRY THIS AT HOME!
.unknown/.code={%
\expandafter\pgfutil@in@\expandafter.\expandafter{\pgfkeyscurrentname}%
\ifpgfutil@in@
\expandafter\forest@relatednode@option@setter\pgfkeyscurrentname=#1\forest@END
\else
\edef\forest@marshal{%
\noexpand\pgfkeysalso{\forest@unknownto={\pgfkeyscurrentname={\unexpanded{#1}}}}%
}\forest@marshal
\fi
},
}
\makeatother
例如,
\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}
\makeatletter
\pgfqkeys{/forest}{% DO NOT TRY THIS AT HOME!
.unknown/.code={%
\expandafter\pgfutil@in@\expandafter.\expandafter{\pgfkeyscurrentname}%
\ifpgfutil@in@
\expandafter\forest@relatednode@option@setter\pgfkeyscurrentname=#1\forest@END
\else
\edef\forest@marshal{%
\noexpand\pgfkeysalso{\forest@unknownto={\pgfkeyscurrentname={\unexpanded{#1}}}}%
}\forest@marshal
\fi
},
}
\makeatother
\forestset{%
rbcirc/.style={%
circle,
fill=white,
draw=black,
dash pattern= on 2pt off 2pt,
very thick,
postaction={%
draw,
red,
dash pattern= on 2pt off 2pt,
dash phase=2pt,
very thick,
},
text width=1.5em
},
rbcirc 2/.style={%
circle,
fill=white,
draw=black,
dash pattern= on 2pt off 2pt,
very thick,
postaction={%
draw=red,
dash pattern= on 2pt off 2pt,
dash phase=2pt,
very thick,
},
text width=1.5em
},
rbcirc 3/.style={%
circle,
fill=white,
draw=black,
dash pattern=on 2pt off 2pt,
very thick,
postaction={%
dash pattern= on 2pt off 2pt,
dash phase=2pt,
very thick,
draw,
red,
},
text width=1.5em
},
rbcirc 4/.style={%
circle,
fill=white,
draw=black,
dash pattern=on 2pt off 2pt,
very thick,
{%
postaction={%
dash pattern= on 2pt off 2pt,
dash phase=2pt,
very thick,
draw,
red,
}%
},
text width=1.5em
},
}
\begin{document}
\begin{forest}
[A, rbcirc
]
\end{forest}
\begin{forest}
[A, rbcirc 2
]
\end{forest}
\begin{forest}
[A, rbcirc 3
]
\end{forest}
\begin{forest}
[A, rbcirc 4
]
\end{forest}
\end{document}