# Additive Roman Numeral 4

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?

• (and apologies if this is a rather simple / stupid question — this is the first time I'm using LaTeX, and somewhat out of necessity, because I'm scripting the creation of the document from my source files) – user0721090601 Dec 31 '15 at 18:11
• Is it only 4 which should be rendered IIII or must all cases of IV be eliminated? In other words, is XXIV OK? or should it be XXIIII? I'm asking because I notice that for 9 you've written IX which seems a bit inconsistent. – A.Ellett Dec 31 '15 at 18:14
• @A.Ellett only instances of -4 are IIII (so 40 is XL), 9 is always subtractive (IV or XC). Editing the question now. – user0721090601 Dec 31 '15 at 18:16
• Use of \thepage in calculations are discouraged, as it may not represent a number in general. – Werner Dec 31 '15 at 18:58
• @guifa: \value{page} would retrieve the value (number) of the page counter. However, even that is not reliable at times, the page shipout routine (which influences the page counter) is asynchronous. The best option is to set a \label{<something>} and then use refcount's \getpagerefnumber{<something>}. References are guaranteed to produce the correct page number, and \getpagerefnumber is expandable (so can be used in calculations). – Werner Jan 1 '16 at 23:07

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}


Just replace IV by IIII.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \myRomannumeral { m }
{
\tl_set:Nx \l_tmpa_tl { \int_to_Roman:n { #1 } }
\tl_replace_all:Nnn \l_tmpa_tl { IV } { IIII }
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff
\begin{document}

4 \myRomannumeral{4}

14 \myRomannumeral{14}

44 \myRomannumeral{44}

\end{document}


If you don't mind loading xparse, here's a solution working with expl3 code:

\documentclass{article}
\pagestyle{empty}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand\myRomannumeral{m}
{
\bool_if:nTF { \int_compare_p:n {#1 = 4} }
{
IIII
}
{
\bool_if:nTF { \int_compare_p:n { \int_mod:nn {#1} {10} = 4 } }
{
\int_to_Roman:n { \int_eval:n {#1/10*10} } IIII
}
{
\int_to_Roman:n {#1}
}
}
}
\ExplSyntaxOff

%% used solely for illustrative purposes.
\usepackage{pgffor}

\begin{document}

\foreach \myn in {1,...,30}
{\myn $\rightarrow$ \myRomannumeral{\myn}\par}

\end{document}