Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
share|improve this question
add comment

2 Answers

up vote 17 down vote accepted

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
share|improve this answer
    
Thanks David. Is there any way around using LuaTeX? –  Jubobs Feb 11 at 11:12
    
@Jubobs do you mean a way using luatex or a way to avoid using luatex (the answer is yes either way) –  David Carlisle Feb 11 at 11:13
    
I mean a way to avoid using luatex. And what would be the way? :) –  Jubobs Feb 11 at 11:14
    
@Jubobs updated –  David Carlisle Feb 11 at 11:17
    
Great stuff! Thanks again. I'll remember to use \show next time... –  Jubobs Feb 11 at 11:19
add comment
\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
share|improve this answer
    
Thanks, Herbert. I guess I should go back to the first few chapters of the TeXbook... –  Jubobs Feb 11 at 11:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.