advance a counter from a macro

  • Permalink
  • submit to reddit
  • Email
  • Follow


This is so basic I'm ashamed to ask, but I know people
here can answer in no time, so I'll shut up my ego and ask.
Consider

\def\ADV{\global\advance\lll by 1}

\lll=-1
\ADV
\ifnum\lll=0 ZZZ \else PPP \fi

\lll=-1
\showthe\lll
\ADV
\ifnum\lll=0 ZZZ \else PPP \fi

\lll=-1
\ADV
\showthe\lll
\ifnum\lll=0 ZZZ \else PPP \fi

\bye

I expected "ZZZ  ZZZ  ZZZ" but I get  "PPP PPP ZZZ"
Why??

Piero
0
Reply pierodancona (12) 9/24/2008 7:52:04 PM

See related articles to this posting

pierodancona@gmail.com a =E9crit :
> This is so basic I'm ashamed to ask, but I know people
> here can answer in no time, so I'll shut up my ego and ask.
> Consider
>
> \def\ADV{\global\advance\lll by 1}
>
> \lll=3D-1
> \ADV
> \ifnum\lll=3D0 ZZZ \else PPP \fi
>
> \lll=3D-1
> \showthe\lll
> \ADV
> \ifnum\lll=3D0 ZZZ \else PPP \fi
>
> \lll=3D-1
> \ADV
> \showthe\lll
> \ifnum\lll=3D0 ZZZ \else PPP \fi
>
> \bye
>
> I expected "ZZZ  ZZZ  ZZZ" but I get  "PPP PPP ZZZ"
> Why??
>  =20

A \relax is wiser after a number to avoid unexpected expansion of \ifnum =
:

\def\ADV{\global\advance\lll by 1\relax}

0
Reply admin_c (13) 9/24/2008 8:05:56 PM

> > Why??
>
> A \relax is wiser after a number to avoid unexpected expansion of \ifnum :
>

Yes!
Thank you. I can go to sleep now

Piero

0
Reply pierodancona (12) 9/24/2008 9:08:25 PM

<pierodancona@gmail.com> wrote:

> This is so basic I'm ashamed to ask, but I know people
> here can answer in no time, so I'll shut up my ego and ask.
> Consider
> 
> \def\ADV{\global\advance\lll by 1}
> 
> \lll=-1
> \ADV
> \ifnum\lll=0 ZZZ \else PPP \fi
> 
> \lll=-1
> \showthe\lll
> \ADV
> \ifnum\lll=0 ZZZ \else PPP \fi
> 
> \lll=-1
> \ADV
> \showthe\lll
> \ifnum\lll=0 ZZZ \else PPP \fi
> 
> \bye
> 
> I expected "ZZZ  ZZZ  ZZZ" but I get  "PPP PPP ZZZ"
> Why??

Read closely your input. It's equivalent to

\lll=-1 \ADV\ifnum\lll=0 ZZZ \else PPP \fi

and, after the initial assignment, it expands to

\global\advance\lll by 1\ifnum\lll=0 ZZZ \else PPP \fi

Now TeX expands tokens after the 1, as explained in the TeXbook;
therefore the conditional is evaluated when the value of \lll is
still -1.

A proper way to define \ADV is

\def\ADV{\global\advance\lll by 1 }

or

\def\ADV{\global\advance\lll by \@ne}

(in a context where @ has category code 11). In the first case
the space will be ignored, in the second case the number specification
ends at the token \@ne which is not expandable (Plain TeX and LaTeX
define it by "\chardef\@ne=1").

In the second case the "\showthe\lll" is irrelevant, but in the third
one it stops the search for a number specification, since \showthe does 
not expand to a <number>; therefore the assignment \lll=0 is performed
and the conditional is evaluated with the intended value.

Ciao
Enrico
0
Reply gregorio (1366) 9/24/2008 9:19:18 PM
comp.text.tex 36139 articles. 68 followers. Post

3 Replies
52 Views

Similar Articles

[PageSpeed] 46

  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Re: Problem with macro logical condition : passing a counter in the macro
Paul explains well the issues, and these are things one must understand in order to use the macro facility with confidence. However, you might find yourself moving in the direction of a quick fix by simply removing the percent signs in your Condition 1 and Condition 2 blocks. On Thu, 8 Jul 2004 01:33:20 -0400, Paul M. Dorfman <sashole@BELLSOUTH.NET> wrote: >Dominic, > >You are trying to use a macro assembling Data step code based on the value >of a Data step variable before the Data step has run. Note that the entire >activity of your macro compilation ends before the S...

