[comp.text.tex] TeX and marks

mdeck@sybil.cs.Buffalo.EDU (Mary Deck) (10/20/90)

Hi, all.  I'm trying to help a professor TeX a book he's writing.
It's chock fullof exercises.  He wants to put the exercise numbers in
the odd-numbered pages' headlines.  Ok.  So I \mark{\the\exrno} (where
\exrno is a count register) in the exercise macro.  No problem.

If \botmark is the same as \topmark, there are no exercises on the
given (odd) page; he wants "Conventions" in the headline of such
pages.  If \firstmark is the same as \botmark, there is one exercise
on the page; "Problem x" should appear in the headline.  Otherwise,
there are more than one exercises on the page; "Problems y--z" should
be in the headline.  So, in the headline macro, I said

	\if\topmark\botmark{Conventions}
	\else\if\firsmark\botmark{Problem \firstmark}
	     \else{Problems \firstmark--\botmark}
	\fi\fi

But, since that didn't work, I tried writing some stuff to a file to
see what TeX was doing.  It *really* surprised me to find out that
\if\topmark\topmark, \if\firstmark\firstmark, and \if\botmark\botmark
are rarely true.  (What?!?!  Something doesn't equal itself?!?!)  I
wrote to the junk file (with \write) the values of \topmark,
\firstmark, and \botmark.  Even when they printed out the same, they
did not test the same. ???

What I would like to know is:
1)  WHY are those \if's not always true?
2)  How can I compare marks?

I am by no means a TeX wiz, but my boss (who's about the closest thing
we have around here) can't figure this out, either, so I don't feel so
bad. :) 

Loads of thanks in advance....

					....Mary
---
Mary M. Deck,  Student Assistant,  User Services
University Computing Services, State University of New York at Buffalo
mdeck@cs.buffalo.edu [Unix]
acsgmmd@ubvms.cc.buffalo.edu [VAX/VMS]

marcel@cs.caltech.edu (Marcel van der Goot) (10/22/90)

In  <41700@eerie.acsu.Buffalo.EDU> Mary Deck (mdeck@sybil.cs.Buffalo.EDU)
writes
> ... It *really* surprised me to find out that \if\topmark\topmark,
> \if\firstmark\firstmark, and \if\botmark\botmark are rarely true.
> (What?!?!  Something doesn't equal itself?!?!) ...

The answer is actually simpler than the question suggests, no trickery
with marks is involved. As an experiment, try to run the following
through TeX:

    \def\a{xxyz}
    \def\b{pqr}
    \message{*\if\a\b\else false\fi*}

You'll see that the message that is printed is "*yzpqr*", although
certainly \a and \b are different. The reason is that \if expands
tokens until two unexpandable tokens are found. In this case, \a is
expanded which gives the two unexpandable tokens "x" and "x", which
compare equal. The <true text> of the \if is then "yz\b" (TeX book,
p. 209). \topmark etc. expand just like macros (p. 213), so if you
use \if to compare them you are actually comparing something entirely
different.

To test things without expanding them, you usually use \ifx instead
of \if. That would work for the above example with \a and \b, but
not with \botmark etc., because the latter are primitives, not macros
(hence, \ifx\firstmark\botmark is always false, regardless of the marks).
The solution is to first assign the expansion to a macro, using \edef:

    \edef\first{\firstmark}
    \edef\bot{\botmark}
    \ifx\first\bot
        % \firstmark and \botmark have equal values
    \else
        % \firstmark and \botmark have different values
    \fi


					   Marcel van der Goot
					   marcel@vlsi.cs.caltech.edu

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

mdeck@sybil.cs.Buffalo.EDU (Mary Deck) writes:

[ introduction deleted]

>	\if\topmark\botmark{Conventions}
>	\else\if\firsmark\botmark{Problem \firstmark}
>	     \else{Problems \firstmark--\botmark}
>	\fi\fi

>What I would like to know is:
>1)  WHY are those \if's not always true?
>2)  How can I compare marks?
About \if: this is a test for equality of *character codes*.
Therefore if there is something that is not a character,
it is expanded until after \if there are two things
that are either a charcter, or an unexpandable control 
sequences. All control sequences are the same for this test,
but they differ from all characters.

Now suppose \topmark is {ab}. Then \if\topmark\topmark
expands to \if ab\topmark. 'a' and 'b' are compared, are
unequal, and therefore...

The test you want is \ifx. I'm not quite sure what the 'x'
stands for, but I interpret it as `extensive equality'.
Quite likely it does exactly what you need.

>Loads of thanks in advance....

You're welcome.

>					....Mary

Victor.