[Tex/LaTex] Specifying an oldstyle number font with fontspec

fontspecluatexoldstylenums

I have some trouble setting oldstyle numbers in my document. I load the font (in this case an early palatino) and define the smallcaps font via fontspec. But the oldstyle numbers are in the smallcaps variant of this font and don't display in the main text.

\setmainfont[ Numbers={Proportional, OldStyle}, SmallCapsFont={PalmerCapsOld} ]{Palmer}

Best Answer

What fontspec provides is syntax for enabling OpenType features. What you need is the ability to switch fonts every time numerals are displayed.

XeTeX has the XeTeXinterchartoks functionality to enable this; LuaTeX does not (at least not directly; see below).

For example, try this code:

% !TeX program = xelatex
\documentclass{article}
\XeTeXinterchartokenstate = 1
\newXeTeXintercharclass \numeralsclass
\newXeTeXintercharclass \numeralsclass
\count255=`\0
\loop\ifnum\count255<`\9
    \XeTeXcharclass \count255 \numeralsclass
    \advance\count255 by 1
\repeat

\XeTeXinterchartoks 0 \numeralsclass = {\bgroup\itshape}
\XeTeXinterchartoks 255 \numeralsclass = {\bgroup\itshape}
\XeTeXinterchartoks \numeralsclass 0 = {\egroup}
\XeTeXinterchartoks \numeralsclass 255 = {\egroup}

\begin{document}
abc 123 def456jkl789

32

`33'--44
\end{document}

This code allocates a character class for numerals, then assigns it to 0–9.

Between character class 0 (letters) or 255 (non-characters: glue, kern, math, box, etc.) and the newly-defined character class for numerals, we insert a font change (\itshape for visibility); between numerals and class 0/255 we insert \egroup to undo that font change.

You should be able to apply this to your case something like this:

\setmainfont[ Numbers={Proportional}, SmallCapsFont={PalmerCapsOld} ]{Palmer}
…
\XeTeXinterchartoks 0 \numeralsclass = {\bgroup\scshape}
\XeTeXinterchartoks \numeralsclass 0 = {\egroup}
\XeTeXinterchartoks 255 \numeralsclass = {\bgroup\scshape}
\XeTeXinterchartoks \numeralsclass 255 = {\egroup}

But… you wanted a LuaLaTeX solution.

See In LuaTex is it possible to change font/language according to the script/glyphs used? for LuaTeX code that mimics XeTeXinterchartoks. The trouble is a difference in when the code executes; if I’ve understood Manuel Pégourié-Gonnard’s comment correctly, even code like \count255 might break.

You might be better off running a regexp search on your document, to surround each run of numerals with \textsc{}.