2

I am trying to create a pic that accepts an argument which encodes what items in the pic should be drawn in a different color. The problem, however, seems not to be about the pic; therefore, I've come up with the MNWE:

\documentclass{article}
\usepackage{tikz,xstring}
\begin{document}
    \tikzset{t/.style={
        s/.style={fill=\IfSubStr{##1}{#1}{black}{}}
    }}
    \begin{tikzpicture}[t={a,b}]
        \node[s={a}] {};
        \node[s={b}] {};
        \node[s={c}] {};
    \end{tikzpicture}
\end{document}

apparently, tikz does not like the IfSubStr in the filling. How can I make the fill property depend on parameters?

3
  • What do you expect shpuld happen? Why don't you write \node[col1] ... and define e.g. col1/.style={fill=orange!20}, etc. ?
    – MS-SPO
    Apr 24 at 14:28
  • @Bubaya So basically the argument can take one value of a set of values and some pgfkeys-keys shall be set depending on which value the argument has? Probably the possible values of the argument can denote different pgfkeys-styles for setting the keys accordingly? Apr 25 at 20:41
  • @UlrichDiez Yes, exactly. Essentially, I pass a list of items (corresponding to the nodes) to the pic, and all the nodes corresponding to these items should be highlighted.
    – Bubaya
    Apr 26 at 7:13

2 Answers 2

6

The main problem is, you cannot have code like \IfSubStr in a style s or in a value-key fill.

I suggest to turn s itself into a key that processes code and calls \tikzset for setting fill, but I doubt that \IfSubStr is really what you want:

\documentclass{article}
\usepackage{tikz,xstring}
\begin{document}
    \tikzset{t/.style={
        s/.code={\IfSubStr{#1}{##1}{\tikzset{fill=green}}{\tikzset{fill=red}}}
    }}
    \begin{tikzpicture}[t={a,b}]
        \node[s={a,}] at (0,0) {`a,' is in `a,b'};
        \node[s={,}] at (0,1) {`,' is in `a,b'};
        \node[s={,b}] at (0,2) {`,b' is in `a,b'};
        \node[s={a,b}] at (0,3) {`a,b' is in `a,b'};
        \node[s={a}] at (0,4) {`a' is in `a,b'};
        \node[s={b}] at (0,5) {`b' is in `a,b'};
        \node[s={c}] at (0,6) {`c' is not in `a,b'};
    \end{tikzpicture}
\end{document}

Expl3's package l3clist provides infrastructure for checking items of comma-lists:

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \ExplSyntaxOn
    \tikzset{t/.style={
        s/.code={\clist_if_in:nnTF{#1}{##1}{\tikzset{fill=green}}{\tikzset{fill=red}}}
    }}
    \ExplSyntaxOff
    \begin{tikzpicture}[t={a,b}]
        \node[s={a,}] at (0,0) {`a,' is not an element of the list `a,b'};
        \node[s={,}] at (0,1) {`,' is not an element of the list `a,b'};
        \node[s={,b}] at (0,2) {`,b' is not an element of the list `a,b'};
        \node[s={a,b}] at (0,3) {`a,b' is not an element of the list `a,b'};
        \node[s={a}] at (0,4) {`a' is an element of the list `a,b'};
        \node[s={b}] at (0,5) {`b' is an element of the list `a,b'};
        \node[s={c}] at (0,6) {`c' is not an element of the list `a,b'};
    \end{tikzpicture}
\end{document}

You can also implement mapping of fill-colors:

\documentclass{article}
\usepackage{tikz}
\ExplSyntaxOn
\tl_new:N \l__mymodule_scratch_tl
\cs_new:Nn \__mymodule_map_kvpair:Nnnn {\tl_put_right:Nn #1 {{#3}{\tikzset{#2#4}}}}
\cs_new:Nn \__mymodule_i_iii_ii:nnn {#1{#3}{#2}}
\cs_new:Nn \__mymodule_tl_braces:nn {#1 {{#2}}}
\cs_generate_variant:Nn \__mymodule_tl_braces:nn { nV }
\tikzset{
  t/.code~2~args={
    \group_begin:
    \tl_set:Nn  \l__mymodule_scratch_tl { \tl_clear:N \l__mymodule_scratch_tl }
    \clist_map_tokens:nn {#1} {
      \tl_put_right:Nn \l__mymodule_scratch_tl {\__mymodule_map_kvpair:Nnnn \l__mymodule_scratch_tl {fill=} }
      \__mymodule_i_iii_ii:nnn { \tl_map_tokens:nn }  { \__mymodule_tl_braces:nn {  \tl_put_right:Nn \l__mymodule_scratch_tl }  }
    }
    \tl_use:N \l__mymodule_scratch_tl
    \__mymodule_tl_braces:nV {\tl_set:Nn\l__mymodule_scratch_tl} \l__mymodule_scratch_tl
    \tl_put_left:Nn \l__mymodule_scratch_tl { \str_case:nnF {##1} }
    \tl_put_right:Nn \l__mymodule_scratch_tl {\tikzset{fill=#2} }
    \__mymodule_tl_braces:nV {\tl_set:Nn\l__mymodule_scratch_tl} \l__mymodule_scratch_tl
    \tl_put_left:Nn \l__mymodule_scratch_tl { s/.code= }
    \__mymodule_tl_braces:nV {\tl_set:Nn\l__mymodule_scratch_tl} \l__mymodule_scratch_tl
    \tl_put_left:Nn \l__mymodule_scratch_tl {\group_end: \tikzset }
    \tl_use:N \l__mymodule_scratch_tl
  },
  t={{a}{cyan},{c}{green}}{none},
}
\ExplSyntaxOff
\begin{document}
    \begin{tikzpicture}
        \node[s={a,}] at (0,0) {`a,' is not mapped, thus fill=none is used.};
        \node[s={,}] at (0,1) {`,' is not mapped, thus fill=none is used.};
        \node[s={,b}] at (0,2) {`,b' is not mapped, thus fill=none is used.};
        \node[s={a,b}] at (0,3) {`a,b' is not mapped, thus fill=none is used.};
        \node[s={a}] at (0,4) {`a' is mapped to fill=cyan.};
        \node[s={b}] at (0,5) {`b'  is not mapped, thus fill=none is used.};
        \node[s={c}] at (0,6) {`c' is mapped to fill=green.};
    \end{tikzpicture}

   \vfill

    \begin{tikzpicture}[t={{a}{green},{b}{blue}}{none}]
        \node[s={a,}] at (0,0) {`a,' is not mapped, thus fill=none is used.};
        \node[s={,}] at (0,1) {`,' is not mapped, thus fill=none is used.};
        \node[s={,b}] at (0,2) {`,b' is not mapped, thus fill=none is used.};
        \node[s={a,b}] at (0,3) {`a,b' is not mapped, thus fill=none is used.};
        \node[s={a}] at (0,4) {`a' is mapped to fill=green.};
        \node[s={b}] at (0,5) {`b' is mapped to fill=blue.};
        \node[s={c}] at (0,6) {`c' is not mapped, thus fill=none is used.};
    \end{tikzpicture}

   \vfill

    \begin{tikzpicture}[t={{a}{orange},{b}{green},{c}{cyan}}{red}]
        \node[s={a,}] at (0,0) {`a,' is not mapped, thus fill=red is used.};
        \node[s={,}] at (0,1) {`,' is not mapped, thus fill=red is used.};
        \node[s={,b}] at (0,2) {`,b' is not mapped, thus fill=red is used.};
        \node[s={a,b}] at (0,3) {`a,b' is not mapped, thus fill=red is used.};
        \node[s={a}] at (0,4) {`a' is mapped to fill=orange.};
        \node[s={b}] at (0,5) {`b' is mapped to fill=green.};
        \node[s={c}] at (0,6) {`c' is mapped to fill=cyan.};
    \end{tikzpicture}

    \vfill    
\end{document}

Probably you prefer choice-keys:

\documentclass{article}
\usepackage{tikz}
\tikzset{
  /utils/exec=\newcommand*\codephrase{.code},
  s/.is choice,
  s/.unknown/.code={\tikzset{fill=none}},
  t/MyForbiddenPath/.unknown/.code={%
    \tikzset{s/\ifx\pgfkeyscurrentname\codephrase .unknown\else\pgfkeyscurrentname\fi/.code={\tikzset{fill=#1}}}%
  },
  t/.code={\tikzset{t/MyForbiddenPath/.cd, #1}}
}
\begin{document}
    \begin{tikzpicture}[t={a=orange,b=green,c=cyan,.unknown=red}]
        \node[s=a] at (0,0) {`a' is mapped to fill=orange.};
        \node[s=b] at (0,1) {`b' is mapped to fill=green.};
        \node[s=c] at (0,2) {`c' is mapped to fill=cyan.};
        \node[s=d] at (0,3) {`d' is not mapped, thus mapping to default .unknown=red.};
    \end{tikzpicture}
\end{document}

But choice-keys behave weird when the choice-value (erroneously) contains a comma.
In the example, the t-key is used for defining the choice-sub-keys for s.
The problem is, you cannot use this outside a local scope like a tikzpicture-environment and you cannot nest local scopes with calls to the t-key as already defined choice-sub-keys will not be overridden.

3
  • Instead of using \tikzset it would be more correct to use \pgfkeysalso in a .style-replacement. Also note that you have a systematic error in the naming of your L3 variables and functions. A package-internal variable should start with the scope and then have two consecutive underscores (e.g., \l__mymodule_scratch_tl), and an internal function should start with two underscores (e.g., \__mymodule_tl_braces:nn).
    – Skillmon
    Apr 25 at 7:22
  • @Skillmon Thanks for pointing out proper naming of things in expl3. Regarding \pgfkeysalso versus \tikzset I am a bit confused about "more correct": pgfmanual.pdf says: With \pgfkeysalso the default path is not modified. With \tikzset the default path is set to /tikz. While I am not sure whether using the path /tikz for defining one's own things really is good practise it seems the questioner wished things to be placed into the path /tikz for some reason. So I used \tikzset to ensure usage of this path. Apr 25 at 20:21
  • Yes, but the ultimate goal of the question (as I understand it), is to replace .style with something that evaluates things and then does the same as a .style, and a .style is defined as .code={\pgfkeysalso{#1}}.
    – Skillmon
    Apr 26 at 6:10
4

You could do something like the following, but it would be better if you could explain what you really try to do as there might be a better solution to this.

\documentclass[border=10pt]{standalone}
\usepackage{tikz, xstring}

\colorlet{mycolor}{black}
\def\mycolorlist{}

\begin{document}
    \tikzset{
        t/.code={\def\mycolorlist{#1}},
        s/.code={
            \IfSubStr{\mycolorlist}{#1}{
                \colorlet{mycolor}{black}
            }{
                \colorlet{mycolor}{red}
            }
            \tikzset{fill=mycolor}
        }
    }
    \begin{tikzpicture}[t={a,b}]
        \node[s={a}] at (0,0) {};
        \node[s={b}] at (1,0) {};
        \node[s={c}] at (2,0) {};
    \end{tikzpicture}
\end{document}

enter image description here

1
  • See my edit for what I am after.
    – Bubaya
    Apr 25 at 8:50

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