# How to get Tikz to ignore drawing on some conditions?

I am trying to develop part of my code, under the condition that if some variable inputs of the functions are equal to zero, tikz do not draw some parts of the drawings. Here is a MWE (although the MWE does not work since I could not handle the condition here):

\documentclass[border = 2mm]{standalone}
\usepackage{tikz}

\newcommand{\drawin}[1]{\draw[line width=\ifnum#1=0 0\fi] (0,0) -- (#1,0) -- (#1,#1) -- (0,#1) -- (0,0);}

\begin{document}

\begin{tikzpicture}

\drawin{5}
\drawin{3}

\end{tikzpicture}

\end{document}


In the above code, I want Tikz to ignore drawing function \drawin if the #1 is equal to zero. Please note I do not want to set the value of line weigth to zero to just not to show a drawing. I want the code itself ignore drawing.

I think you are looking for:

\newcommand{\drawin}[1]{%
\ifnum#1=0
\else
\draw (0,0) -- (#1,0) -- (#1,#1) -- (0,#1) -- (0,0);
\fi
}


Here is a version that also works with non-integer arguments. There is a also second, shorter version using the rectangle path construction.

\documentclass[border = 2mm]{standalone}
\usepackage{tikz}

\newcommand{\drawin}[1]{%
\pgfmathtruncatemacro{\itest}{ifthenelse(#1==0,0,1)}%
\ifnum\itest=1
\draw (0,0) -- (#1,0) -- (#1,#1) -- (0,#1) -- (0,0);
\fi}

\begin{document}

\begin{tikzpicture}

\drawin{5}
\drawin{3}
\drawin{2.5}
\drawin{0}

\end{tikzpicture}

\renewcommand{\drawin}[1]{%
\pgfmathtruncatemacro{\itest}{ifthenelse(#1==0,0,1)}%
\ifnum\itest=1
\draw (0,0) rectangle (#1,#1);
\fi}
\begin{tikzpicture}

\drawin{5}
\drawin{3}
\drawin{2.5}
\drawin{0}

\end{tikzpicture}

\end{document}


• Thank you, the pgf functions seem to be so varied and efficient... I need to read more about them. – enthu Jan 3 '20 at 19:29