# Using unicode Roman numerals in XeTeX

As mentioned in How can I get Roman numerals in text?, there are several ways in which one can add Roman numerals to text in TeX. However, these solutions all seem to use three I's to make a roman numeral 3, rather than combining using the "Number forms" symbols found in many fonts such as "Ⅲ".

Is there a way to get XeTeX to render all of the Roman numerals in a document using the special Roman numeral symbols found in many fonts?

-
Please note the Roman numerals in Unicode are compatibility characters, and using ordinary Latin characters to denote Roman numerals is preferred. – Khaled Hosny Dec 18 '11 at 16:22
@KhaledHosny Can you show a reference? – egreg Dec 18 '11 at 17:28
Check section 15.3, p. 486 of The Unicode Standard, Version 6.0 book; For most purposes, it is preferable to compose the Roman numerals from sequences of the appropriate Latin letters. However, the uppercase and lowercase variants of the Roman numerals through 12, plus L, C, D, and M, have been encoded for compatibility with East Asian standards. – Khaled Hosny Dec 18 '11 at 17:43
I'm quite surprised to see that you think that my answer deals only with small numbers; probably you haven't tried it with larger ones. – egreg Dec 21 '11 at 7:58
@BrunoLeFloch The decomposition of Ⅸ is <compat> 0049 0058 [ ‌I ‌X ], so I don't see a point here; however, writing ⅮⅨ (D-IX) instead of ⅮⅠⅩ (D-I-X) might give spacing problems. – egreg Dec 22 '11 at 10:44

\documentclass{article}
\usepackage{fontspec}
\usepackage{libertine}
\def\uromannumeral#1{\symbol{\numexpr"216F+#1\relax}}
\def\uRomannumeral#1{\symbol{\numexpr"215F+#1\relax}}
\def\uroman#1{\uromannumeral{\the\value{#1}}}
\def\uRoman#1{\uRomannumeral{\the\value{#1}}}

\begin{document}
\uromannumeral{12} \uRomannumeral{7}

\uroman{page} \uRoman{page}

\def\theenumi{\uRoman{enumi}}
\begin{enumerate}
\item foo
\item foo
\item foo
\item foo
\item foo
\end{enumerate}

\end{document}


-
\makeatletter
\def\UniRoman#1{\expandafter\@UniRoman\csname c@#1\endcsname}
\def\uniroman#1{\expandafter\@uniroman\csname c@#1\endcsname}

\def\@UniRoman#1{\ifcase#1\or
Ⅰ\or Ⅱ\or Ⅲ\or Ⅳ\or Ⅴ\or Ⅵ\or Ⅶ\or Ⅷ\or Ⅸ\or Ⅹ\or Ⅺ\or Ⅻ\else
\expandafter\@slowUniRoman\romannumeral #1@\fi}
\def\@slowUniRoman#1{\ifx @#1% then terminate
\else
\if i#1Ⅰ\else\if v#1Ⅴ\else\if x#1Ⅹ\else\if
l#1Ⅼ\else\if c#1Ⅽ\else\if d#1Ⅾ\else \if
m#1Ⅿ\else#1\fi\fi\fi\fi\fi\fi\fi
\expandafter\@slowUniRoman
\fi
}
\def\@uniroman#1{\ifcase#1\or
ⅰ\or ⅱ\or ⅲ\or ⅳ\or ⅴ\or ⅵ\or ⅶ\or ⅷ\or ⅸ\or ⅹ\or ⅺ\or ⅻ\else
\expandafter\@slowuniroman\romannumeral #1@\fi}
\def\@slowuniroman#1{\ifx @#1% then terminate
\else
\if i#1ⅰ\else\if v#1ⅴ\else\if x#1ⅹ\else\if
l#1ⅼ\else\if c#1ⅽ\else\if d#1ⅾ\else \if
m#1ⅿ\else#1\fi\fi\fi\fi\fi\fi\fi
\expandafter\@slowuniroman
\fi
}
\makeatother

