Let's say I have defined some colors. I'd like to produce a test page that functions as a color-key for developers.


The defined colors colora, colorb, and colorc should be iterated without mentioning them explicitly again (happens automatically). This is efficient programming in that I can have an arbitrary number of colors (perhaps 50 or so) and create a time-saving color key.




%\foreach \definedcolor in {defined colors} % pseudo-code
%   \tikz \node [fill=definedcolor, minimum width=2cm, minimum height=1cm,text=white] {name of defined color}; % pseudo-code
\color{colora} COLORA

\color{colorb} COLORB

\color{colorc} COLORC
  • Should any pre-defined named colors (such as blue, red, cyan, etc.) be included or excluded from this loop? What if your color definitions overwrite a pre-defined color name? Mar 29 '15 at 19:32

One possible way is to intercept the color definition with your own macro.


\csname pgfutil@ifundefined\endcsname{definedcolorsofar}{\def\definedcolorsofar{#1}}{%


\foreach \x[count=\xi] in \definedcolorsofar 
   \node at (0,\xi)[fill=\x, minimum width=2cm, 
           minimum height=1cm,text=white] {\x}; 


enter image description here

  • Could you please explain \csname pgfutil@ifundefined\endcsname? Apr 9 '15 at 13:29
  • 1
    @macmadness86 Normally if a control sequence has @ character in it you have to put the code inside \makeatletter...\makeatother pair (if it is a package LaTeX handles behind the scene). But if you only have a few of those you can place it within \csname...\endcsname and it will handle it automatically such that you don't need to change the catcode of @. \pgfutil@ifdefined is an if syntax and checks whether the macro has been defined already or not. So if not defined I create, if already exists I append there.
    – percusse
    Apr 9 '15 at 13:42
  • I know this question is old, but I just had a request for putting the cmyk values explicitly on the page. Is that also possible with a minor adjustment to the code? Aug 14 '15 at 12:42
  • @macmadness86 I think I didn't get exactly the additional requirement can you elaborate a bit?
    – percusse
    Aug 14 '15 at 18:13
  • For example, if I wanted not just "colora" to be printed, but also cymk and its values: {cmyk}{0,.5,1,.3} either decimally like that or in percentages. The reason for this is that I am designing a style sheet, where the outputted PDF can be used as a reference for other users who might want to user a color. They can then easily reference the cmyk values. Aug 16 '15 at 21:07

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.