Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm continuing my work on the "changeable" graphics. I have an array of values, and I have already succeeded in making variables from it. The next step is that I want to ignore the zero values. Here it is, what I have a problem with:

\pgfmathifthenelse{\myq==0}{}{\fill (B) circle (4pt)};

I thought this code should draw a circle in the node B in case the variable \myq has non-zero value. But I cannot even compile the code, compilation freezes. Can you figure out the possible problem or give an advice?

The whole code is:

\begin{tikzpicture}
\def\zamkl{{3,4,4.5,5}}
\def\zamkh{{4,3.5,5,4.5}}
\def\zamkq{{20,0,30,0}}

\pgfmathparse{\zamkl[0]}
\edef\myl{\pgfmathresult}

\pgfmathparse{\zamkh[0]}
\edef\myh{\pgfmathresult}

\pgfmathparse{\zamkq[0]}
\edef\myq{\pgfmathresult}

\draw[help lines] (0,0) grid (2*\myl,2*\myh);

\coordinate (A) at (0,0);
\coordinate (B) at (\myl,0); \node at (B) [below] {$T$};
\coordinate (C) at (2*\myl,0);
\coordinate (D) at (2*\myl,\myh); \node at (D) [left] {$S$};
\coordinate (E) at (2*\myl,2*\myh);
\coordinate (F) at (\myl,2*\myh); \node at (F) [below] {$R$};
\coordinate (G) at (0,2*\myh);
\coordinate (H) at (0,\myh); \node at (H) [right] {$W$};

\draw [very thick] (A) -- (C) -- (E) --(G) -- (A);

\pgfmathifthenelse{\myq==0}{}{\fill (B) circle (4pt)};

\end{tikzpicture}
share|improve this question
2  
\ifnum\myq=0 \relax \else \fill (B) circle (4pt); \fi –  Claudio Fiandrino Apr 10 at 7:25
    
Or \ifthenelse{\myq=0}{}{\fill (B) circle (4pt)}; –  Torbjørn T. Apr 10 at 7:27
    
Or \pgfmathifthenelse{\myq==0}{}{"\noexpand\fill (B) circle (4pt);"}\pgfmathresult –  esdd Apr 10 at 8:22
    
@esdd: your solution is the most correct according to OP's intentions; why don't you add an answer with all methods? –  Claudio Fiandrino Apr 10 at 8:25

1 Answer 1

up vote 7 down vote accepted

With pgfmath the correct syntax is

\pgfmathifthenelse{\myq==0}{}{"\noexpand\fill (B) circle (4pt);"}\pgfmathresult

or

\pgfmathparse{\myq==0?:"\noexpand\fill (B) circle (4pt);"}\pgfmathresult

Every expression is expanded with \edef before it is parsed. Using \noexpand protects \fill from this expansion. And as always \pgfmathifthenelse returns the result in the macro \pgfmathresult.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.