LaTeX Beginner's Guideifthenelse

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

ifthenelse

Post by topsquark »

[quote="rais"]Hi Dan,
I'm actually surprised your last code works :D

:?

Grrr....

Okay, emotional response over. :)

Thank you for that paper. IT'S AWESOME. Contains a lot more than I want on what I might call "word processing" but there are a number of surprising little tidbits even in there. I did a quick skim and it looks like this is going to be one of the more valuable resources I've got right now. I had thought the if then thing would be an easy to learn thing: apparently there's a lot more going on in the background than I had imagined. And all that time spent on explaining macros... Again, thank you!!

Okay, based on what I've read I have to do a solid once over on the code. It also looks like I might be able to compartmentalize some of the code to "subrountine it up" (as I call it. I have no idea what a more modern terminology for that might be.) I'll have to get the thing working correctly first, then investigate that. Ultimately, my goal here (beyond the overall goal to get down the if then coding) is to create a library-esque command that will take a point and produce quadrant, distance to origin, reference angle, and angle information all in one little command to help out with debugging a diagram. (I just tried to reproduce a mosaic I found tikz.net and I had to go through all sorts of fits and conniptions. It would just make like easier to have a single command line that will give everything, rather than write several lines of code over and over to get different pieces of information.)

I now have a base understanding of "extendable." I'll read that section more carefully and see what I can do with it.

Thanks again!

-Dan

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

ifthenelse

Post by topsquark »

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

Code: Select all

\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.

Code: Select all

\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
Okay, I just wrote a long response and it got dumped when my power glitched!

In short, thank you for the article; it's going to be a God-send to me!

