LaTeX forum ⇒ LaTeX Beginner's Guideifthenelse Topic is solved

Questions and answers about the LaTeX Beginner's Guide
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

ifthenelse

Postby topsquark » Tue Aug 22, 2023 2:59 am

I'm not really finding examples on the net and (as usual) I'm finding the PGF/TikZ manual to be less than useful.

I'm trying to determine the quadrant a point is in. Heck, let's get simpler. I want to find out if 1 = 1.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
      \ifthenelse{\equal{1}{1}}{true}{false};
\end{tikzpicture}

\end{document}


But this doesn't work. And even if it did, where is the result stored? The examples I've found on the net, which are virtual copies of this, don't say how to get the result.

Gah!

I'd like to do something like the following:

\def\px{1}
\def\py{-1}
\ifthenelse{\and{\greater{\px}{0}}{greater{\py}}}{1}{};
\ifthenelse{\and{\greater{0}{\px}}{greater{\py}}}{2}{};
\ifthenelse{\and{\greater{0}{\px}}{greater{0}{\py}}}{3}{};
\ifthenelse{\and{\greater{\px}{0}}{greater{0}{\py}}}{4}{};

to get the result 4.

Thanks!

-Dan

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics
User avatar
Stefan Kottwitz
Site Admin
Posts: 10228
Joined: Mon Mar 10, 2008 9:44 pm

ifthenelse

Postby Stefan Kottwitz » Tue Aug 22, 2023 12:53 pm

Hi Dan,

you need to load the ifthen package and print the result in a node:

\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}
  \node {\ifthenelse{\equal{1}{1}}{true}{false}};
\end{tikzpicture}
\end{document}


Stefan
LaTeX.org admin

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

ifthenelse

Postby topsquark » Tue Aug 22, 2023 5:23 pm

Yes, I just found out about the ifthen package thing this morning. (I found a couple new examples.) I actually got some new information:
1. Apparently \pgfmathresult takes the very last value in the code, so the command
\node at (0,0) {\pgfmathresult}

will always print a 0 and
\node at (0,-5) {\pgfmathresult}

will always print a -5. That explains some of my troubles.

2. I actually got the quadrant thing to work:
\documentclass{article}

\usepackage{tikz}
\usepackage{ifthen}

\begin{document}

\begin{tikzpicture}
\def\px{-1}
\def\py{-1}
\newcommand\q{
     \ifthenelse{\px>0 \AND \py>0}{1}{}
     \ifthenelse{\px<0 \AND \py>0}{2}{}
      \ifthenelse{\px<0 \AND \py<0}{3}{}
     \ifthenelse{\px>0 \AND \py<0}{4}{}
     }
\node at (0,0) {This is a Quadrant \q angle.};
\end{tikzpicture}

\end{document}


