2

I have put \node commands inside a TikZ matrix of nodes. I am aware that this breaks automatic m-1-1 node naming, but when I give the name of a node explicitly, the anchors are in the wrong place.

Full code:

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

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

%% Language and font encodings
\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}}}

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

\end{document}

(I am aware that I don't need the \node commands in this example, but the above code is placeholder -- the full code requires nontrivial drawing within some matrix cells.)

The Foo node appears in the middle of the matrix instead of above it:

enter image description here

The (age) node is being referenced in \node[vartype] {Foo} [above=2ex of age.west,anchor=west]; and all my experiments to refer to somethingelse.west have failed to compile.

Why is the Foo node appearing in the middle of the matrix?

1

I changed the order of label and braces a little bit. By the way i prefer the node name from 57.1 Matrices of Nodes Tikz Manual (auto).

Modifications:

\matrix (foo) [struct]  

instead of

\matrix[struct] (foo) 

and

\node[vartype,above=2ex of age.west,anchor=west] {Foo};

replaces

\node[vartype] {Foo} [above=2ex of age.west,anchor=west];

.

Example image: enter image description here

MWE:

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

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

%% Language and font encodings
\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}}}

\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[vartype,above=2ex of age.west,anchor=west] {Foo};

\end{tikzpicture}

\end{document}
3

First question. You can use explicit names and automatic names together in a matrix of nodes. The syntax is explained in page 646. |(age)| will apply name age to the node replacing the automatic name. All other nodes will keep their automatic names.

In fact the pair |...| is used to particularly change node attributes. Inside bars you use [...] for options and (...) for node name. As an example, |[blue](age)| would define a node to be drawn with blue color and named age.

Second question: loose of anchors. Anchors are correct, but your syntax not.

You wrote

\node[vartype] {Foo} [above=2ex of age.west,anchor=west];

and everything behind node contents is ignored. (Try the same line with ;% behind {Foo})

This was the mistake.

You should write all options, together or not, with node's name o not, but all of them before node's text:

\node[vartype] (fooo) [above=2ex of age.west,anchor=west] {Foo};

\node[vartype, above=2ex of age.west,anchor=west] {Foo};

Both work.

Following code shows how to type your example with a matrix of nodes (without \node...). One node is labelled age and all others follow foo-xx-xx. And as an example of names mixture a line is drawn between (age) and (foo-3-2).

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

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

%% Language and font encodings
\usepackage[english]{babel}

\begin{document}

\tikzset{%
    varname/.style={rectangle, thin, inner sep=0.3ex,
        font=\ttfamily, text height=1.5ex, text depth=0.35ex},
    vartype/.style={rectangle, text=red, thin, 
        inner sep=0.3ex, font=\ttfamily, text height=1.5ex, 
        text depth=0.35ex},
    struct/.style={draw, matrix of nodes, 
        column 1/.style={varname, anchor=base west},
        column 2/.style={vartype, anchor=base west}}
    }

\begin{tikzpicture}
\matrix[struct] (foo) 
{
    |(age)| age & int* \\
    height & float \\
    weight & float \\
};

\node[vartype, above=2ex of age.west, anchor=west] {Foo};
\node[vartype] (fooo) [draw=green, above=2ex of foo.north, anchor=west] {Foo};

\draw (age) -- (foo-3-2);
\end{tikzpicture}

\end{document}

enter image description here

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.