![TikZ:重新定义/更新节点外观](https://linux22.com/image/434284/TikZ%EF%BC%9A%E9%87%8D%E6%96%B0%E5%AE%9A%E4%B9%89%2F%E6%9B%B4%E6%96%B0%E8%8A%82%E7%82%B9%E5%A4%96%E8%A7%82.png)
以下代码绘制了一个带有标记节点的超立方体。到目前为止,这工作正常。现在,我想用不同的颜色为某些特定节点着色。PGF 手册在第 16.14 章(第 207 页)中说,使用\node also
[...] 大多数选项将不起作用,因为您无法更改节点的外观,也就是说,您无法使用这些“后期”选项将红色节点更改为绿色节点。但是,
append after command
在prefix after command
晚期选择(直接或间接)确实产生了预期的效果:给定的路径将通过\tikzlastnode
设置到确定的节点来执行。
我所有将节点涂成蓝色的方法都失败了。有人能帮我吗?
顺便说一句,我有一个可行的解决方案,我可以为每个节点单独着色,因为我手动编码了立方体,没有使用 for 循环。这个问题是关于一个解决方案,使用 for 循环来绘制立方体。
梅威瑟:
\documentclass[tikz, border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[
line width=0.6pt,
every node/.style={circle, draw, fill, minimum size=6pt, inner sep=0pt, font=\scriptsize\bfseries}]
\pgfsetxvec{\pgfpoint{0.9cm}{0.0cm}}
\pgfsetyvec{\pgfpoint{0.0cm}{0.9cm}}
\foreach \point / \id / \angle in {
% outer cube
(0,0)/0001/270,
(0,5)/0011/90,
(5,0)/1001/270,
(5,5)/1011/90,
(2,2)/0101/180,
(2,7)/0111/90,
(7,2)/1101/270,
(7,7)/1111/90,
% inner cube
(2.5,1.5)/0000/270,
(2.5,3.5)/0010/90,
(4.5,1.5)/1000/250,
(4.5,3.5)/1010/120,
(3.5,2.5)/0100/170,
(3.5,4.5)/0110/180,
(5.5,2.5)/1100/10,
(5.5,4.5)/1110/0}
{
\node (\id) at \point [label=\angle:\id] {};
}
% Here I'd like to color specific nodes blue.
% Unfortunately this isn't working...
%
% \foreach \id in {0011, 1011}
% {
% \node also [append after command={blue}] (\id);
% }
\path
(0011) edge (1011) edge (0111) edge (0001)
(1001) edge (0001) edge (1101) edge (1011)
(1111) edge (1101) edge (1011) edge (0111)
(0010) edge (1010) edge (0110) edge (0000)
(1000) edge (0000) edge (1100) edge (1010)
(1110) edge (1100) edge (1010) edge (0110);
\path[dashed]
(0101) edge (1101) edge (0001) edge (0111)
(0100) edge (1100) edge (0000) edge (0110);
\path[dotted]
(0000) edge (0001)
(0010) edge (0011)
(0100) edge (0101)
(0110) edge (0111)
(1000) edge (1001)
(1010) edge (1011)
(1100) edge (1101)
(1110) edge (1111);
\end{tikzpicture}
\end{document}
答案1
编辑:第二版
根据 OP 的要求,保持四维空间像在原始帖子中一样绘制,这里有一个解决方案,只需使用节点名称,就可以将特定节点列表着色为您想要的任何颜色:
\documentclass[tikz, border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[
line width=0.6pt,
every node/.style={circle, draw, fill, minimum size=6pt, inner sep=0pt, font=\scriptsize\bfseries}]
\pgfsetxvec{\pgfpoint{0.9cm}{0.0cm}}
\pgfsetyvec{\pgfpoint{0.0cm}{0.9cm}}
\foreach \point / \id / \angle in {
% outer cube
(0,0)/0001/270,
(0,5)/0011/90,
(5,0)/1001/270,
(5,5)/1011/90,
(2,2)/0101/180,
(2,7)/0111/90,
(7,2)/1101/270,
(7,7)/1111/90,
% inner cube
(2.5,1.5)/0000/270,
(2.5,3.5)/0010/90,
(4.5,1.5)/1000/250,
(4.5,3.5)/1010/120,
(3.5,2.5)/0100/170,
(3.5,4.5)/0110/180,
(5.5,2.5)/1100/10,
(5.5,4.5)/1110/0}
{
\node (\id) at \point [label=\angle:\id] {};
}
% Here you have some specific nodes coloured in blue.
\foreach \id in {0011, 0101, 1011, 1101}
{
\node[blue] at (\id){};
}
% And here some other nodes couloured in red
\foreach \id in {0001, 0100, 1001, 1110}
{
\node[red] at (\id){};
}
\path
(0011) edge (1011) edge (0111) edge (0001)
(1001) edge (0001) edge (1101) edge (1011)
(1111) edge (1101) edge (1011) edge (0111)
(0010) edge (1010) edge (0110) edge (0000)
(1000) edge (0000) edge (1100) edge (1010)
(1110) edge (1100) edge (1010) edge (0110);
\path[dashed]
(0101) edge (1101) edge (0001) edge (0111)
(0100) edge (1100) edge (0000) edge (0110);
\path[dotted]
(0000) edge (0001)
(0010) edge (0011)
(0100) edge (0101)
(0110) edge (0111)
(1000) edge (1001)
(1010) edge (1011)
(1100) edge (1101)
(1110) edge (1111);
\end{tikzpicture}
\end{document}
先前版本
\documentclass[tikz, border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[
line width=0.6pt,
every node/.style={circle, draw, fill, minimum size=6pt, inner sep=0pt, font=\scriptsize\bfseries}]
\pgfsetxvec{\pgfpoint{0.9cm}{0.0cm}}
\pgfsetyvec{\pgfpoint{0.0cm}{0.9cm}}
\foreach \point / \id / \angle / \clr in {
% outer cube
(0,0)/0001/270/black,
(0,5)/0011/90/black,
(5,0)/1001/270/black,
(5,5)/1011/90/black,
(2,2)/0101/180/black,
(2,7)/0111/90/black,
(7,2)/1101/270/black,
(7,7)/1111/90/black,
% inner cube
(2.5,1.5)/0000/270/blue,
(2.5,3.5)/0010/90/blue,
(4.5,1.5)/1000/250/blue,
(4.5,3.5)/1010/120/blue,
(3.5,2.5)/0100/170/blue,
(3.5,4.5)/0110/180/blue,
(5.5,2.5)/1100/10/blue,
(5.5,4.5)/1110/0/blue}
{
\node[\clr] (\id) at \point [label=\angle:\id] {};
}
\path
(0011) edge (1011) edge (0111) edge (0001)
(1001) edge (0001) edge (1101) edge (1011)
(1111) edge (1101) edge (1011) edge (0111)
(0010) edge (1010) edge (0110) edge (0000)
(1000) edge (0000) edge (1100) edge (1010)
(1110) edge (1100) edge (1010) edge (0110);
\path[dashed]
(0101) edge (1101) edge (0001) edge (0111)
(0100) edge (1100) edge (0000) edge (0110);
\path[dotted]
(0000) edge (0001)
(0010) edge (0011)
(0100) edge (0101)
(0110) edge (0111)
(1000) edge (1001)
(1010) edge (1011)
(1100) edge (1101)
(1110) edge (1111);
\end{tikzpicture}
\end{document}