答案1
一个更简单的解决方案可能是clockwise
在使用subgraph
命令之前按顺序手动创建具有所需属性的节点。
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs, graphs.standard}
\begin{document}
\tikz
\graph [nodes={draw, circle}, n=5, radius=1.5cm, clockwise]
{ 1; 2; 3[fill=gray]; 4; 5; subgraph K_n };
\end{document}
答案2
比我预想的要简单得多。首先,我重新实现了subgraph I_n
图形宏,tikzlibrarygraphs.standard.code.tex
以便根据节点编号应用样式。
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,graphs.standard,graphdrawing}
\begin{document}
\tikzgraphsset{declare={subgraph I_n}{%
\foreach \n in \tikzgraphV{ \n[/tikz/node \n/.try] }
}}
\tikz[nodes={draw, circle}, node 3/.style={fill=gray}]
\graph { subgraph K_n[n=5, radius=1.5cm, clockwise]; };
\end{document}
其次,我们可以将这种方法的部分内容与这答案(当 n 很大时会变得有点难以处理)如下:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,graphs.standard,graphdrawing}
\begin{document}
\tikz[nodes={draw, circle}, node 3/.style={fill=gray}]
\graph [n=5, radius=1.5cm, clockwise]{
\foreach \i in {1,...,5}{ \i[/tikz/node \i/.try] }; subgraph K_n; };
\end{document}
结果和上面一样。
答案3
以下示例将节点 3 的灰色背景放在背景层中:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{graphs, graphs.standard, graphdrawing}
\begin{document}
\begin{tikzpicture}[nodes={draw, circle}]
\graph {
subgraph K_n[n=5, radius=1.5cm, clockwise];
};
\scoped[on background layer]
\node[circle, fill=gray, draw=none] at (3) {\phantom{3}};
\end{tikzpicture}
\end{document}
答案4
我定义了一个针对特定顶点的样式。它可以像这样使用:
vertex={3}{fill=yellow}
不幸的是,我无法恢复顶点数(此刻\tikz@fig@name
似乎尚未定义),所以我使用了一个新的计数器。
\documentclass[border=7mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs, graphs.standard, graphdrawing}
\newcounter{nodenum}\setcounter{nodenum}{0}
\tikzset{
step counter/.code={\stepcounter{nodenum}},
vertex/.code 2 args={
\ifnum\thenodenum=#1\pgfkeysalso{#2}\fi}
}
\begin{document}
\begin{tikzpicture}[nodes={draw, circle,
step counter,vertex={3}{fill=yellow},vertex={4}{thick,red}}]
\graph {
subgraph K_n[n=5, radius=1.5cm, clockwise];
};
\end{tikzpicture}
\end{document}
编辑 :实际上我们可以使用\tikz@lib@graph@name
自定义计数器来代替:
\documentclass[border=7mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs, graphs.standard, graphdrawing}
\makeatletter
\tikzset{vertex/.code 2 args={\ifnum\tikz@lib@graph@name=#1\pgfkeysalso{#2}\fi}}
\makeatother
\begin{document}
\begin{tikzpicture}[nodes={draw, circle, vertex={3}{fill=yellow}, vertex={4}{thick,red}}]
\graph {
subgraph K_n[n=5, radius=1.5cm, clockwise];
};
\end{tikzpicture}
\end{document}