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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:





\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)};

share|improve this question
\ifnum\myq=0 \relax \else \fill (B) circle (4pt); \fi – Claudio Fiandrino Apr 10 '14 at 7:25
Or \ifthenelse{\myq=0}{}{\fill (B) circle (4pt)}; – Torbjørn T. Apr 10 '14 at 7:27
Or \pgfmathifthenelse{\myq==0}{}{"\noexpand\fill (B) circle (4pt);"}\pgfmathresult – esdd Apr 10 '14 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 '14 at 8:25
up vote 7 down vote accepted

With pgfmath the correct syntax is

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


\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


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.