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.

  • 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}
| improve this answer | |
  • 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
8

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

| improve this answer | |
  • 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

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.