\newcommand and boolean statements
Here a way using xparse
and l3prop
. The usage of l3prop
allows more modifications.
Maybe the tikz-part can be improved ;-)
\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{xparse,expl3}
\ExplSyntaxOn
\prop_new:N \l_alenanno_color_prop
\prop_put:Nnn \l_alenanno_color_prop {0} {white}
\prop_put:Nnn \l_alenanno_color_prop {1} {black}
\NewDocumentCommand {\command} { > { \SplitArgument { 2 } { , } } m }
{
\alenanno_command_aux:nnn #1
}
\cs_new:Npn \alenanno_command_aux:nnn #1 #2 #3
{
\begin{tikzpicture}
\draw[fill=\prop_get:Nn \l_alenanno_color_prop { #1 }] (0,0) rectangle (1,1);
\draw[fill=\prop_get:Nn \l_alenanno_color_prop { #2 }] (0,1) rectangle (1,2);
\draw[fill=\prop_get:Nn \l_alenanno_color_prop { #3 }] (0,2) rectangle (1,3);
\end{tikzpicture}
}
\ExplSyntaxOff
\begin{document}
\command{1,0,1} \command{0,0,1} \command{0,1,1}
\end{document}
Here a solution using \node
:
\documentclass[10pt]{article}
\usepackage{tikz}
\tikzset{mynodestyle/.style={minimum height=1cm,minimum width=1cm,outer sep=0pt,rectangle,draw=black}}
\usepackage{xparse,expl3}
\ExplSyntaxOn
\prop_new:N \l_alenanno_color_prop
\prop_put:Nnn \l_alenanno_color_prop {0} {white}
\prop_put:Nnn \l_alenanno_color_prop {1} {black}
\NewDocumentCommand {\command} { > { \SplitArgument { 2 } { , } } m }
{
\alenanno_command_aux:nnn #1
}
\cs_new:Npn \alenanno_command_aux:nnn #1 #2 #3
{
\begin{tikzpicture}
\node[mynodestyle, fill=\prop_get:Nn \l_alenanno_color_prop { #2 }] (P) {};
\node[mynodestyle, fill=\prop_get:Nn \l_alenanno_color_prop { #1 },anchor=south] at (P.north) {};
\node[mynodestyle, fill=\prop_get:Nn \l_alenanno_color_prop { #3 },anchor=north] at (P.south) {};
\end{tikzpicture}
}
\ExplSyntaxOff
\begin{document}
\command{1,0,1} \command{0,0,1} \command{0,1,1} \command{0,0,0}
\end{document}
It's important to know that inside \ExplSyntaxOn
... \ExplSyntaxOff
all spaces are ignored. This is explained here: What do ExplSyntaxOn and ExplSyntaxOff do?. So you can't use TikZ options like minimum with
You can temporally disable this behaviour as described her: Text within ExplSyntaxOn/Off or do the setting outside.
As mentioned by egreg: If you want to use some TikZ options which require a space you can use the symbol ~
. That means:
\draw[fill=\prop_get:Nn \l_alenanno_color_prop { #3 },rounded~corners] (0,2) rectangle (1,3);
I'd have needed more help in the MWE to make that with Tikz, but here's the testing part:-)
\documentclass[10pt]{article}
\def\command#1{\xcommand#1\relax}
\def\xcommand#1,#2,#3\relax{%
\begin{tabular}{|l|}
\relax\ifnum#1>0 \leaders\vline\fi\hskip 1em\mbox{}\\
\relax\ifnum#2>0 \leaders\vrule\fi\hskip 1em\mbox{}\\
\relax\ifnum#3>0 \leaders\vrule\fi\hskip 1em\mbox{}%
\end{tabular}}
\begin{document}
\command{1,0,1}
\bigskip
\command{0,1,0}
\end{document}
Why waste electrons and use a comma list? 010
conveys the same information. Here is a short solution. If you insist on commas, change @tfor
to @for
. Solution is extensible, use as many 0s or 1s you wish.
\documentclass{article}
\usepackage{xcolor}
\fboxsep0pt
\makeatletter
\def\roll#1{%
\def\boxblack{\rule{1cm}{1cm}}%
\def\boxwhite{{\color{white}\rule{1cm}{1cm}}}%
\fbox{\parbox{1cm}{%
\@tfor\next:=#1\do{%
\ifnum\next=0\boxblack\else\boxwhite\fi%
\par
}}}}
\makeatother
\begin{document}
\roll{01010}
\end{document}
Posted a solution to illustrate that it is always best to generalize the problem. It is also best IMHO to avoid heavyweight libraries when simpler solutions exist.