2

I read a lot of stuff about \relax for 2 days but can't handle the following problem.

Why \romanNumber has no problem but \romanNumberX does not work? \relax must stop \romannumeral and not \csname. Please correct my vision about TeX.

\documentclass{article}

\newcommand{\romanNumber}[1]{\romannumeral #1}
\newcommand{\romanNumberX}[1]{\romannumeral #1\relax}
\begin{document}

\expandafter\def\csname name\romanNumber{2}\endcsname{Must do!\\}
\nameii%OK, no problem here

%The following line generate error "missing \endcsname"
\expandafter\def\csname nameX\romanNumberX{2}\endcsname{Also must do!\\}%
\nameXii%Does not work, :(

\end{document}

Thanks a lot!

campa
  • 31,130

1 Answers1

4
\newcommand{\romanNumber}[1]{\romannumeral #1}

\romanumeral will parse ahead consuming any valid part of a <number> so

 \romanNumber{1}1 

would convert 11 producing xi not convert 1 so expanding to i1

The parsing stops at any non expandable token that is not allowed in a <number> so you could use \relax but then

   \newcommand{\romanNumber}[1]{\romannumeral #1\relax}

 \romanNumber{1}1 

expands to i\relax 1 which isn't allowed in csname.

If the non expandable token that stops the number is a space it is not replaced so you could do

   \newcommand{\romanNumber}[1]{\romannumeral #1 }

then

 \romanNumber{1}1 

expands to i1

But \newcount\foo \foo=1

 \romanNumber{\foo}1

expands to i 1 as \foo terminates the number itself so does not need the space so the space is not consumed.

using e-tex you can use

\newcommand{\romanNumber}[1]{\romannumeral\numexpr#1\relax}
David Carlisle
  • 757,742