8

I know that there is a command \romannumeral that converts numbers in roman numeral

\romannumeral3 

gives iii

Is there a converse like

\arabicnumeral{x}

that should produce 10?

11

The etoolbox package has a \rmntonum command:

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{etoolbox}

\begin{document}

\rmntonum{MMXV}

\rmntonum{mmxvi}

\rmntonum{mMxViI}

\end{document} 

enter image description here

7

There should be a “classical” package, but expl3 includes it out of the box; it's just a matter of defining an interface to it.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\arabicnumeral}{m}
 {
  \int_from_roman:n { #1 }
 }
\ExplSyntaxOff

\begin{document}

\arabicnumeral{x}

\arabicnumeral{MMXV}

\end{document}

enter image description here

3

Here is an example how to do this at TeX primitive level. The \romantonum macro is defined here with usage \romantonum{mmcdvii}. This prints 2407.

\newcount\tmpnum
\def\romantonum#1{\tmpnum=0
   \lowercase\expandafter{\expandafter\romantonumA#1\end{}}\the\tmpnum}
\def\romantonumA#1{\ifx#1\end\else
   \ifx#1m\advance\tmpnum by1000 \let\next=\romantonumA
   \else \def\next{\romantonumB#1}%
   \fi \expandafter\next \fi
}
\def\romandef#1#2#3#4#5#6{%
   \def#1##1##2{\ifx##1\end\else
      \ifx##1#6\ifx##2#4\advance\tmpnum by9#3 \let\next=#1%
               \else \ifx##2#5\advance\tmpnum by4#3 \let\next=#1%
                     \else \advance\tmpnum by1#3 \def\next{#1##2}%
               \fi   \fi
      \else    \ifx##1#5\advance\tmpnum by5#3 \def\next{#1##2}%
               \else \def\next{#2##1##2}%
      \fi      \fi
      \expandafter\next \fi
}}
\romandef\romantonumB\romantonumC{00}mdc
\romandef\romantonumC\romantonumD{0}clx 
\romandef\romantonumD\romantonumE{}xvi 
\def\romantonumE#1\end{\errmessage{romantonum: something wrong}}

\romantonum{mmcmlvii}
\romantonum{MMXV}

\end
  • Does this also work under \def\rnum{MMXV}\romantonum{\rnum} (not tested)? – Werner Feb 18 '15 at 21:10
  • Note that both \rmntonum of etoolbox and \int_from_roman:n of expl3 are fully expandable. But they use \numexpr – egreg Feb 18 '15 at 21:16
  • @Werner OK, I've added two \expandafters in order to work your example :). – wipet Feb 18 '15 at 21:18
  • @egreg I am not able to imagine why somebody explicitly needs this feature fully expanable. Anytime, when user needs to use this in \edef or \write then he/she can do something like \prepareout before \edef or \write and then use \the\tmpnum. With a small knowleges about \csname and \endcsname it is possible to do this in asynchronous write too. I spent 10 minutes of my time with programming this because the programming at TeX primitive level is entertaining. The similar thinks used in the code cannot be used in any other programming language. – wipet Feb 19 '15 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.