我如何定义 Ti 内部节点的别名钾Z 图片?
在此 MWE 上,黑线是我想要获得的线(使用别名而不是相对定位),而红线是我能得到的最好的线:
\documentclass[tikz]{standalone}
\usepackage{amsmath}
\usepackage{tikz-cd}
\tikzset{
groundpic/.pic={
% the () makes sure the node "replaces" the parent node (this way, \rar points to this node)
\node[draw,alias=internalB](){Some text};
% \tikzset{add anchor to pic'/.list={t1, b1, t2, b2, t3, b3}}
},
ground/.style={
shape=coordinate, yshift=axis_height,
append after command={
pic[scale=2,at=(\tikzlastnode),name=\tikzlastnode,alias=semiInternalB]{groundpic}
}
}
}
\begin{document}
\begin{tikzcd}
|[alias=myA]| A \rar & |[ground, alias=myB]|
% Works, but is pointing to the center of the shape, while I would like it to point to the inner nodes itself.
\ar[from=myA, to=myB, red]
% % Fails:
%\ar[from=myA, to=internalB, green]
% % Fails:
%\ar[from=myA, to=semiInternalB, green]
\end{tikzcd}
\end{document}
此问题是以下问题的后续问题:Tikz pic:使用内部节点作为锚点名称
编辑: 似乎:
append after command={\pgfextra{\pgfnodealias{bidule}{\tikzlastnode}}}
比alias=…
,不知道为什么...知道别名出了什么问题吗?
编辑举一个完整的例子:
\documentclass[tikz]{standalone}
\usepackage{amsmath}
\usepackage{tikz-cd}
\tikzset{
groundpic/.pic={
% the () makes sure the node "replaces" the parent node (this way, \rar points to this node)
\node[draw,alias=internalB](){Some text};
% \tikzset{add anchor to pic'/.list={t1, b1, t2, b2, t3, b3}}
},
ground/.style={
shape=coordinate, yshift=axis_height,
append after command={
pic[scale=2,at=(\tikzlastnode),name=\tikzlastnode,append after command={\pgfextra{\pgfnodealias{bidule}{\tikzlastnode}}}]{groundpic}
}
}
}
\begin{document}
\begin{tikzcd}
|[alias=myA]| A & |[ground]|
% Works, but is pointing to the center of the shape, while I would like it to point to the inner nodes itself.
\ar[from=myA, to=bidule, red]
% % Fails:
%\ar[from=myA, to=internalB, green]
% % Fails:
%\ar[from=myA, to=semiInternalB, green]
\end{tikzcd}
\end{document}
答案1
这种情况令人难以接受。让我们分析一下:
该宏
\tikzlastnode
仅保存最后一个节点(坐标只是另一个节点)的名称(而不是任何别名)或 pic(如果已明确命名)。\tikz \coordinate[ name=COORDINATE, append after command={ \pgfextra{\typeout{\tikzlastnode}} pic[ name=PIC, append after command=\pgfextra{\typeout{\tikzlastnode}}]{code=} }, append after command=\pgfextra{\typeout{\tikzlastnode}}];
将会在日志文件中放入
COORDINATE
一次和两次。PIC
因此, ed 坐标不会将其别名“传递”给与(标签、固定、命令后的附加/前缀)
alias
有关的任何事物。\tikzlastnode
别名
pic
不起作用。
图片不是对象,但其内容可以继承其名称。带有 的图片内的节点
alias
将具有与正常名称相同的图片名称前缀。为节点设置别名将在 TeX 级别创建该节点的副本。这些副本现在是独立的。
\node[alias=A-copy](A){}; \node (A){};
将留下两个节点:(
A-copy
第一个)和A
(第二个)。在 TeX 级别,一个节点包含五个宏,用于
- 形状,
- 保存的锚点,
- 节点的局部坐标系(位置、旋转、缩放、剪切),
- 图片 ID(用于在图片之间绘制)和
- 已保存的宏包含其余内容:临时锚点、我在链接答案中添加的额外锚点。
我的链接答案中的键
add anchor to node
将仅添加到其中一个副本:通过其名称指定的副本。需要在添加额外锚点后对此类节点进行别名处理。无论是直接在图片内还是在图片后,都无关紧要。
我在代码中添加了一个未经测试的
\pgfnodelink
宏以及所需的 TikZ 键。我还没有测试过。(仅仅命名它似乎很危险,因为它可能是用于边缘的流行键名。)alias link
link
这意味着:
第一个箭头指向
myB
的是 坐标。这是 的别名myB
,没有其他内容。你的第一支失败的箭需要
\ar[from=myA, to=tikz@f@1-1-2internalB, green]
其中
tikz@f@1
是赋予矩阵的内部名称,1 是行,2 是列,因为它使用与名称本身相同的前缀/后缀设置,因此使用图片的名称作为前缀。alias = semiInternalB
这里不执行任何操作。
代码
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}
\tikzset{
groundpic/.pic={
% the () makes sure the node "replaces" the parent node (this way, \rar points to this node)
\node[draw,alias=internalB](){Some text};
% \tikzset{add anchor to pic'/.list={t1, b1, t2, b2, t3, b3}}
},
ground/.style={
shape=coordinate, yshift=axis_height,
append after command={
pic[scale=2,at=(\tikzlastnode),name=\tikzlastnode,alias=semiInternalB]{groundpic}
}
},
}
\makeatletter
% A primitive test whether a node name exists
% (somewhere not necessarily inside the current TikZ picture)
\newcommand*\tikznodeexistTF[3]{\pgfutil@ifundefined{pgf@sh@ns@#1}{#3}{#2}}
% add link/pointer
\def\pgfutil@namedefname#1#2{% untested
\expandafter\gdef\csname#1\expandafter\endcsname\expandafter{\csname#2\endcsname}}
\def\pgfnodelink#1#2{% untested
\pgfutil@namedefname{pgf@sh@ns@#1}{pgf@sh@ns@#2}%
\pgfutil@namedefname{pgf@sh@np@#1}{pgf@sh@np@#2}%
\pgfutil@namedefname{pgf@sh@nt@#1}{pgf@sh@nt@#2}%
\pgfutil@namedefname{pgf@sh@pi@#1}{pgf@sh@pi@#2}%
\pgfutil@namedefname{pgf@sh@ma@#1}{pgf@sh@ma@#2}}
\tikzset{alias link/.code={%
\tikz@fig@mustbenamed
\begingroup
\toks0=\expandafter{\tikz@alias}%
\edef\pgf@temp{\noexpand\pgfnodelink{\tikz@pp@name{#1}}{\noexpand\tikz@fig@name}}%
\toks1=\expandafter{\pgf@temp}%
\xdef\pgf@marshal{\noexpand\def\noexpand\tikz@alias{\the\toks0 \the\toks1 }}%
\endgroup
\pgf@marshal}}%
\makeatother
% Log entries for creating/aliasing/linking
\usepackage{etoolbox}
\pretocmd{\pgfmultipartnode}{\typeout{Creating node #3 in \pgfpictureid}}{}{\PatchFailed}
\pretocmd{\pgfnodealias} {\typeout{Aliasing #2 -> #1}} {}{\PatchFailed}
\pretocmd{\pgfnodelink} {\typeout{Linking #2 -> #1}} {}{\PatchFailed}
\begin{document}
\begin{tikzcd}
|[alias=myA]| A \rar & |[ground, alias=myB]|
% Works, but is pointing to the center of the shape, while I would like it to point to the inner nodes itself.
\ar[from=myA, to=myB, red]
% % Fails:
\ar[from=myA, to=tikz@f@1-1-2internalB, green]
% % Fails:
% \ar[from=myA, to=semiInternal, green]
\end{tikzcd}
\begin{tikzpicture}
\coordinate[
alias=C',
append after command={
pic[alias=P'](P){
code={
\node[draw, alias=N'](){};
}
}
}] (C);
\end{tikzpicture}
\foreach \nodename in {C, C', P, P', PN', P'N'}{
\par % y y y n y n
The node name \texttt{\nodename}
\tikznodeexistTF{\nodename}{exists}{doesn't exist}.
}
\tikz
\coordinate[
name=COORDINATE,
append after command={
\pgfextra{\typeout{\tikzlastnode}}
pic[
name=PIC,
append after command=\pgfextra{\typeout{\tikzlastnode}}]{code=}},
append after command=\pgfextra{\typeout{\tikzlastnode}}];
\end{document}