Problem with macro logical condition : passing a counter in the macro from the data step
Hi, I have that little piece of code as an example (see below). In the macro 'ma_create' I want to use the variable count that is created in the data step 'test'. Basically count in that exemple is set to _n_. I know that count is seen correctly in the macro because I generate the variable check is has the right value. Still logical operation that requires the value of count fail. In execution of the macro, count is always seen as greater than the macro variable days which is set in that example to 5. Why does the code only excute condition 1 (count is always se...

Advanced Macro Class
You have been complaining about this class even before you took it. Lets work on these awful examples, so the "Ivory Billed Woodpeckers" can see them and improve this course. We can start with the example you refer to. What macro technique was being demonstrated? What would be a better example? When we beat this one to death we can move on to other irritating examples. On 9/5/08, Toby Dunn <tobydunn@hotmail.com> wrote: > Cherish, > > This is one of those examples I saw and hated in the Advanced Macro class > I attend last week. No macro is needed at all. Idea...

Advanced usage of macros
I have to create a lot of similar tables that only differs by type and name of each column (ok, I guess that _is_ the difference between tables). I create these tables by creating a class for a row and then letting a std::vector hold objects of these classes. As I have to write quite a lot I would like to use macros for this. Below I have shown how most of it works. I just cannot figure out a good way of keeping the name of each column. I'm hoping somebody have a great idea on how to accomplish this :-) --- table_row.cc --- #include <iostream> #include <string> #defin...

More advanced forms of macros?
Have there ever been any experiments with something like: a. "semi-colon eating" macros #define foo(a, b;) case ((a)+(b)): foo(a, b); becomes case ((a)+(b)): b. "block taking" macros: #define foo(a, b, {c}) switch (a) \ { case (b): { c } break; default: blah; } foo(a, b) { outer1; { inner; } outer2; } becomes: switch (a) { case (b): { outer1; { inner; } outer2; } break; default: blah; } Maybe somebody thought of something like this before but they decide...

NaturallySpeaking advanced scripting macros
In this thread, I will provide some potentially useful Naturally Speaking advanced scripting macros for Windows. You must have Dragon NaturallySpeaking 8 Professional (or maybe prior Professional versions). My methods are subject to change as I learn the specific scripting capabilities of the speech recognition program. I will try to post any corrections/improvements to an already posted macro under that macro's original subthread. If a macro will not work on your system, please advise and I will be happy to reformulate/rewrite or offer assistance to get the thing working on your...

Macro Variable Counter Resolving to ** ?
Hi, I wrote a macro that creates a dataset, and need to make the number of observations in that dataset available a a variable inside the macro: %macro PValueLoop; .... /*Create dataset to be analysed. */ data CorrelationData; set Head2SHT1; where ...; run; /* Determine # of observations in dataset. */ data _null_; set CorrelationData end=eof; o+1; oo=left(put(o,2.)); if eof then call symput ('Total',oo); run; .... /* Format and relabel correlation results. */ data FormatResults; set CorrResults; NumObs=&&Total; run; %mend PValueLoop; Why do I get ** as the output? I tri...

Advanced Macros && Preprocessors
Hi all, I'm developing a small defensive programming toolkit for my own projects. So, here are my questions. 1) Is it possible to move from something like this: SAFECALL(foo();) to __safecall foo(); 2) I need to auto-add code to every method of particular class safeclass MyClass { public: void foo(); } void MyClass::foo() {} will modify the foo implementation - add some lines of code on the end of the method 3) even more advanced stuff, is it possible to modify the preprocessor (or add my own preprocessor)? I need to parse something like this. __precheck minutes >= 0 &&am...

Macro counter exceeding its upper bound
One more minute of SAS-L brain time please . . . Can anyone suggest why the "logmo" counter is exceeding its specified bound (12). I'm seeing it go to 13. Many thanks, Michael Murff Provo, UT ***************************************************************; options mlogic mprint symbolgen; %macro bigloop; %do yr=86 %to 86; %let year = %eval(&yr+4); data _null_; /*** Create log variables based on mcap ratios ***/ /*** 12 total for current year ***/ %do logmo= 1 %to 12; ...