[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to compare two macros in TeX?



On 20 Nov 2002 01:44:33 +0100
martin f krafft <madduck@debian.org> wrote:

> 
> also sprach Rupert <rupert.debian@hotpop.com> [2002.11.19.1932 +0100]:
> > This compares the complete expansions of whatever you throw at it:
> 
> nice. i didn't even think that far...
> 
> > \def\same#1#2{00\fi%
> >   \edef\tmpone{#1}%
> >   \edef\tmptwo{#2}%
> >   \ifx\tmpone\tmptwo}  =20
> >=20
> > \def\variable{somevalue}
> >=20
> > \if\same{\variable}{somevalue}
> > same\else diff\fi
> 
> given that \ifx is part of the defintion, you don't need that \if,
> right? then again i don't understand the 00\fi notation...

The 00\fi is an evil trick designed precisely to allow you to use the
\if, as I like using \if..\else..\fi rather than weird four-parameter
macros that I'm going to forget the meaning of :) I got it from Donald
Arseneau:

  http://groups.google.com/groups?selm=yfi1ymgsmzj.fsf%40triumf.ca

> anyway, i think i'll just do this:
> 
> \def\same#1#2#3#4{
>   \edef\tmpone{#1}%
>   \edef\tmptwo{#2}%
>   \ifx\tmpone\tmptwo}#3\else#4\fi}

ok.

> what's \edef compared to \def?

\def\a{\b}  : \a expands to \b 

\edef\a{\b} : \a expands to what you get when you expand \b as much as
	      possible at the time of the \def

eg

\def\a{\b}
\def\b{B}
\def\d{\a} 
\edef\e{\a}
\def\b{C}
\d\e

yields "CB".

> and why do TeX people put % at the end of the line so frequently?

to catch unwanted implicit spaces, e.g. you have an unwanted space
from the first newline in your definition of \same above; it should be

 \def\same#1#2#3#4{%
   \edef\tmpone{#1}%
   \edef\tmptwo{#2}%
   \ifx\tmpone\tmptwo #3\else#4\fi}

Possibly the two % you did put in are not necessary, but the first one
certainly is. If in doubt (ie nearly always) I put them in.

Rupert



Reply to: