[Tex/LaTex] Additive Roman Numeral 4

roman numerals

I am working on a diplomatic edition of a 15th century book and it numbers its folios using the standard roman numerals of the time. I've been using the following to get the page/folio in my header:

\newcommand{\rectoverso}[1]{\ifthenelse{\isodd{#1}}{r}{v}}
\newcommand{\folio}[1]{\the\numexpr (#1+1)/2\relax}
\uppercase\expandafter{\romannumeral \value{\folio{\thepage}}}\rectoverso{\thepage}

This works great, but the problem is that the roman numeral sequence is off of what I need. Instead of I, II, III, IIII, V, VI, VII, VIII, IX, X, I get the subtractive IV (somewhat understandably, given it's the far more common one in modern usage). My source text only (and always) uses the additive 4 in the one's position: 24 is XXIIII, 34 is XXXIIII but 44 is XLIIII. 9 is universally rendered subtractively as IX or XC.

Is there a package that allows for some control over which numbers are done additively and which are subtractively, or would my best option be to write my own custom definition to account for the specific style used in my book?

Best Answer

This is a fully expandable version, so in your header or footer you just use \thepage and not complicated unexpandable constructions.

\documentclass[twocolumn]{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\rectoversoroman}{m}
 { % #1 is a counter name
  \guifa_rectoverso_roman:n { #1 }
 }

\cs_new:Nn \guifa_rectoverso_roman:n
 {
  \guifa_rectoverso_fix:f { \int_div_round:nn { \value{#1} } { 2 } } 
  \int_if_odd:nTF { \value{#1} } { r } { v }
 }

\cs_new:Nn \guifa_rectoverso_fix:n
 {
  \int_compare:nTF { \int_mod:nn { #1 } { 10 } == 4 }
   {
    \int_to_Roman:n { \int_div_truncate:nn { #1 } { 10 } * 10 } IIII
   }
   {
    \int_to_Roman:n { #1 }
   }
 }
\cs_generate_variant:Nn \guifa_rectoverso_fix:n { f }
\ExplSyntaxOff

\renewcommand{\thepage}{\rectoversoroman{page}}

\newcounter{test}
\renewcommand{\thetest}{\rectoversoroman{test}}

\begin{document}

\ExplSyntaxOn
\prg_replicate:nn {250}
 {
  \stepcounter{test}\thetest\par
 }
\ExplSyntaxOff

\label{test}

This text is on page \pageref{test}

\end{document}

enter image description here