3

Here is code which renders as expected. I want to refactor it.

\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}

enter image description here

Every structtype node will be anchored in the same way to the corresponding matrix, so I want to move the 2ex and anchor=west parts into a style so I would only have to write something like this each time:

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

instead of

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

My attempt to move anchor=west to the style gives a different rendering:

\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}

enter image description here

How do I refactor "put it this much above whatever other thing, anchored west" into a style?

3
  • 2
    because above right override anchor option. When you use both, always option anchor=... should be the last between options. – Zarko Mar 8 '17 at 14:00
  • An alternative: \node[structtype, right= 0pt of foo.north-|age.west] {Foo}; – Ignasi Mar 8 '17 at 16:16
  • @TorbjørnT., done! Thank you to encourage me. However, now I'm not sure, if I correctly understand OP problem. Anyway, I significantly rewrote his MWE. See below: – Zarko Jun 20 '17 at 11:00
2

I suppose, that you like to obtain the following result:

enter image description here

I (significantly) rewrote your code, hopefully on desired way and that the code is self explanatory:

\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}

Note: When you use above right and anchor option and you like thatanchor is considered, it should be the last between options. In contrary case above right will overwrite anchor option.

Edit: An alternative simple solution is:

  • define structtype as structtype/.style={vartype, draw, fill=white}
  • positioning node with content "Foo" as \node[structtype] at (M-1-1 |- M.north) {Foo};

Result will be the same as before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.