18

This should be easy, but I don't understand why the test below fails... I expected Hurrah! to be printed when the \currentchar count reaches 90 (i.e. the character code of Z).

There must be some expansion problem somewhere, but I believe I use \expandafter as is necessary, here. What am I doing wrong? Should I take a different approach?

enter image description here

\def\mysteryletter{Z}

% loop through A-Z to find out the mystery letter
\newcount\currentchar
\currentchar=65 % <-- charcode of A
\loop
  \edef\temp{\char\currentchar}
  \temp
  \expandafter\expandafter\expandafter\ifx\expandafter\mysteryletter\temp Hurrah!\fi
  \advance \currentchar by 1
  \unless\ifnum \currentchar>90
\repeat

\bye

2 Answers 2

19

add

> \show\temp

and you will see that the \edef has made no difference, neither \char nor \currentchar are expandable, so \temp consists of those two tokens every time, and never is \ifx equal to a character token.

luatex has an expandable \Uchar primitive.

In this case it is easier to test the character code rather than the token.

\def\mysteryletter{Z}

% loop through A-Z to find out the mystery letter
\newcount\currentchar
\currentchar=65 % <-- charcode of A
\loop
  \char\currentchar
  \expandafter\ifnum\expandafter`\mysteryletter=\currentchar Hurrah!\fi
  \advance \currentchar by 1
  \unless\ifnum \currentchar>90
\repeat

\bye
5
  • Thanks David. Is there any way around using LuaTeX?
    – jub0bs
    Feb 11, 2014 at 11:12
  • @Jubobs do you mean a way using luatex or a way to avoid using luatex (the answer is yes either way) Feb 11, 2014 at 11:13
  • I mean a way to avoid using luatex. And what would be the way? :)
    – jub0bs
    Feb 11, 2014 at 11:14
  • @Jubobs updated Feb 11, 2014 at 11:17
  • 1
    Great stuff! Thanks again. I'll remember to use \show next time...
    – jub0bs
    Feb 11, 2014 at 11:19
13
\chardef\mysteryletter=`Z
% loop through A-Z to find out the mystery letter
\newcount\currentchar

\currentchar=`A
\loop
  \chardef\temp=\the\currentchar
  \temp
  \ifx\mysteryletter\temp Hurrah!\fi
  \advance \currentchar by 1
  \unless\ifnum \currentchar>90
\repeat

\bye
1
  • Thanks, Herbert. I guess I should go back to the first few chapters of the TeXbook...
    – jub0bs
    Feb 11, 2014 at 11:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .