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.

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?

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

\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.

share|improve this answer
    
\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 <number>. For example, the constant \@one, that is often used in packages, is defined as \chardef\@ne=1. –  egreg Oct 27 '11 at 17:29
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.