8

I want to fill a node with a specific color, but not with the usual [fill=blue] way. The reason is, that I am creating many many nodes and I want to fill some random nodes with a different color from the others, so the coloring can not be embedded in the creation process. Any ideas?

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}

\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=gray!20,minimum size=20}]


  \foreach \x in {0,...,2}
    \foreach \y in {0,...,2} 
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (1.5*\x,1.5*\y) {};} 

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,1}  
      \draw (\x\y)--(\x\yi)  ;


\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south); 

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,3} 
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (10+1.5*\x,1.5*\y) {};} 

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,2}  
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south); 



  \foreach \x in {0,...,5}
    \foreach \y in {0,...,4} 
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (3+1.5*\x,-10+1.5*\y) {};} 

  \foreach \x in {0,...,5}
    \foreach \y [count=\yi] in {0,...,3}  
      \draw (\x\y)--(\x\yi)  ;



\end{tikzpicture}
\end{document}  

Lets say we want to fill with blue color the top leftmost node and the bottom rightmost node.

  • 1
    why can't you colour randomly as you create? in any case your question would be much clearer (and much easier to test possible answers) if you provided a sample document. – David Carlisle Feb 28 '15 at 18:05
  • First idea: Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Feb 28 '15 at 18:05
  • Please see the edit – Paramar Feb 28 '15 at 18:11
5

enter image description here

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}

\begin{document}
\begin{tikzpicture}[
darkstyle/.style={circle,draw,fill=gray!20,minimum size=20},
bluestyle/.style={circle,draw,fill=blue!20,minimum size=20},
]


  \foreach \x in {0,...,2}
    \foreach \y in {0,...,2} 
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
\ifnum\x=\ifnum\y=2 0\else-1\fi
       \node [bluestyle]  (\x\y) at (1.5*\x,1.5*\y) {};
\else
       \node [darkstyle]  (\x\y) at (1.5*\x,1.5*\y) {};
\fi
       } 

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,1}  
      \draw (\x\y)--(\x\yi)  ;


\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south); 

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,3} 
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (10+1.5*\x,1.5*\y) {};} 

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,2}  
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south); 



  \foreach \x in {0,...,5}
    \foreach \y in {0,...,4} 
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
\ifnum\x=\ifnum\y=0 5\else-1\fi
       \node [bluestyle]  (\x\y) at (3+1.5*\x,-10+1.5*\y) {};
\else
       \node [darkstyle]  (\x\y) at (3+1.5*\x,-10+1.5*\y) {};
\fi

} 

  \foreach \x in {0,...,5}
    \foreach \y [count=\yi] in {0,...,3}  
      \draw (\x\y)--(\x\yi)  ;



\end{tikzpicture}
\end{document}
  • Can I group in your if statement more coordinates? (Like not only 2 0 but also 1 1)? Thanks a lot – Paramar Feb 28 '15 at 18:53
  • 1
    @Paramar you can use any tex tests, there are packages that let you use more sensible infix logical operators or you could just \ifnum\x=\ifnum\y=0 5\else-1\fi \node [bluestyle] (\x\y) at (3+1.5*\x,-10+1.5*\y) {}; \else\ifnum\x=\ifnum\y=1 1\else-1\fi \node [bluestyle] (\x\y) at (3+1.5*\x,-10+1.5*\y) {};\else \node [darkstyle] (\x\y) at (3+1.5*\x,-10+1.5*\y) {}; \fi\fi – David Carlisle Feb 28 '15 at 18:59
10

In your question you said that you want to fill random nodes. If you want a realy random choice you can do the following random darkstyle style :

\documentclass[tikz, border=7pt]{standalone}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}
\tikzset{
  % ----- the static part of the style
  darkstyle/.style={
    circle,minimum size=20,draw
  },
  % ----- the random part of the style
  darkstyle/.append code = {
    \pgfmathsetmacro\randcolor{{"red!50","blue!35"}[int(2*random())]}
    \pgfkeysalso{fill=\randcolor}
  }
}
\begin{document}
\begin{tikzpicture}

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,2}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (1.5*\x,1.5*\y) {};}

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,1}
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,3}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (10+1.5*\x,1.5*\y) {};}

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,2}
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);

  \foreach \x in {0,...,5}
    \foreach \y in {0,...,4}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (3+1.5*\x,-10+1.5*\y) {};}

  \foreach \x in {0,...,5}
    \foreach \y [count=\yi] in {0,...,3}
      \draw (\x\y)--(\x\yi)  ;

