2

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.

6

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
}
| improve this answer | |
3

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}

enter image description here

| improve this answer | |
  • Thank you, the pgf functions seem to be so varied and efficient... I need to read more about them. – Enthusiastic Engineer Jan 3 at 19:29

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.