4

I'd like to have access to the number associated to a box by giving its control sequence. This control sequence is defined by \newbox using \chardef so a "control sequence box" expands to something like \char"1A. I'd like to get that hexadecimal 1A. Here is what I did so far.

\documentclass{article}

\begin{document}
\newbox\mybox

\edef\temp{%
  \def\noexpand\stripchar\detokenize{\char"}##1\noexpand\endstripchar{##1}}
\temp

\def\printboxnumber#1{%
  \expandafter\stripchar\meaning#1\endstripchar}

\printboxnumber\mybox
\end{document}

The \detokenize is there to take care of \catcode change due to the use of \meaning in \printboxnumber. Unfortunately, I get

! Use of \stripchar doesn't match its definition.
<inserted text> \char"
                      1A
l.13 \printboxnumber\mybox

What's wrong?

cjorssen
  • 10,032
  • 4
  • 36
  • 126

1 Answers1

4

\detokenize{\char"} results in \char " (with a space after the control sequence name); you need \string:

\begingroup\edef\temp{\endgroup
  \def\noexpand\stripchar\string\char\string"##1\noexpand\endstripchar{##1}}
\temp

The second \string is in case babel is used. The \begingroup-\endgroup is to avoid leaving \temp defined.

The decimal representation can be obtained simply by \number\mybox.

egreg
  • 1,121,712
  • \number\mybox... Good exercise anyway :-) Thanks for pointing out the \detokenize subtlety. – cjorssen Oct 27 '11 at 13:37
  • Every \chardef or \mathchardef token can be used when TeX is looking for a . For example, the constant \@one, that is often used in packages, is defined as \chardef\@ne=1. – egreg Oct 27 '11 at 17:29