\end{tikzpicture}
\end{document}

enter image description here

EDIT : And if you don't realy want a random choice, you can prescribe some nodes to be with different color like this :

\documentclass[tikz, border=7pt]{standalone}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}
\tikzset{
  % ----- the static part of the style
  darkstyle/.style={
    circle,minimum size=20,draw, fill = blue!35
  },
  % ----- the prescribed part pf the style
  mycolor/.style = {fill=red!35},
  mycolorA12/.style = {mycolor},
  mycolorA20/.style = {mycolor},
  mycolorB11/.style = {mycolor},
  mycolorC54/.style = {mycolor},
}
\begin{document}
\begin{tikzpicture}

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,2}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle,mycolorA\x\y/.try]  (\x\y) at (1.5*\x,1.5*\y) {};}

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,1}
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,3}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle,mycolorB\x\y/.try]  (\x\y) at (10+1.5*\x,1.5*\y) {};}

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,2}
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);

  \foreach \x in {0,...,5}
    \foreach \y in {0,...,4}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle,mycolorC\x\y/.try]  (\x\y) at (3+1.5*\x,-10+1.5*\y) {};}

  \foreach \x in {0,...,5}
    \foreach \y [count=\yi] in {0,...,3}
      \draw (\x\y)--(\x\yi)  ;

\end{tikzpicture}
\end{document}

enter image description here

  • Great answer, I upvoted it. I do not really need a real random choice, but I learned something from your code I did not know in the past. Thank you – Paramar Feb 28 '15 at 20:04
  • 1
    Ok. If "random nodes" doesn't meen random, I came to edit another not so random styling ;) – Kpym Feb 28 '15 at 20:13
0

First, consider a simpler example (one that applies to the original example directly) so that the answer can actually be understood and adapted by people from the future:

  • (node1) and (node2) are respectively drawn as a circle and a rectangle, and
  • we seek to re-color the former as blue.

I don't know if re-coloring is possible, but if you haven't already drawn something else over (node1), then drawing a blue-colored node right on top of (node1) could work for you:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=gray!20,minimum size=20}]

    \node [darkstyle] (node1) at (2,1) {};
    \node [darkstyle, rectangle] (node2) at (4,2) {};

    % What to write here to change the color of (node1) to blue?
    % A workaround:
    \node [darkstyle, fill=blue!30] at (node1) {};

\end{tikzpicture}
\end{document}

Output:

nodes without text


If your old node also had some text in it, then that also needs to be the label of the overlaid node. Those can either be hard-coded, or stored in a macro and recalled. Let's do it with (node2) this time:

\documentclass{standalone}
\usepackage{tikz}

% Thanks to: https://tex.stackexchange.com/q/175707/69346
\newcommand\storelabel[2]{\expandafter\xdef\csname label#1\endcsname{#2}}
\newcommand\getlabel[1]{\csname label#1\endcsname}

\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=gray!20,minimum size=20}]

    \node [darkstyle] (node1) at (2,1) {\storelabel{node1}{5.7}\getlabel{node1}};
    \node [darkstyle, rectangle] (node2) at (4,2) {\storelabel{node2}{9000.1}\getlabel{node2}};

    % What to write here to change the color of (node2) to blue?
    % A workaround (note the "rectangle" in the options):
    \node [darkstyle, rectangle, fill=blue!30] at (node2) {\getlabel{node2}};

\end{tikzpicture}
\end{document}

Output:

nodes with text


An answer 5 years late, because the others completely dismiss the "after creation" part of the original request. I mind you that this one also does not re-color an already-drawn node, therefore has drawbacks, and will not be applicable to everyone. However, the workaround offered here at least does something after creation, as requested.

I would say that this question, even after this one, still needs an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.