![TiKZ 中的单个节点颜色](https://linux22.com/image/322075/TiKZ%20%E4%B8%AD%E7%9A%84%E5%8D%95%E4%B8%AA%E8%8A%82%E7%82%B9%E9%A2%9C%E8%89%B2.png)
我有以下代码:
\begin{tikzpicture}
\graph[circular placement,
empty nodes, nodes={circle,draw, minimum size=0.01cm}] {
\foreach \x in {a,...,c} {
\foreach \y in {\x,...,c} {
\x -- \y;
};
};
};
\draw (a) edge[red, thick] (b);
\draw (b) edge[red, thick] (c);
\draw (c) edge[blue, thick] (a);
\end{tikzpicture}
生成一个边缘为红色/蓝色的完整图形。我想将节点 (a) 涂成蓝色,将 (b)、(c) 涂成红色。或者其他配置。我可以在区域fill=red!30
中使用nodes={}
,但这会导致所有节点都变成红色。我如何指定单个节点?
一般来说,有没有办法给特定节点填充颜色?
答案1
像这样吗?
\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}
\graph[counterclockwise=6,
empty nodes, nodes={circle,draw, minimum size=0.01cm}] {
a[fill=blue], b[fill=red], c[fill=red]
};
\draw (a) edge[red, thick] (b);
\draw (b) edge[red, thick] (c);
\draw (c) edge[blue, thick] (a);
\end{tikzpicture}
\end{document}
答案2
您可以使用条件fill
来方便使用。对于第一个节点,您只应添加\x[fill={\ifnum\i=1 blue\else white\fi}]
,但对于所有其他节点,您可以添加\y[fill={\ifnum\j=2 red\else\ifnum\j=3 red\else white\fi\fi}]
。当然,您可以单独选择任意数量的所需节点。
\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}
\graph[circular placement,
empty nodes, nodes={circle,draw, minimum size=0.01cm}] {
\foreach \x[count=\i] in {a,...,c} {
\foreach \y[count=\j] in {\x,...,c} {
\x[fill={\ifnum\i=1 blue\else white\fi}] -- \y[fill={\ifnum\j=2 red\else\ifnum\j=3 red\else white\fi\fi}];
};
};
};
\draw (a) edge[red, thick] (b);
\draw (b) edge[red, thick] (c);
\draw (c) edge[blue, thick] (a);
\end{tikzpicture}
\end{document}