这是一个问题对上一个问题的回答:
所提出的解决方案采用了画中画技术。正如作者所指出的:
注意:
(foo)
和(age)
不会在最终的 tikzpicture 中定义。
对我来说,包含的图片在某种意义上是原子的,这是有道理的,但我想看看如果我尝试从外部图片引用内部图片的节点,它会如何中断:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,intersections,shapes,backgrounds,scopes,positioning,fit,matrix}
%% Language and font encodings
\usepackage[english]{babel}% Really?
\newsavebox{\tempbox}
\begin{document}
\tikzset{varname/.style={rectangle,thin,inner sep=0.3ex,font=\ttfamily,text height=1.5ex,text depth=0.35ex}}
\tikzset{vartype/.style={rectangle,text=red,thin,inner sep=0.3ex,font=\ttfamily,text height=1.5ex,text depth=0.35ex}}
\tikzset{struct/.style={draw,matrix of nodes,column 1/.style={anchor=base west},column 2/.style={anchor=base west}}}
\tikzset{structtype/.style={rectangle,draw,fill=white,thin,inner sep=0.3ex,font=\ttfamily,text height=1.5ex,text depth=0.35ex}}
\setbox\tempbox=\hbox{\begin{tikzpicture}[baseline=(age.text)]
\matrix[struct] (attributes)
{
\node[varname] (age) {age}; & \node[vartype] {int*}; & \\
\node[varname] {height}; & \node[vartype] {float}; & \\
\node[varname] {weight}; & \node[vartype] (zzz) {float}; & \\
};
\node[structtype,above=0.5ex of attributes.north west,anchor=west] {Attributes};
\end{tikzpicture}}%
\begin{tikzpicture}
\matrix[struct] (person)
{
\node[varname] {name}; & \node[vartype] {const char*}; \\
\node[varname] {attrs}; & \node{\usebox{\tempbox}}; \\
\node[varname] {xxx}; & \node (xxx) {TODO}; \\
};
\node[structtype,above=0.5ex of person.north west,anchor=west] {Person};
\draw[->,very thick] (xxx.east) -- ++(2,0) |- (zzz.east);
\end{tikzpicture}
\end{document})
问题是(zzz)
节点能被引用,但外部图像将其置于错误的位置。可以通过插入额外内容来突出显示问题。
如果代码能正常工作,我本来期望得到这样的结果
我本来并不指望(zzz)
节点能够编译,但事实上它确实能够编译,箭为什么会落在那里?
(请注意,在内部图片上方添加 X 像素的东西不会将箭头的端点向下移动 X 像素:端点与图形中任何其他东西的距离似乎都没有固定。)
答案1
将选项添加remember picture
到两个tikzpicture
环境中。
\setbox\tempbox=\hbox{\begin{tikzpicture}[baseline=(age.text),remember picture]
...
\begin{tikzpicture}[remember picture]
您必须运行两次 LaTeX 才能获得写入aux
文件的正确参考点。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,intersections,shapes,backgrounds,scopes,positioning,fit,matrix}
%% Language and font encodings
\usepackage[english]{babel}% Really?
\newsavebox{\tempbox}
\begin{document}
\tikzset{varname/.style={rectangle,thin,inner sep=0.3ex,font=\ttfamily,text height=1.5ex,text depth=0.35ex}}
\tikzset{vartype/.style={rectangle,text=red,thin,inner sep=0.3ex,font=\ttfamily,text height=1.5ex,text depth=0.35ex}}
\tikzset{struct/.style={draw,matrix of nodes,column 1/.style={anchor=base west},column 2/.style={anchor=base west}}}
\tikzset{structtype/.style={rectangle,draw,fill=white,thin,inner sep=0.3ex,font=\ttfamily,text height=1.5ex,text depth=0.35ex}}
\setbox\tempbox=\hbox{\begin{tikzpicture}[baseline=(age.text),remember picture]
\matrix[struct] (attributes)
{
\node[varname] (age) {age}; & \node[vartype] {int*}; & \\
\node[varname] {height}; & \node[vartype] {float}; & \\
\node[varname] {weight}; & \node[vartype] (zzz) {float}; & \\
};
\node[structtype,above=0.5ex of attributes.north west,anchor=west] {Attributes};
\end{tikzpicture}}%
\begin{tikzpicture}[remember picture]
\matrix[struct] (person)
{
\node[varname] {name}; & \node[vartype] {const char*}; \\
\node[varname] {name}; & \node[vartype] {const char*}; \\
\node[varname] {name}; & \node[vartype] {const char*}; \\
\node[varname] {attrs}; & \node{\usebox{\tempbox}}; \\
\node[varname] {xxx}; & \node (xxx) {TODO}; \\
};
\node[structtype,above=0.5ex of person.north west,anchor=west] {Person};
\draw[->,very thick] (xxx.east) -- ++(2,0) |- (zzz.east);
\end{tikzpicture}
\end{document}