# latex/tikz macro conditional set options for \draw

this is some kind of a expansion of latex/tikz macro conditional

up to now, I have something like this:

\newif\ifCond
\tikzset{
cond/.is if=Cond,Cond/.default=false,
}

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

\ifCond
\draw (0,0) rectangle (0.5,0.5);
\fi
\end{tikzpicture}
}


This works so far for (de)activating that the second rectangle is drawn. But now I'd like to set the background color of the first rectangle when executing this macro (best would be again with tikz keys).

Up to now I tried something like this with an .is choice key:

\definecolor{color1}{HTML}{cccdf1}

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

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

\ifCond
\draw (0,0) rectangle (0.5,0.5);
\fi
\end{tikzpicture}
}


Now I can pass [bgCol=blue] to the draw command, but I didn't found a way to set this option from the outside when using this macro later on in the document.

Any suggestions?

Something like this?

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

\newif\ifCond
\tikzset{
Cond/.is if=Cond,Cond/.default=true,
myrect/.style={},
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);
\fi
\end{tikzpicture}
}
\begin{document}
\myTikZdrawing
\bigskip

\myTikZdrawing[Cond]
\bigskip

\myTikZdrawing[Cond,bgCol=blue]
\bigskip

\myTikZdrawing[Cond,bgCol=red]
\bigskip

\end{document}


• Thanks, just one more question to go "how to specify some kind of node text for the rectangle" but this will be a new post ;) – atticus May 17 '20 at 13:38