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'm using pgf and enumitem to define a hexadecimal enumeration scheme as follows:



  \expandafter\@Hex\csname c@#1\endcsname


% Why does cutting this line cause it to fail?



\item First
\item Second\addtocounter{enumi}{10}
\item Letters
\item More
\item Another
\item Ten!

Now if I remove the highlighted line, the compilation fails and I can't understand why. It tells me:

! Undefined control sequence.
<argument> \The@Hex 

I'm trying to understand why it needs this macro to already be defined for the enumeration to work, especially since the value of \The@Hex isn't actually used until it is redefined by the use of enumerate...

share|improve this question
Smells after an \edef context where the \The@Hex in \pgfmathdectoBase\The@Hex{.. is expanded. A typical fragility issue. It works when you use \DeclareRobustCommand to define \Hex. The question is: why does it work anyway then? –  Martin Scharrer Jul 3 '11 at 22:13
@Martin: Never use \DeclareRobustCommand for things to which you would apply \label and \ref. –  egreg Jul 3 '11 at 22:26
@egreg: you are totally right; I agree. However, Seamus code is fragile and therefore doesn't work with these macros anyway. –  Martin Scharrer Jul 3 '11 at 22:28

2 Answers 2

up vote 5 down vote accepted

There's a way that avoids assignments, making conversion to hexadecimal completely expandable:

\newcommand*{\Hex}[1]{\expandafter\hex\csname c@#1\endcsname}

binhex.tex is by David Kastrup.

As partly explained by Martin, one problem with \pgfmathdectoBase that performs assignments and so it's unusable in defining number representations for \ref, because the system has to use \edef (in the form of \protected@edef) in order to write in the aux file the actual number and not a command. With your definition, assigning \label to an item and trying \ref with it would result in an error.

Since an enumerate is probably not too long, one can use

\newcommand{\Hex}[1]{\expandafter\@Hex\csname c@#1\endcsname}
\newcommand{\@Hex[1]{\ifnum#1>15 \@ctrerr\else\hexnumber@{#1}\fi}

maybe extending the definition of \hexnumber@ to cover more cases.

share|improve this answer
Nice. I was just going to point out that LaTeX provides \def\hexnumber@#1{\ifcase\number#1 0\or 1\or 2\or 3\or 4\or 5\or 6\or 7\or 8\or 9\or A\or B\or C\or D\or E\or F\fi} which could also be expanded to number larger than 15. –  Martin Scharrer Jul 3 '11 at 22:32
I was wondering about this. Loading pgf seems rather excessive just to get hex numbers... –  Seamus Jul 4 '11 at 9:44
fmtcount also provides hex as well as binary, octal and a bunch of other formats... –  Seamus Jul 14 '11 at 20:42
Always non expandable, though. –  egreg Jul 14 '11 at 20:48

The error is caused because the label code is supposed to by fully expandable. Your one is actually fragile which causes the \The@Hex in \pgfmathdectoBase\The@Hex to be expanded before it got assigned (there is a \protected@edef used to set the \@currentlabel in \refstepcounter.). The solution for this is to change your definition of the macro. As egreg pointed out making the macro robust would avoid that error but cause issues when the enumeration item is \labeled. Note that LaTeX provides the macro \hexnumber@ which can be used for the number 0-15 (See egreg's answer for are bigger range).

share|improve this answer
Bigger range indeed! \hex manages numbers up to 2147483647, which is the greatest legal number in TeX. binhex.tex manages also bases 2, 4 and 8. –  egreg Jul 3 '11 at 23:18
@egreg: Wow, imagine an enumerate with 2147483647 items! ;-) (I just posted the answer to explicitly point out the source of the trouble; which seemed to be the main reason why Seamus asked the question) –  Martin Scharrer Jul 3 '11 at 23:28

Your Answer


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.