(Well, I'm getting better at posting the codes!)

But why does this not work if I use \pgfmathsetmacro{\q}{...}?

Thanks again!

-Dan

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

ifthenelse

Postby rais » Tue Aug 22, 2023 11:12 pm

Hi Dan,
topsquark wrote:But why does this not work if I use \pgfmathsetmacro{\q}{...}?

\ifthenelse isn't fully expandable which may throw off \pgfmathsetmacro's parser of its second argument.
A somewhat more archaic approach (using TeX if.. constructs) works in this case, too:
\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\def\px{-1pt}
\def\py{-1pt}
\pgfmathsetmacro\q{%
  \ifdim\px > 0pt % Q1/Q4 or on x axis
    \ifdim\py > 0pt
      1%
    \else
      \ifdim\py < 0pt
        4%
      \fi
    \fi
  \else
    \ifdim\px < 0pt % Q2/Q3 or on x axis
      \ifdim\py > 0pt
        2%
      \else
        \ifdim\py < 0pt
          3%
        \fi
      \fi
    \fi
  \fi
}
\node at (0,0) {This is a Quadrant \q\ angle.};
\end{tikzpicture}

\end{document}

To make either code `foolproof' you'd need to catch if \px or \py is zero...

KR
Rainer

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

ifthenelse

Postby topsquark » Wed Aug 23, 2023 4:28 am

Thank you so much, both of you. I understand what's been said so far.

I have added to the code a bit. I can state my eventual goal, now that the preliminaries are out of the way. I have just one little problem left.

What I want to do is determine the quadrant of a point, whether given in coordinates or as a coordinate. I need to distinguish if I'm looking at (X) or (\x,\y), respectively. Now, from an earlier question, I've actually learned how to do this. I can create a new pair of commands: point and coords, that will take care of that. What I need to do is structure the command "quadrant" to take the form:
\quadrant{point}{X}{}

or
\quadrant{coords}{3}{2}

I've even set this up already. (See the code below.) (Note: to print out the three possibles, I've added an extra argument to \quadrant to print out a dot on the correct line.)

My problem is this. In order for the command quadrant to use either point or coords, I have to put the "if" statement inside a scope. Now, I can do anything I want inside the scope (I've currently got it printing a red dot if I'm using point and a blue dot if I'm using coords), but I need to define \x and \y depending on which option is being used. But if I'm inside the scope, I lose that information when I end the scope.

To be more precise:
I have a subroutine to use to get the x,y coordinates from X, so if I'm using point I do that.
If I'm using coords, I already have the x,y coordinates and I just set them to be \x = x and \y = y.

But I lose this information once I pass outside the scope, so the coordinates \x and \y are undefined when I try to find the quadrant. (newcommand\q{...}). I need to "hard save" the macro values \x and \y.

I know (somewhere on the net) I've seen a solution to this problem, but I don't recall where or how to do it.

Any suggestions?

Thanks!

-Dan
\documentclass{article}

\usepackage{tikz}

\begin{document}

\newif\ifpoint
\tikzset{
     point/.code={\pointtrue}
}

\newif\ifcoords
\tikzset{
     coords/.code={\coordstrue}
}

\newcommand\quadrant[4]{
     \begin{scope}[#1]
          \ifpoint
               \fill[red] (0,-#4) circle(2pt);
          \fi
          \ifcoords
               \fill[blue] (0,-#4) circle(2pt);
          \fi
     \end{scope}

     \def\x{#2}
     \def\y{#3}

     \pgfmathsetmacro\q{
          \ifdim\x > 0pt
               \ifdim\y > 0pt
                    1
               \else
                    \ifdim\y < 0pt         
                         4
                    \fi
               \fi
          \else
               \ifdim\x < 0pt
                   \ifdim\y > 0pt
                        2
                   \else
                        \ifdim\y < 0pt
                             3
                        \fi
                   \fi
               \fi
          \fi
     }
}

\begin{tikzpicture}
     \quadrant{coords}{3pt}{-2pt}{0}; \node[anchor=west] at (0.5,0) {The point (3,-2) is in Quadrant \q.};
     \quadrant{point}{4pt}{1pt}{1}; \node[anchor=west] at (0.5,-1) {The point (4,1) is in Quadrant \q.};
     \quadrant{coords}{-5pt}{-1pt}{2}; \node[anchor=west] at (0.5,-2) {The point (-5,-1) is in Quadrant \q.};
\end{tikzpicture}

\end{document}

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

ifthenelse

Postby rais » Wed Aug 23, 2023 8:07 pm

Hi Dan,
yes, ending a scope (or any other LaTeX environment) also ends a TeX group after which definitions made in that scope are lost.
\documentclass{article}

\def\A{foo}
\def\B{foo}
\def\C{foo}
\begin{document}
Before some group: \A, \B, \C

\begingroup %to simulate the scope
\def\A{bar}
\gdef\B{bar}
\def\C{baz}% in case some other macro defined \C:
\global\let\C=\C
Within some group: \A, \B, \C
\endgroup

After some group: \A, \B, \C

\end{document}

\gdef is short for \global\def, so the redefinition of \B made within a group is still valid after the group closes.
\global\let\C=\C is a way to, hmm, globalize the definition of \C, so even if you redefined \C within a group using some other macro that may use a non-global definition (like \def), you could still make its (re)definition global this way.

There are other ways, of course :)

KR
Rainer

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

ifthenelse

Postby topsquark » Wed Aug 23, 2023 11:41 pm

Thank you. I have some more tweaking to do. (The \gdef is working for coords, but something is going wrong in points, for some reason. I'm now putting in coordinates of a point given as (\x,\y), and all the code works up to the point I use \gdef. I need to work on that.) I'll post again if I have another question, and I'll post my final product.

-Dan

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

ifthenelse

Postby topsquark » Thu Aug 24, 2023 12:51 am

Nope. We're back. I've done every test I can think of. The only explanation I can come up with is when I use point, the \x and \y are not being passed outside of scope. I am having the same problem if I do any calculation in point and assign the result to \x and \y. (I even checked to make sure that the calculated values had units, they did.) But coords works like a charm.

I hate to post questions step by step like this, but I'm flummoxed. (Again! :) )

I was able to find precious little on the net about \gdef, but what I found suggests that I am doing this right. So far as I know, my code below should work. Clearly, I'm missing something.

Along with this question: I've got Kottwitz's "LaTeX Graphics with Tikz"(excellent!) and, of course, the PGF/TikZ online manual (for what it's worth.) I am trying to expand what I know and \tikzset, for example, has .style, .pic, .code, .initial, .get, and .default options, the only two of which I can find out anything about on the net are .style and .pic. I'm looking for a more comprehensive TikZ guide (much more clearly written than the PGF/TikZ manual) that is still accessible to someone with a weak programming background. (I've got BASIC, FORTRAN, PASCAL, and Apple machine coding in my background, but nothing modern.) I am still learning by looking at examples and trying to figure things out, and I appreciate all the help I've been getting here, but I can't keep coming here hoping to be taught what are apparently basic commands. I need another resource. The net is hopeless and I have no money to spare, but if I'm going to learn this right, I guess I'm going to have to pay. I see the other two guides listed along with the posts. Are they what I might be looking for, or is there something else you might suggest?

Thanks!

-Dan

\documentclass{article}

\usepackage{tikz}
\usepackage{pgf}

\begin{document}

\newif\ifpoint
\tikzset{
     point/.code={\pointtrue}
}

\newif\ifcoords
\tikzset{
     coords/.code={\coordstrue}
}

\newcommand\quadrant[3]{
     \begin{scope}[#1]
          \ifpoint
               \path[transform canvas] (#2);
               \pgfgetlastxy{\X}{\Y}
               \gdef\x{\X}
               \gdef\y{\Y}
          \fi
          
          \ifcoords
               \gdef\x{#2}
               \gdef\y{#3}
          \fi
     \end{scope}

     \pgfmathsetmacro\q{
          \ifdim\x > 0pt
               \ifdim\y > 0pt
                    1
               \else
                    \ifdim\y < 0pt         
                         4
                    \fi
               \fi
          \else
               \ifdim\x < 0pt
                   \ifdim\y > 0pt
                        2
                   \else
                        \ifdim\y < 0pt
                             3
                        \fi
                   \fi
               \fi
          \fi
     }
}

\begin{tikzpicture}
     \quadrant{coords}{3pt}{-2pt}; \node[anchor=west] at (0,0) {The point (\x,\y) is in Quadrant \q.};
     \quadrant{point}{4pt,-1pt}{}; \node[anchor=west] at (0,-1) {The point (\x,\y) is in Quadrant \q.};
     \quadrant{coords}{-5pt}{-1pt}; \node[anchor=west] at (0,-2) {The point (\x,\y) is in Quadrant \q.};
\end{tikzpicture}

\end{document}

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

ifthenelse

Postby topsquark » Thu Aug 24, 2023 2:04 am

I was able to solve this using a completely different system for finding \q if I have a given point or given coordinates. I am happy about that, but I still don't know why the code in the last post didn't work. This uses the same method to define \x and \y.

-Dan

\documentclass{article}

\usepackage{tikz}
\usepackage{pgf}
\usepackage{etoolbox}

\begin{document}

\newcommand\quadrant[2]{
     \ifstrempty{#2}{
          \path[transform canvas] (#1);
          \pgfgetlastxy{\X}{\Y}
          \gdef\x{\X}
          \gdef\y{\Y}
     }
     {
          \gdef\x{#1}
          \gdef\y{#2}
     }

     \pgfmathsetmacro\q{
          \ifdim\x > 0pt
               \ifdim\y > 0pt
                    1
               \else
                    \ifdim\y < 0pt         
                         4
                    \fi
               \fi
          \else
               \ifdim\x < 0pt
                   \ifdim\y > 0pt
                        2
                   \else
                        \ifdim\y < 0pt
                             3
                        \fi
                   \fi
               \fi
          \fi
     }
}

\begin{tikzpicture}
     \coordinate (P) at (4pt,-1pt);
     \quadrant{2pt}{3pt}; \node[anchor=west] at (0,0) {The point (\x,\y) is in Quadrant \q.};
     \quadrant{P}{}; \node[anchor=west] at (0,-1) {The point (\x,\y) is in Quadrant \q.};
     \quadrant{-5pt}{-1pt}; \node[anchor=west] at (0,-2) {The point (\x,\y) is in Quadrant \q.};
\end{tikzpicture}

\end{document}

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

ifthenelse

Postby rais » Thu Aug 24, 2023 9:49 am

Hi Dan,
I'm actually surprised your last code works :D

\gdef\x{\X}

globally defines \x to look up \X, when called--it does not expand \X.
Replace \gdef by \xdef which is short for \global\edef, then \X gets expanded.
\gdef\x{#2}

works as expected, because argument placeholders are always expanded.

\gdef and friends are plain TeX commands, see e.g. TeXbyTopic.

KR
Rainer


Return to “LaTeX Beginner's Guide”

Who is online

Users browsing this forum: No registered users and 1 guest