0

TikZ allows node contents to be set using an option:

\tikz{\node[node contents=whatever];}

Note the syntax, the following is not allowed:

\tikz{\node[node contents=whatever] {};}

and gives an error:

! Use of \@next doesn't match its definition.

Now, when specifying graph nodes, a special syntax is (must be) used:

\tikz{\graph {test [color=blue]; test -> test;}}

This automatically fills the node contents with ‘test’, so when trying

\tikz{\graph {test [node contents=passed]; test -> test;}}

we expectedly encounter the error mentioned above.

My question is: (how) can I use ‘node contents’ option for graph nodes? Perhaps graphs allow classical node specification as well (I did not manage this)?

To avoid answers that (only) include workarounds or solutions that are too simple for my purposes: the end goal is to use multipart nodes in a graph in which one part has a default text and the other can be set. Think of dealing with

\tikzset{
  Thing/.style={
    circle split,
    node contents={Type\nodepart{lower}#1}
  }
}
\tikz{\node[Thing=something];}
\tikz{\graph {test [Thing=something]; test -> test;}}
3
  • Please always provide a complete example rather than a mere code fragment.
    – cfr
    Commented Apr 22, 2016 at 18:03
  • I don't think you can use node contents so Thing is out. You can use as, however, and define thing for use within graphs, if you need Thing for regular TikZ nodes.
    – cfr
    Commented Apr 23, 2016 at 1:16
  • I wonder why \tikz{\node[node contents=whatever] {};} syntax is not allowed. Why is not the default behaviour one over-ruling the other? (i mean node contents=<text> vs. {<text>}). Commented Dec 14, 2019 at 6:44

1 Answer 1

2

You cannot use Thing to create multipart nodes in graphs if you also want it to create multipart nodes outside them. However, you can create a similar style thing for use within graphs. This needs to use as rather than node contents to specify the contents of the node.

For example, if we write

  thing/.style={
    circle split,
    graphs/as={Type\nodepart{lower}#1}
  },

then we can use this as follows in a graph

\tikz{\graph {test [thing=something]; (test.lower) -> (test.text);}}

with this result

graph thing

Obviously thing can't be used outside graphs, so you need to retain Thing if you want the same style of multipart node to be available there as well.

Complete code:

\documentclass[border=10pt,tikz,multi]{standalone}
\usetikzlibrary{graphdrawing,graphs,shapes.multipart}
\begin{document}
\tikzset{
  Thing/.style={
    circle split,
    node contents={Type\nodepart{lower}#1}
  },
  thing/.style={
    circle split,
    graphs/as={Type\nodepart{lower}#1}
  },
}
\tikz{\node[Thing=something];}
\tikz{\graph {test [thing=something]; (test.lower) -> (test.text);}}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .