TikZ:为什么将 anchor= 移动到样式会改变结果?

TikZ:为什么将 anchor= 移动到样式会改变结果?

这是按预期呈现的代码。我想重构它。

\documentclass[tikz,border=10pt,a4paper]{standalone}

\usetikzlibrary{arrows,intersections,shapes,backgrounds,scopes,positioning,fit,matrix}

\usepackage[english]{babel}

\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,text=red,fill=white,thin,inner sep=0.3ex,font=\ttfamily,text height=1.5ex,text depth=0.35ex}}

\begin{tikzpicture}
\matrix (foo) [struct]
{
    \node[varname] (age) {age}; & \node[vartype] {int*};  & \\
    \node[varname] {height};    & \node[vartype] {float}; & \\
    \node[varname] {weight};    & \node[vartype] {float}; & \\
};
\node[structtype,above=2ex of age.west,anchor=west]  {Foo};
\end{tikzpicture}

\end{document}

在此处输入图片描述

每个structtype节点都将以相同的方式锚定到相应的矩阵,所以我想将2exanchor=west部分移动到一种样式中,这样每次我只需要写类似这样的内容:

\node[structtype,above=of FOO.west]  {...};

代替

\node[structtype,above=2ex of FOO.west,anchor=west]  {...};

我尝试转换anchor=west风格,但结果却有所不同:

\documentclass[tikz,border=10pt,a4paper]{standalone}

\usetikzlibrary{arrows,intersections,shapes,backgrounds,scopes,positioning,fit,matrix}

\usepackage[english]{babel}

\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,text=red,fill=white,thin,inner sep=0.3ex,font=\ttfamily,text height=1.5ex,text depth=0.35ex,anchor=west}}

\begin{tikzpicture}
\matrix (foo) [struct]
{
    \node[varname] (age) {age}; & \node[vartype] {int*};  & \\
    \node[varname] {height};    & \node[vartype] {float}; & \\
    \node[varname] {weight};    & \node[vartype] {float}; & \\
};
\node[structtype,above=2ex of age.west]  {Foo};
\end{tikzpicture}

\end{document}

在此处输入图片描述

我如何将“把它放在其他东西之上,固定在西边”重构为一种风格?

答案1

我想,您希望获得以下结果:

在此处输入图片描述

我(大量)重写了你的代码,希望按照期望的方式并且代码是不言自明的:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{arrows, 
                backgrounds, 
                fit,
                intersections,
                matrix,
                positioning,
                scopes, 
                shapes}
\usepackage[english]{babel}

\tikzset{Matrix/.style ={% named as Matrix, moved in preamble, 
varname/.style = {% now serve also as common nodes style
                  rectangle, inner sep=0.3ex,
                  text height=1.5ex, text depth=0.35ex,
                  font=\ttfamily},
vartype/.style = {varname, text=red},         % simplified
 struct/.style = {matrix of nodes,
                  draw, outer sep=0pt,
                  nodes={anchor=base west},   % added
                  column 1/.style={varname},  % changed
                  column 2/.style={vartype}}, % changed
structtype/.style={vartype, draw, fill=white, 
                   above=-0.35ex}             % changed
                            }
        }% end of tikzset

\begin{document}
    \begin{tikzpicture}[Matrix]% Matrix indicate name of used tikzset 
\matrix (M) [struct]
{
    age     &   int*    \\      % simplified
    height  &   float   \\      % simplified
    weight  &   float   \\      % simplified
};
\node[structtype]  at (M-1-1.north) {Foo};% changed as I understood you wish to have
    \end{tikzpicture}
\end{document}

笔记:当您使用above rightanchor选项并且您喜欢anchor考虑时,它应该是选项之间的最后一个。相反的情况下above right将覆盖anchor选项。

编辑:另一种简单的解决方案是:

  • 定义structtypestructtype/.style={vartype, draw, fill=white}
  • 定位内容为“Foo”的节点为\node[structtype] at (M-1-1 |- M.north) {Foo};

结果将与以前相同。

相关内容