在 TikZ 手册第 55/1318 页中,有一个创建红色标签的示例,如下所示:
下面是我重复该示例的代码:
\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:$s\le3$] {};
\node[transition] (leave critical) [right=of critical] {};
\node[transition] (enter critical) [left=of critical] {};
\end{tikzpicture}
\end{document}
但是我收到错误:!包 pgfkeys 错误:我不知道密钥“/tikz/place”,我将忽略它。也许你拼错了。有什么办法可以修复它吗?
答案1
作为命令的一部分定义的样式tikzpicture
必须用一组方括号括起来。您有两组,这就是第二个样式无法实现的原因。因此,
\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}]
你需要具备:
\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}]
tikzpicture
但是,通常情况下,您通常会使用 在命令之外定义样式\tikzset{...}
,这样您就不需要为每张图片重复样式。 (我知道这只是来自 TikZ 教程的代码,但了解如何更通用地执行此操作可能会有所帮助。)因此,您的代码的不同版本如下:
由于place
和transition
样式是您可能希望全局化的样式,因此在 之外定义它们是有意义的tikzpicture
。但是像 这样的设置every label
实际上并不是一个适合全局样式的设置,因为您可能希望标记其他内容而不将其设为红色。因此,我创建了一种样式,使每个标签都变成红色,但随后将其作为选项red labels
添加到 中。tikzpicture
\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:$s\le3$] {};
\node[transition] (leave critical) [right=of critical] {};
\node[transition] (enter critical) [left=of critical] {};
\end{tikzpicture}
\end{document}