2

this is some kind of an extension of latex/tikz macro conditional set options for \draw

I've got something like this

\definecolor{color1}{HTML}{cccdf1}

\newif\ifCond
\tikzset{
    cond/.is if=Cond,Cond/.default=false,
    bgCol/.is choice,
    bgCol/blue/.code={\tikzset{myrect/.style={fill=color1}}},
    bgCol/red/.code={\tikzset{myrect/.style={fill=red}}},
}

\newcommand{\myTikZdrawing}[1][Cond=false]{
    \begin{tikzpicture}[#1]
        \draw (0,0) rectangle (1,1);

        \ifCond
        \draw[myrect] (0,0) rectangle (0.5,0.5) node[midway] {text};
        \fi
    \end{tikzpicture}
}

Now I'd like to specify with some tikz key the content of the node text when using the macro later on in the document.

0
3

You can store the value of a pgf key in some macro that you fill in the node. The pgf key can be changed.

\documentclass{article}
\usepackage{tikz}
\definecolor{color1}{HTML}{cccdf1}

\newif\ifCond
\tikzset{
    Cond/.is if=Cond,Cond/.default=true,
    myrect/.style={},
    my text/.store in=\mytext,
    my text={A},
    bgCol/.is choice,
    bgCol/blue/.code={\tikzset{myrect/.style={fill=color1}}},
    bgCol/red/.code={\tikzset{myrect/.style={fill=red,draw=cyan,dashed}}},
}

\Condtrue
\newcommand{\myTikZdrawing}[1][Cond=false]{
    \begin{tikzpicture}[#1]
        \draw (0,0) rectangle (1,1);

        \ifCond
        \draw[myrect] (0,0) rectangle (0.5,0.5) node[midway] 
        {\mytext};
        \fi
    \end{tikzpicture}
}
\begin{document}
\myTikZdrawing
\bigskip

\myTikZdrawing[Cond]
\bigskip

\myTikZdrawing[Cond,bgCol=blue,my text=B]
\bigskip

\myTikZdrawing[Cond,bgCol=red,my text=C]
\bigskip

\end{document}

enter image description here

If you do not want to "waste" a macro on this, you can work with \pgfkeysvalueof and the /.initial key handler.

\documentclass{article}
\usepackage{tikz}
\definecolor{color1}{HTML}{cccdf1}

\newif\ifCond
\tikzset{
    Cond/.is if=Cond,Cond/.default=true,
    myrect/.style={},
    my text/.initial={A},
    bgCol/.is choice,
    bgCol/blue/.code={\tikzset{myrect/.style={fill=color1}}},
    bgCol/red/.code={\tikzset{myrect/.style={fill=red,draw=cyan,dashed}}},
}

\Condtrue
\newcommand{\myTikZdrawing}[1][Cond=false]{
    \begin{tikzpicture}[#1]
        \draw (0,0) rectangle (1,1);

        \ifCond
        \draw[myrect] (0,0) rectangle (0.5,0.5) 
            node[midway] 
        {\pgfkeysvalueof{/tikz/my text}};
        \fi
    \end{tikzpicture}
}
\begin{document}
\myTikZdrawing
\bigskip

\myTikZdrawing[Cond]
\bigskip

\myTikZdrawing[Cond,bgCol=blue,my text=B]
\bigskip

\myTikZdrawing[Cond,bgCol=red,my text=C]
\bigskip

\end{document}

The result is as above.

2
  • 3
    On the long run, if you produce many of these keys, you may want to start putting them in your own subdirectories. – user194703 May 17 '20 at 13:59
  • Ok, looks like I'll have to learn this as well as checking if there is a way to get an if not checking a boolean but checking a tikz key for hits value. (I'm still new to tikz and it's just such a huge topic to learn) – atticus May 17 '20 at 14:03

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.