[comp.text.tex] simple tex/latex question/puzzler/quiz

rom@xor.Eng.Sun.COM (Circuit Group) (12/10/90)

I'm stuck; any help is welcome.
-------------------------------------------
\documentstyle{article}
\begin{document}
\def\macro1{apple}
\def\macro2{\macro1}
\ifx \macro1\macro2 truetext \else falsetext \fi
macro1\macro1 end
macro2\macro2 end
some text here.
\end{document}
--------------------------------------------
produces:
falsetext
macro1appleend macro2appleend
some text here.
-------------------------------------------

It appears that \ifx is not expanding the macro \macro1, does this
make sense?  How can I get an if statement that expands macros until
its just text? Am I doing this the wrong way?

I'm trying to write a .sty file and so I don't think that I can use
\ifthen.  ANY HELP WILL BE APPRECIATED.

BTW are there any books other than the ones by Knuth and Lamport that
explain this stuff; I have both books and find the one by Knuth to be
cryptic, wereas the one by Lamport does not give enough information.

Thanks.
-Ram
--
-Rom
rom@xor.sun.com

eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) (12/10/90)

rom@xor.Eng.Sun.COM (Circuit Group) writes:


>I'm stuck; any help is welcome.
>-------------------------------------------
>\documentstyle{article}
>\begin{document}
>\def\macro1{apple}

This defines a macro \macro, which has to be followed by '1'.

>\def\macro2{\macro1}

This redefines \macro, now to be followed by '2', and if you
don't you get an error message. 

>\ifx \macro1\macro2 truetext \else falsetext \fi

Here \macro is matched to '1', which is a mismatch. 
The test \ifx never expands macros, it only checks if the
top-level expansion is the same.
Example:
\def\a{\b}
\def\c{\d}
\def\b{x} \def\d{x}
makes \b and \d equal for \ifx, but not \a and \b or \a and \d.

>macro1\macro1 end

This I don't believe. When I do this I get:
! Use of \macro doesn't match its definition.
<*> macro1\macro1
                  end
so the information you gave wasn't quit correct.

>macro2\macro2 end

This one is nice. You call \macro followed by '2' the way
it should be, but it expands to \macro followed by '1' which
is an error. See for yourself:
! Use of \macro doesn't match its definition.
\macro 2->\macro 1

<*> macro2\macro2
                  end

Now the whole point is that macro nams can contain only
letters. Not digits or punctuation, unless you change
the category codes of these.

>It appears that \ifx is not expanding the macro \macro1, does this
>make sense?  How can I get an if statement that expands macros until
>its just text? Am I doing this the wrong way?

Try this:
\def\ifREALLYequal#1#2{\edef\testa{#1}\edef\testb{#2}\ifx\testa\testb}

Here the \edef's expand your arguments down to the bottom,
and then you can simply test them.

>BTW are there any books other than the ones by Knuth and Lamport that
>explain this stuff; I have both books and find the one by Knuth to be
>cryptic, wereas the one by Lamport does not give enough information.

There are some books. Probably 'TeX for the impatient' would
be a nice book for you; it explains a number of concepts of
TeX quit well. Can't remember if the treatment of conditionals
is especially good. (authors: Abrahams, Hargreaves and Berry).
Maybe 'Introduction to TeX' by Schwarz. (both Addison-Wesley)
The book by Stephan von Bechtolsheim (you out there Stephan?)
has been announced for a while, but I haven't seen it yet.

Victor.

hanche@imf.unit.no (Harald Hanche-Olsen) (12/11/90)

In article <4221@exodus.Eng.Sun.COM> rom@xor.Eng.Sun.COM (Circuit Group) writes:

   I'm stuck; any help is welcome.
   -------------------------------------------
   \documentstyle{article}
   \begin{document}
   \def\macro1{apple}
   \def\macro2{\macro1}
   \ifx \macro1\macro2 truetext \else falsetext \fi
   macro1\macro1 end
   macro2\macro2 end
   some text here.
   \end{document}
   --------------------------------------------
   produces:
   falsetext
   macro1appleend macro2appleend
   some text here.
   -------------------------------------------

When I run this through LaTeX, I get

! Use of \macro doesn't match its definition.
l.6 macro1\macro1
                  end
? 

because Macro names are made up of letters only, no digits allowed.
So you first defined \macro in such a manner that it must always be
immediately followed by `1' when used, and then so it must be
immediately follwed by `2'.  Your \ifx line compared the meaning of
\macro with that of 1, and found them different.  Your usage of
\macro1 on line 6 is an error.

Now that that is out of the way, lets assume you had named your macros
\macroa and \macrob.  Now your example essentially does what you
described.

   It appears that \ifx is not expanding the macro \macro1, does this
   make sense?

Yes, \ifx only compares the immediate meaning of tokens, and for
macros that means comparing the immediate replacement text (and the
garbage, if any, conatining a description of parameters and text to be
matched).

   How can I get an if statement that expands macros until
   its just text? Am I doing this the wrong way?

\edef\1{\macroa}
\edef\2{\macrob}
\ifx \1\2 truetext \else falsetext \fi

will work.  Exercise 1:  Look up \edef in the TeXbook and figure out
why this works.  Exercise 2:  Why is it ok to have macros named \1 and
\2 when I just expalined above that macro names consist of letters
only?  The answer is in the TeXbook.

- Harald Hanche-Olsen <hanche@imf.unit.no>
  Division of Mathematical Sciences
  The Norwegian Institute of Technology
  N-7034 Trondheim, NORWAY