2

As shown below, the code fails to redefine the color when it is white (e.g. rgb(1,1,1)).

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
   \foreach \code/\col in {%
   {1,1,1}/white,
   {1,1,0}/yellow,
   {1,0,1}/pink
   }{

   \ifx {\code} {1,1,1}
   \definecolor{tempcolor}{rgb}{1,0,0}
   \else
   \definecolor{tempcolor}{rgb}{\code}
   \fi
   \textcolor{tempcolor}{\col};
   }

\end{document}

enter image description here

Why is my condition failing?

2 Answers 2

1

REVISION: Your original attempt works almost literally.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
   \edef\speciallist{1,1,1}
   \foreach \code/\col in {%
   {1,1,1}/white,
   {1,1,0}/yellow,
   {1,0,1}/pink
   }{

   \ifx\code\speciallist
    \definecolor{tempcolor}{rgb}{1,0,0}
   \else
    \definecolor{tempcolor}{rgb}{\code}
   \fi
    \textcolor{tempcolor}{\col};
   }

\end{document}

enter image description here

OLDER ANSWER: TikZ comes with all the tools to compare these lists, you do not need to load additional packages. (BTW, you also do not have to load xcolor.) In more detail, TikZ allows you to parse lists (or arrays), and this allows you to define a quantity that is 0 if all entries coincide with your target list and 1 otherwise. In more detail, I compute a quantity

\pgfmathtruncatemacro{\myx}{sign(abs({\code}[0]-{\speciallist}[0])+abs({\code}[1]-{\speciallist}[1])+abs({\code}[2]-{\speciallist}[2]))}

where

  • \pgfmathtruncatemacro ensures that one gets an integer, such that \ifnum, which only works for integers, works.
  • the argument is |first entry of \code - first entry of \speciallist|+|second entry of \code - second entry of \speciallist|+|third entry of \code - third entry of \speciallist|.

Clearly, this quantity is only 0 if all the entries of the lists coincide. Here, {\code}[0] evaluates to the first entry of the list \code, and so on.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
   \edef\speciallist{1,1,1}
   \foreach \code/\col in {%
   {1,1,1}/white,
   {1,1,0}/yellow,
   {1,0,1}/pink
   }{
   \pgfmathtruncatemacro{\myx}{sign(abs({\code}[0]-{\speciallist}[0])+abs({\code}[1]-{\speciallist}[1])+abs({\code}[2]-{\speciallist}[2]))}
   \ifnum\myx=0
    \definecolor{tempcolor}{rgb}{1,0,0}
   \else
    \definecolor{tempcolor}{rgb}{\code}
   \fi
    \textcolor{tempcolor}{\col};
   }

\end{document}
4
  • Thanks for your help! Do you mind to explain or point where to get more info about this? \pgfmathtruncatemacro{\myx}{sign(pow({\code}[0]-1,2)+pow({\code}[1]-1,2)+pow({\code}[2]-1,2))}
    – Tony Tan
    Jan 27, 2019 at 5:01
  • @TonyTan I added an explanation to my answer. \pgfmathtruncatemacro{\myx}{sign(pow({\code}[0]-1,2)+pow({\code}[1]-1,2)+pow({\code}[2]-1,2))} is very similar except that one takes the second power of the differences rather than computing their absolute values.
    – user121799
    Jan 27, 2019 at 5:09
  • 1
    @TonyTan A slight variation of your original proposal works, and is much simpler.
    – user121799
    Jan 27, 2019 at 6:07
  • very nice. Thanks again for your time and efforts! @marmot
    – Tony Tan
    Jan 28, 2019 at 6:24
1

You're technically hoping to see whether \code equals 1,1,1 as a text string. You can use \pdfstrcmp{<strA>}{<strB>} for this. It returns -1/0/1 if <strA> is smaller than/equal to/greater than <strB> (lexicographically):

enter image description here

\documentclass{article}

\usepackage{tikz}

\begin{document}

\foreach \code/\col in {%
    {1,1,1}/white,%
    {1,1,0}/yellow,%
    {1,0,1}/pink%
  }{

  \ifnum\pdfstrcmp{\code}{1,1,1}=0
    \definecolor{tempcolor}{rgb}{1,0,0}%
  \else
    \definecolor{tempcolor}{rgb}{\code}%
  \fi
  \textcolor{tempcolor}{\col};
}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .