0

The following code draws a hypercube with labeled nodes. This is working fine so far. Now, I'd like to colour some specific nodes in a different colour. The PGF manual says in chapter 16.14 (page 207) by using \node also

[...] most of the options will have no effect since you cannot change the appearance of the node, that is, you cannot change a red node into a green node using these “late” options. However, giving the append after command and prefix after command options inside the late options (directly or indirectly) does have the desired effect: The given path gets executed with the \tikzlastnode set to the determined node.

All my approaches to colour a node in blue failed though. Can someone help me with this?
Btw, I have a working solution, where I'm able to colour each node individually, because I coded the cube manually without a for loop. This question is about a solution, using a for loop to draw the cube.

MWE:

\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}

The desired hypercube in this example should be:
Hypercube

  • In your every node parameter, define fill=blue. Does this help? – Someone May 25 '21 at 21:02
  • Why don't you add a fourth value into your foreach loop, like \foreach \point / \id / \angle / \colour, then add the colour of the node inside the datas? Please feel free to be more specific about what nodes you want to draw in another colour (and what colour/colours). – SebGlav May 25 '21 at 21:03
  • Would something similar to code 1 in https://tex.stackexchange.com/a/106976/ be an option? I.e. define a style for every node that should be colored with '\tikzset{ 1011/.style={blue}, 0011/.style={blue} } and so on. Then modify the loop to say \node (\id) at \point [label=\angle:\id, \id/.try] {};, i.e. add , \id/.try in the node style. – Torbjørn T. May 25 '21 at 23:16

1 Answers1

3

EDIT: SECOND VERSION

Following OP's demand to keep the tesseract drawn like in the original post, here's a solution to colour a list of particular nodes in whatever colour you want, just by using their node names:

tesseract in blue v2

\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}

Previous version

tesseract in blue

\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}

SebGlav
  • 19,186
  • I haven't thought about adding a 4th parameter, good idea. But I'd like to keep the cube, made by the first loop (all nodes = black), as a template and override nodes with a different colour afterwards. Lets say I'd like to draw 20 cubes, each with a different set of black/blue nodes, then I don't want to copy&paste the entire code over and over and edit each line individually. I'd rather call my template and override the nodes which have to change. Therefore I'm interested in a solution that uses the method mentioned in the manual with \node also and apped after command or so. – Tatsuno May 25 '21 at 21:55
  • @Tatsuno Your quote from the manual specifically says "you cannot change a red node into a green node using these “late” options." ... – Torbjørn T. May 25 '21 at 22:34
  • I edited my answer, hoping this will fit your needs. Drawing a node at the exact same place of one other seems to me to be the best solution ;) – SebGlav May 26 '21 at 14:40
  • +1 Good evening, it is better to put your last answer at the top so that it is read first. The previous answers show the evolution of your answer and remain present for the readers curious to understand the evolution of your answer. – AndréC May 26 '21 at 20:03
  • @AndréC Maybe you're right, I'll keep that in mind, and welll, I can change it here too, if you think it's a better habit ;) – SebGlav May 26 '21 at 21:16