Discussion:
Tikz branching/conditional execution problems
(too old to reply)
Dave94705
2015-04-13 15:15:06 UTC
Permalink
I am not getting anything like I expect using either the "if" or the "\pgfmathifthenelse" commands within my tikz code. I want to branch and actually execute some general block of code, rather than a simple assignment of color, if condition A is true and execute another block if it is not true.

This code:
\documentclass{article}
%\usepackage{graphics}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {90,89,...,-90} { % change 89 to 80 or 45 for speed
if (\x==105) then \draw (\x/2 - 40, 0) circle [radius=2pt];
% \pgfmathifthenelse{\x==105}{\draw (\x/2 - 40, 0) circle [radius=2pt];}{}
}
\end{tikzpicture}
\end{document}

Quickly produces lots of circles indicating that the if is totally ignored.

------------

\documentclass{article}
%\usepackage{graphics}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {90,89,...,-90} { % change 89 to 80 or 45 for speed
% if (\x==105) then \draw (\x/2 - 40, 0) circle [radius=2pt];
\pgfmathifthenelse{\x==105}{\draw (\x/2 - 40, 0) circle [radius=2pt];}{}
}
\end{tikzpicture}
\end{document}

freezes up within (MikTeX's PDFTeXify) at "[Loading MPS to PDF converter <version 2006.09.02>.]" and never (~20 minutes later) gets beyond that.

-----------------

Including the graphics package:
\documentclass{article}
\usepackage{graphics}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {90,89,...,-90} { % change 89 to 80 or 45 for speed
% if (\x==105) then \draw (\x/2 - 40, 0) circle [radius=2pt];
\pgfmathifthenelse{\x==105}{\draw (\x/2 - 40, 0) circle [radius=2pt];}{}
}
\end{tikzpicture}
\end{document}

freezes up with "ABD: EveryShipout initializing macros" just after the "Loading ...." from immediately above

-------

Basically, I want to be able to conditionally branch for the execution of sizable chunks of code within tikz. There must be a way, but I don't seem to be getting it. I have skimmed the manual numerous times, but cannot see how to do. Any help or hints would be useful. Thanks, ds
GL
2015-04-14 00:30:17 UTC
Permalink
Post by Dave94705
I am not getting anything like I expect using either the "if" or the "\pgfmathifthenelse" commands within my tikz code. I want to branch and actually execute some general block of code, rather than a simple assignment of color, if condition A is true and execute another block if it is not true.
I'm not sure i understood because 105 does not appear to be in the
list 90,89,...,-90.

Anyway, programming TeX without using TeX is contradictory isn't it ?

\begin{tikzpicture}
\foreach \x in {90,89,...,-90} { % change 89 to 80 or 45 for speed
\ifnum 105=\x \relax \show\x
\draw (\x/2 - 40, 0) circle [radius=2pt];\fi
}
\end{tikzpicture}

But \x is never ever equal to 105 in the loop...
Post by Dave94705
\documentclass{article}
%\usepackage{graphics}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {90,89,...,-90} { % change 89 to 80 or 45 for speed
if (\x==105) then \draw (\x/2 - 40, 0) circle [radius=2pt];
% \pgfmathifthenelse{\x==105}{\draw (\x/2 - 40, 0) circle [radius=2pt];}{}
}
\end{tikzpicture}
\end{document}
Quickly produces lots of circles indicating that the if is totally ignored.
------------
\documentclass{article}
%\usepackage{graphics}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {90,89,...,-90} { % change 89 to 80 or 45 for speed
% if (\x==105) then \draw (\x/2 - 40, 0) circle [radius=2pt];
\pgfmathifthenelse{\x==105}{\draw (\x/2 - 40, 0) circle [radius=2pt];}{}
}
\end{tikzpicture}
\end{document}
freezes up within (MikTeX's PDFTeXify) at "[Loading MPS to PDF converter <version 2006.09.02>.]" and never (~20 minutes later) gets beyond that.
-----------------
\documentclass{article}
\usepackage{graphics}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {90,89,...,-90} { % change 89 to 80 or 45 for speed
% if (\x==105) then \draw (\x/2 - 40, 0) circle [radius=2pt];
\pgfmathifthenelse{\x==105}{\draw (\x/2 - 40, 0) circle [radius=2pt];}{}
}
\end{tikzpicture}
\end{document}
freezes up with "ABD: EveryShipout initializing macros" just after the "Loading ...." from immediately above
-------
Basically, I want to be able to conditionally branch for the execution of sizable chunks of code within tikz. There must be a way, but I don't seem to be getting it. I have skimmed the manual numerous times, but cannot see how to do. Any help or hints would be useful. Thanks, ds
Dave94705
2015-04-14 02:21:17 UTC
Permalink
Yes, that is the point. I just wanted to create an example that was easy to was not doing what I/someone would expect if the if statement did what is expected. The problem with TeX's \if (and its relatives) is that I have some more complicated comparisons I want to make. I can use the ifthen package, but I was trying to use tikz commands within the tikz environment. I also didn't understand why certain packages failed to load and continue. Nevertheless, thanks for your \ifnum idea.
Dave94705
2015-04-14 02:23:14 UTC
Permalink
Yes, that is the point, there should be no circles. I just wanted to create an example that was easy to see was not doing what I/someone would expect if the if statement did what is expected. The problem with TeX's \if (and its relatives) is that I have some more complicated comparisons I want to make. I can use the ifthen package, but I was trying to use tikz commands within the tikz environment. I also didn't understand why certain packages failed to load and continue when I used tikz' ifthenelse structure. Nevertheless, thanks for your \ifnum idea.
GL
2015-04-14 04:19:34 UTC
Permalink
Post by Dave94705
Yes, that is the point, there should be no circles. I just wanted to create an example that was easy to see was not doing what I/someone would expect if the if statement did what is expected. The problem with TeX's \if (and its relatives) is that I have some more complicated comparisons I want to make. I can use the ifthen package, but I was trying to use tikz commands within the tikz environment. I also didn't understand why certain packages failed to load and continue when I used tikz' ifthenelse structure. Nevertheless, thanks for your \ifnum idea.
Could you please add carriage returns (possibly every 80
characters) when you post.

I just can't read you.

If you have more complex comparisons to do,
i would be very, very, very surprised you
couln't do them with TeX conditionnals. i would be shocked !

Regards

Loading...