\newcounter{cnta}
\renewcommand{\thecnta}{\uniroman{cnta}} % lowercase Roman numerals
\newcounter{cntb}
\renewcommand{\thecnta}{\UniRoman{cntb}} % uppercase Roman numerals


It's up to you to decide that \roman and \@roman become, respectively, \uniroman and \@uniroman (similarly for the uppercase variant).

These macros allow for representing any positive integer with the Roman number system. If one wants to use also "ↁ", "ↂ", "ↇ", and "ↈ", then some more work is needed.

Support for the extended numerals can be obtained in a quite straigthforward way:

\usepackage{bigintcalc}
\makeatletter
\def\extUniRoman#1{\expandafter\@extUniRoman\csname c@#1\endcsname}

\def\@extUniRoman#1{%
\expandafter\@slowhighUniRoman\romannumeral\bigintcalcDiv{\number#1}{1000}@%
\expandafter\@slowUniRoman\romannumeral\bigintcalcMod{\number#1}{1000}@}
\def\@slowhighUniRoman#1{\ifx @#1% then terminate
\else
\if i#1ↀ\else\if v#1ↁ\else\if x#1ↂ\else\if
l#1ↇ\else\if c#1ↈ\else\if d#1ↈↈↈↈↈ\else \if
m#1ↈↈↈↈↈↈↈↈↈↈ\else#1\fi\fi\fi\fi\fi\fi\fi
\expandafter\@slowhighUniRoman
\fi
}
\makeatother


Just to make an example, with

\newcounter{cntc}
\renewcommand{\thecntc}{\extUniRoman{cntc}}
\setcounter{cntc}{792409}
\thecntc


we get

-
Oh, I've never seen roman numerals like ↁ – Leo Liu Dec 21 '11 at 14:12
Neither have I, but it seems that they were used in some cases. The ↀ form for 1000 was rather common, actually. – egreg Dec 21 '11 at 14:16
That's funny. Now the solution is suitable for generic usage, maybe it can be exported as a package or merge into some package? – Leo Liu Dec 21 '11 at 14:33
Probably with some options to decide which kind of roman numeral is used. – Bruno Le Floch Dec 22 '11 at 6:34

The free (Open Font License) font Junicode automatically turns "spelled out" Roman numerals from I to XII (both uppercase and lowercase) into their Unicode counterparts if you activate discretionary ligatures (dlig). It works just like f and l are combined to the ﬂ ligature in the output.

Pros:

• no additional code necessary when you're writing
• awesome free font, not only, but especially for Medievalists

Cons:

• numbers like XIII look strange because they're rendered as XII+I
This really is a problem on Unicode's side though, because Unicode only has Roman numbers up to 12. Then again, I guess Junicode could've kept going and added more numerals as ligatures in the private area usage area of Unicode.

• you may not want all the ligatures in your document that are included in dlig
For the latter problem, Mico's upcoming selnolig package might be useful, which allows you to globally disable a specific ligature with a single macro, all using LuaTeX. Stay tuned.

Here's a document showing some of Junicode's discretionary ligatures:

% compile with XeLaTeX or LuaLaTeX
\documentclass{article}

\usepackage{fontspec}
\setmainfont[Ligatures={Discretionary}]{Junicode}
% Naturally, I'd also enable "TeX" and "Common" ligatures
% when using discretionary ligatures
% I dare you to check out Junicode's historical ligatures!
% http://junicode.sourceforge.net/Junicode.pdf#page=13

\textwidth=4cm % These two lines are just
\parindent=0cm % to keep my example compact

\begin{document}

i, ii, iii, iv, v, vi, vii, viii, ix, x, xi, xii

I, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII

{\Huge XIII} % That ain't pretty

Ⅼ, Ⅽ, Ⅾ, Ⅿ, ↀ, ↁ, ↂ % These are (all?) other Unicode Roman numerals Junicode has
% (which have nothing to do with dlig though)

tt, tr, tty % These are some presumably acceptable discretionary ligatures of Junicode

ct, hv, st % These are the weird ligatures in dlig

[7], [[4]], <13>, [g], [W] % These are also part of dlig, but probably not bothersome

\end{document}


-