Problem with \relax

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!

• Look e.g. the end of this answer, or the quote from the TeX book of this one. Whatever comes between \csname and \endcsname should expand into characters. – campa Aug 18 '16 at 11:22
• Thanks a lot @campa, that was helpful. So is there any alternative for \relax in this situation? – Abdollah Aug 18 '16 at 11:38

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

• Thanks @David Carlisle. It seems that I must use first version of \romanNumber. – Abdollah Aug 18 '16 at 12:03