TransWikia.com

A Question Regarding TikZ [every label/.style={red}]

TeX - LaTeX Asked on September 3, 2021

In the TikZ manual page 55/1318 there is an example to create a red label as below:
enter image description here

Below is my code to repeat the example:

documentclass{standalone}
usepackage{tikz}
usetikzlibrary {positioning}
begin{document}

begin{tikzpicture}[every label/.style={red}]
[place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=10 pt},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=10 pt}]


node[place] (waiting) {};
node[place] (critical) [below=of waiting] {};
 node[place] (semaphore) [below=of critical,
label=above:$sle3$] {};
node[transition] (leave critical) [right=of critical] {};
node[transition] (enter critical) [left=of critical] {};



end{tikzpicture}

end{document}

But I got an error:
! Package pgfkeys Error: I do not know the key ‘/tikz/place’ and I am going to
ignore it. Perhaps you misspelled it.
Any idea how to fix it?

One Answer

The styles defined as part of the tikzpicture command must be enclosed in a single set of square brackets. You have two sets, which is why the second style doesn't get implemented. So instead of

begin{tikzpicture}[every label/.style={red}]
[place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=10 pt},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=10 pt}]

you need to have:

begin{tikzpicture}[every label/.style={red}, % ] removed and comma added
place/.style={circle,draw=blue!50,fill=blue!20,thick, % [ removed
inner sep=0pt,minimum size=10 pt},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=10 pt}]

Typically, however you would usually define styles outside of the tikzpicture command using tikzset{...}, so that you don't need to repeat the style for every picture. (I understand that this is just the code from the TikZ tutorial, but it might be helpful to know how to do it more generally.) So a different version of your code would be the following:

Since the place and transition styles are the kind of thing you might want to be global, it makes sense to define them outside of the tikzpicture. But a setting like every label is not really one which is good as a global style, since you may want to label other things without making them red. Because of this, I've created a red labels style which makes every label red, but then is added to the tikzpicture as an option.

documentclass{standalone}
usepackage{tikz}
usetikzlibrary {positioning}
begin{document}
tikzset{red labels/.style={every label/.style={red}},
place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=10 pt},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=10 pt}}

begin{tikzpicture}[red labels]

node[place] (waiting) {};
node[place] (critical) [below=of waiting] {};
 node[place] (semaphore) [below=of critical,
label=above:$sle3$] {};
node[transition] (leave critical) [right=of critical] {};
node[transition] (enter critical) [left=of critical] {};



end{tikzpicture}

end{document}

Correct answer by Alan Munn on September 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP