How to convert Roman into Arabic numeral
\expandafter\rmntonum{\test}
expands to \rmntonum{\test}
since \expandafter
applies to {
(skipping over \rmntonum
, as the name implies). What you're after is \expandafter\rmntonum\expandafter{\test}
in order to expand \test
. You can update \rmntonum
to do this by default:
7 corresponds to VII
7 should not be -1!
\documentclass{article}
\usepackage{etoolbox}
\def\test{VII}
\let\oldrmntonum\rmntonum
\renewcommand{\rmntonum}[1]{\expandafter\oldrmntonum\expandafter{#1}}
\begin{document}
\rmntonum{VII} corresponds to \test
\rmntonum{\test} should not be -1!
\end{document}
You could save some keystrokes
\let\oldrmntonum\rmntonum
\renewcommand{\rmntonum}{\expandafter\oldrmntonum\expandafter}
but it requires you to explicitly brace the argument to \rmntonum
(rather than, say, \rmntonum\test
). Of course, this is not a bad idea in general.
It's a matter of turning a kernel function into a user level command, with expl3
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\romantoarabic}{m}
{
\int_from_roman:f { #1 }
}
\cs_generate_variant:Nn \int_from_roman:n { f }
\ExplSyntaxOff
\begin{document}
\romantoarabic{VII}
\newcommand{\seven}{VII}
\romantoarabic{\seven}
\end{document}
This prints two 7’s.
You can even do
\romantoarabic\seven
(but I wouldn't recommend it).
If you are using LuaLaTeX, there's a slicker definition:
\documentclass{article}
\usepackage{etoolbox}
\let\etoolboxrmntonum\rmntonum
\renewcommand{\rmntonum}[1]{%
\expanded{\noexpand\etoolboxrmntonum{#1}}%
}
\newcommand\test{VII}
\newcommand\another{X\test}
\begin{document}
\rmntonum{VII} corresponds to \test
\rmntonum{\test} should not be -1!
\rmntonum\test{} should not be -1!
\rmntonum{C\another} should not be -1!
\end{document}