LaTeX forum ⇒ Graphics, Figures & Tablespredefining colors in TikZ

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

predefining colors in TikZ

Postby Antoine C » Thu Sep 22, 2016 6:42 pm

[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.

  1. \documentclass{minimal}
  2. \usepackage{tikz}
  3. \usepackage{xcolor}
  4. \begin{document}
  5. \def\mycolor#1{\ifnum#1=0
  6. green
  7. \else
  8. red
  9. \fi
  10. }
  11. \def\somecolor{green}
  12. \tikz \fill[\somecolor](0,0) rectangle (1,1);
  13. \tikz \fill[\mycolor0](0,0) rectangle (1,1);
  14. \end{document}

Tags:

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

Postby rais » Thu Sep 22, 2016 9:30 pm

Welcome aboard :)

Your definition of \mycolor introduces whitespace at the end of the color name.
Without this space it works for me:
  1. \documentclass{minimal}
  2. \usepackage{tikz}
  3. \def\mycolor#1{%
  4. \ifnum#1=0
  5. green%
  6. \else
  7. red%
  8. \fi
  9. }
  10. \begin{document}
  11. \tikz \fill[\mycolor1](0,0) rectangle (1,1);
  12. \tikz \fill[\mycolor0](0,0) rectangle (1,1);
  13. \end{document}

KR
Rainer


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 5 guests