6

This is perhaps a basic question, but I am quite confused now.

How should I expand a box register to a character?

For example, after \newbox\foo, suppose \number\foo equals 26, how should I compare \foo as a character to another character, like \if\foo b?

I am trying to test the equality of \foo after a \newbox command with the character it represents, just to see if I know how to work with this, but if I type (assume \number\foo gives 26),

\newbox\foo
\if\foo\char26 yes\else no\fi

I get 26 yes.

So I try to expand as follows.

\expandafter\expandafter\expandafter%
\if\expandafter\expandafter\foo\char26 yes\else no\fi

Then I still get 26 yes, as (this took me some time to figure out) \char happens at the final stage of processing (the execution processor, to quote "TeX by Topic").

Further, I think maybe using \uccode can solve the problem, so I tried:

\uccode\lq b=26
\uppercase{\if\foo b yes\else no\fi}

Then I get NO æ. Maybe I have to somehow extract the character from \foo?

And I suddenly realize that \foo is equivalent with \char26.


Sorry if this question appears messy; I am not very clear about the macro structures of TeX yet. Any help or reference will be sincerely appreciated, thanks in advance.

  • 1
    \ifnum\number\foo=26 yes\else no\fi? – Manuel Sep 27 '16 at 8:24
  • 'You can't': boxes contain typesetting instructions, not characters in a sense that TeX will allow a comparison. (Yes, often the content is largely made up of character slots that look much like text, but that's not the same thing.) Can you specify what you are actually trying to achieve in a wider sense? – Joseph Wright Sep 27 '16 at 8:24
  • @Manuel Thanks a lot! Why I forgot I can test equality on numbers! If you post an answer, I shall accept that. :) – awllower Sep 27 '16 at 8:27
  • @JosephWright Thanks for the information. I just found that after a \newbox command, it can be used as a number, so I thought maybe I can treat it as a character with that character code, but it turns out that, with \char I don't know how. :( – awllower Sep 27 '16 at 8:29
  • How can this be useful? The number that's allocated to the new box register can change as soon as you load another package in your preamble. You should never use the allocated number for any reason whatsoever, because it's essentially random. – egreg Sep 27 '16 at 8:43
4

I'm not sure what are you exactly trying to achieve, and this seems weird, but you can test with \ifnum

\ifnum\number\foo=26 yes\else no\fi
| improve this answer | |
  • May be you can just write \ifnum\foo=26 yes\else no\fi but I haven't tested. – Manuel Sep 27 '16 at 8:29
  • Just tested; it works, thanks. :D Also I explain a little in the reply to Joseph Wright. :) – awllower Sep 27 '16 at 8:30
  • Doesn't \char\number\foo work? Or \char\foo? Still, I don't get why would you try to do that, you can may be explain a real use case in your question? – Manuel Sep 27 '16 at 8:31
  • I think \foo just outputs that character already? Eh, I don't have any specific use in mind, just trying to know how the conditionals work in different cases, or how they interact with other commands, and familiarizing myself with the tex commands. – awllower Sep 27 '16 at 8:34
  • You are supposed to use \box\foo or \copy\foo to get what's in the box. The fact that \foo outputs a character is just a side effect. In any case, it doesn't expand to that character, it ouputs it. So you can't use \foo in \if or \ifx. May be with LuaTeX or XeLaTeX you can do \Uchar\foo and it will actually expand to the character. – Manuel Sep 27 '16 at 8:37
8

This is like defining a global variable in a high level programming language and trying to use the memory register address of that variable in the same program. This will obviously not work.

Consider the simple example (in plain TeX)

%\input manmac
\newbox\foo

\foo
\bye

This will print a dotless i, because it turns out that the box register allocated is number 16 and the cmr10 font has that character in slot 16.

Now uncomment the first line and you will get a breve accent, because it turns out that the box register allocated is number 20. Why is that? Simple: manmac.tex contains four \newbox declarations.

You clearly see that you can't foresee what number the box register will correspond to, but this is of no consequence for your programs, provided you never refer to the number directly, but only with its symbolic name \foo.

The fact that \newbox\foo internally does \chardef\foo=<number> should never be exploited in programs. It's just implementation and uses the property that a \chardef token can be used in the context of a number.

Using \foo in any other context than for boxes is wrong: your output will be unpredictable, as the previous example shows.


Let's turn to a different point of view. Suppose you do

\chardef\foo=26

This is a perfectly legal instruction and plain.tex has lots of similar ones, for instance

\chardef\#=`#

(where `# refers to the ASCII code of the # character). Now \foo can be used to print the glyph occupying slot 26 in the current font. If the current font is cmr10, you get ‘æ’; if the current font is ecrm1000, you get ‘ȷ’ (dotless j).

You see that \foo does not refer to a character (considered as an abstract entity), but just to a slot: it's simply an instruction

print the glyph sitting at slot 26 in the current font

There are some conventions, of course; for instance slot 98 will contain a ‘b’ in most font; but if the current font is tcrm1000 you would get ‘ ⁀’ (a tie accent).

Even \# would have quite unpredictable results if used with some fonts: with wasy10 you get ‘○’ (a white circle).

Conclusion: comparing a \chardef token with a character makes no sense.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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