Graphics, Figures & Tablespredefining colors in TikZ

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Antoine C
Posts: 1
Joined: Thu Sep 22, 2016 6:11 pm

predefining colors in TikZ

Post by Antoine C »

[This is my very first question, so please judge me charitably if I am not presenting it in the expected format!]

I am running into problem when using a color in TikZ which I have predefined using conditionals. The message I get is
Package pgfkeys Error: I do not know the key '/tikz/ green ' and I am going t
o ignore it. Perhaps you misspelled it.
In the minimal example below, the first call to TikZ works fine, but not the second. I tried using {...} but it doesn't seem to do anything.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
\def\mycolor#1{\ifnum#1=0
green
\else
red
\fi
}
\def\somecolor{green}
\tikz \fill[\somecolor](0,0) rectangle (1,1);
\tikz \fill[\mycolor0](0,0) rectangle (1,1);
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

predefining colors in TikZ

Post by rais »

Welcome aboard :)

Your definition of \mycolor introduces whitespace at the end of the color name.
Without this space it works for me:

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\def\mycolor#1{%
  \ifnum#1=0
    green%
  \else
    red%
  \fi
}
\begin{document}
\tikz \fill[\mycolor1](0,0) rectangle (1,1);
\tikz \fill[\mycolor0](0,0) rectangle (1,1);
\end{document} 
KR
Rainer
Post Reply