4

I want to draw different kinds of graphs which I want to define using styles. One particular graph has labelled nodes, and I want to be able to attach these labels using a custom key (cn).

The following document demonstrates this, the problem is, that the node labels specified using cn do not show up.

\documentclass{standalone} \usepackage{tikz} \tikzstyle{RegionGraph}=[ every node/.style={fill=blue!20}, every edge/.style={draw,latex-}, cn/.style={label={#1}} ] \begin{document} \begin{tikzpicture}[RegionGraph] \draw (0,0) node[cn=1] (lr1) {$X_1$} ++(2,0) node[cn=0] (lr2) {$X_2$} edge (lr1); \end{tikzpicture} \end{document} 

Where as, when I add the definition of the cn-style directly to the picture, like in the following code, the labels do show.

\begin{tikzpicture}[RegionGraph,cn/.style={label={#1}}] ... 

What am I doing wrong and how can it be fixed?

2 Answers 2

6

When you nest the style definitions you need to double the number of # characters as when they get expanded they halve the numbers. So if you nest it in three levels it needs ####1 with four levels eight times etc. That's why labels don't appear because #1 holds the argument for RegionGraph but you meant cn's own argument at the time when it is defined.

\documentclass[tikz]{standalone} \tikzset{RegionGraph/.style={ every node/.style={fill=blue!20}, every edge/.style={draw,latex-}, cn/.style={label={##1}} }} \begin{document} \begin{tikzpicture}[RegionGraph] \draw (0,0) node[cn=1] (lr1) {$X_1$} ++(2,0) node[cn=0] (lr2) {$X_2$} edge (lr1); \end{tikzpicture} \end{document} 
2
  • This works, and also explains the problem. Thank you. Commented Jan 29, 2015 at 13:47
  • @ziggystar My pleasure Commented Jan 29, 2015 at 13:48
1

Use tikzset instead of tikzstyle:

\documentclass{standalone} \usepackage{tikz} \tikzset{ every node/.style={fill=blue!20}, every edge/.style={draw,latex-}, cn/.style={label={#1}}} \begin{document} \begin{tikzpicture} \draw (0,0) node[cn=1] (lr1) {$X_1$} ++(2,0) node[cn=0] (lr2) {$X_2$} edge (lr1); \end{tikzpicture} \end{document} 

enter image description here

3
  • But now every picture has these styles applied. I want the style to be only valid in pictures with style RegionGraph. Commented Jan 29, 2015 at 11:05
  • Use tikzset definition in only desired tikzpicture. Commented Jan 29, 2015 at 11:12
  • But then I have to define it each time anew. Basically I want that adding RegionGraph to a picture enables the use of cn on the nodes of the picture. But maybe I have to settle with enabling cn globally for all nodes. Commented Jan 29, 2015 at 11:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.