I'll have to look at the code again. I think I understand more about why things weren't working before (though I'm not completely sure why you were so surprised that my current one does. :cry: ) But even a first-order skim through that article has shown me many things that now make a lot more sense. A detailed study is definitely in order!

I only have a base idea about "extensions" but I think I understand what you are saying. I was trying to copy the contents of \X to globally define \x, but if I understand what you are saying when I try to do that its copying the temporary variable \X and turning it into another temporary \x, even though I'm using \gdef. I need to use \xdef to change the (catagory?) to global. That might be gibberish. I'll read the article more carefully.

Thanks again!

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

ifthenelse

Post by rais »

topsquark wrote: (though I'm not completely sure why you were so surprised that my current one does. :cry: )
I was surprised because you used the same \gdef definitions for \x, \y as in the previous code.
What I failed to realize was: said definitions no longer are within a TeX group. In other words: you may have just used \def instead of \gdef in your last code and it still would have worked.
You can write this off as `rais not fully awake yet' :D
topsquark wrote: I only have a base idea about "extensions"
the proper term would be "expansions".
To me, an extension is just .*, that is, if your file is called "foo.tex", its base name would be "foo" its extension would be ".tex", but then, that may just be me...
topsquark wrote: but I think I understand what you are saying. I was trying to copy the contents of \X to globally define \x, but if I understand what you are saying when I try to do that its copying the temporary variable \X and turning it into another temporary \x, even though I'm using \gdef.
Not quite. The definition

Code: Select all

\gdef\x{\X}
does define \x to be global (to escape the current group), but it uses an unexpanded `\X' as replacement text. After the group closes, \X is no longer defined (which \x tries to call on).

Code: Select all

\xdef\x{\X}
on the other hand expands \X for the definition, so if \X contains "1pt", \x will contain "1pt" (and not "\X" as defined by \gdef...)
kaiserkarl13 wrote: Welcome to the wonderful world of TeX macro expansion!
exactly :)
topsquark wrote: (catagory?)
Don't you start with category codes now, that's a whole different chapter (also covered by the aforementioned TeXbyTopic)

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

ifthenelse

Post by topsquark »

Aw! But if I want to mess myself up properly, I need to crowd in many many things I don't understand all at once! :twisted:

(Back in the early 2000s I had a problem with Norton SystemWorks. I finally got a tech on the phone that knew what they were doing and they had me edit the registry, which the program had somehow fouled up. I got really excited at the possibilities of what I could do with the registry...I could have some serious fun with this! Then I remembered that I had had to call someone all the way across the country to get a professional to fix my computer because something had crippled my computer when it did something bad to the registry. I decided there are some things I'm not going to mess with!)

Okay, I've decided it's time to end this project. I've learned quite a bit already and I have that article to study. I'm sure I'll be back with other questions, simple or complex. In the meantime, I rewrote most of this in tikzmath, and I've fixed up all the little issues like what if the point is at the origin.

Just for completeness, here it is. Thanks again, everyone, for all the help!

-Dan

Code: Select all

\documentclass{article}

\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{pgf}
\usetikzlibrary{math}

\begin{document}

\newcommand\PointInfo[2]{
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Takes in a point P as {P}{} or as (x,y) coordinates {x}{y}.               %
%  Coordinates are assumed to be in cm.                                      %
%  Outputs                                                                   %
%       (\Pxcoord,\Pycoord):      Coordinates                                %
%       \Pquadrant:     Quadrant                                             %
%       \Pdisttoorigin: Distance from point to origin                        %
%       \Prefangle:     Reference angle                                      %
%       \Pangle:        Angle measured from +x axis                          %
%  (Pquadrant is set to 0 if P is at the origin.)                            %
%  (Prefangle and Pangle are set to "undefined" if P is at the origin.)      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Determine which input is being used and set (\Pxcoord,\Pycoord) coordinates

     \ifstrempty{#2}{
          \path[transform canvas] (#1);
          \pgfgetlastxy{\xcoord}{\ycoord}
          \pgfmathsetmacro{\Xcoord}{scalar{\xcoord}/28.452756}
          \pgfmathsetmacro{\Ycoord}{scalar{\ycoord}/28.452756}
          \xdef\Pxcoord{\Xcoord}
          \xdef\Pycoord{\Ycoord}
          }
          {
          \gdef\Pxcoord{#1}
          \gdef\Pycoord{#2}
          }

     \tikzmath{
%
%  Calculate the quadrant the point is in
          if and(\Pxcoord==0,\Pycoord==0) then { \Pquadrant = 0; };
          if and(\Pxcoord>0,\Pycoord==0) then { \Pquadrant = 1; };
          if and(\Pxcoord==0,\Pycoord>0) then { \Pquadrant = 2; };
          if and(\Pxcoord<0,\Pycoord==0) then { \Pquadrant = 3; };
          if and(\Pxcoord==0,\Pycoord<0) then { \Pquadrant = 4; };
          if and(notequal(\Pxcoord,0),notequal(\Pycoord,0)) then { \Pquadrant = int(round(5/2-(\Pycoord)/abs(\Pycoord)-1/2*(\Pxcoord*\Pycoord)/(abs(\Pxcoord*\Pycoord))); };
%
%  Calculate the distance from the point to the origin
          \Pdisttoorigin = sqrt((\Pxcoord)^2+(\Pycoord)^2);
%
%  Calcluate the reference angle
          if and(\Pxcoord==0,\Pycoord==0) then { \Prefangle = 999; };
          if and(\Pxcoord==0,\Pycoord>0) then { \Prefangle = 90; };
          if and(\Pxcoord==0,\Pycoord<0) then { \Prefangle = 90; };
          if notequal(\Pxcoord,0) then { \Prefangle = abs(atan(\Pycoord/\Pxcoord)); };
%
%  Calculate the angle from the +x axis
          if and(\Pxcoord==0,\Pycoord==0) then { \Pangle = 999; }
          else { \Pangle = 180*floor(\Pquadrant/2)-(-1)^(\Pquadrant)*\Prefangle); };
     }

%  Set Prefangle and Pangle to undefined if necessary
     \if \Prefangle=999
          \renewcommand\Prefangle{undefined}
     \fi
     \if \Pangle=999
          \renewcommand\Pangle{undefined}
     \fi
}

\begin{tikzpicture}
    \node at (0,0) {Points on a hexagon.};
     \foreach \s in {0,...,5} {
          \coordinate (P\s) at (\s*60:4);
          \PointInfo{P\s}{};
          \node at (0,-\s*0.6-0.6) {P is in Q\Pquadrant. The polar coordinates are (\Pdisttoorigin,\Pangle).};
     };
\end{tikzpicture}

\end{document}
Post Reply