# Font selection in XeTeX for specific characters

I just ran into another issue, I like to use gill sans very much but the number 1, lower case L and upper case I are almost indistinguisable. On the other hand I like Helvetica numbers. Is there a way to use Gill Sans for all non-numeric characters and Helvetica only for numbers [0 9] ?

-
I think it's likely that you just gave some of the people around here a migraine. – Kieran Feb 4 '11 at 16:25
I did ?, that was not my intention... – Gregthom Feb 8 '11 at 1:36

Yes, you can. But I only know a tricky method --- to use \XeTeXinterchartoks and related commands. There have been some packages, for example xeCJK and ucharclasses, that use this mechanism for multilingual support. (I wrote a few code for xeCJK.) For more information, you can read XeTeX's reference.

Set \XeTeXinterchartokenstate=1 to enable the mechanism.

There are already some predefined char classes. 0 for normal westen alphabets and symbols, 1 for CJK ideographs, 255 for boundary, etc. And you can use \newXeTeXintercharclass to allocate a new class.

Use \XeTeXcharclass to set the numbers to the new character class, and use \XeTeXinterchartoks to do the trick.

Full example:

\documentclass{article}
\usepackage{fontspec}
\XeTeXinterchartokenstate=1
\chardef\CharNormal=0
\chardef\CharBound=255
\newXeTeXintercharclass\CharNumbers
\XeTeXcharclass0=\CharNumbers
\XeTeXcharclass1=\CharNumbers
\XeTeXcharclass2=\CharNumbers
\XeTeXcharclass3=\CharNumbers
\XeTeXcharclass4=\CharNumbers
\XeTeXcharclass5=\CharNumbers
\XeTeXcharclass6=\CharNumbers
\XeTeXcharclass7=\CharNumbers
\XeTeXcharclass8=\CharNumbers
\XeTeXcharclass9=\CharNumbers
\newtoks\TokSetfont
\TokSetfont={\begingroup\fontspec{Latin Modern Mono}}
\XeTeXinterchartoks\CharNormal\CharNumbers=\TokSetfont
\XeTeXinterchartoks\CharBound\CharNumbers=\TokSetfont
\XeTeXinterchartoks\CharNumbers\CharNormal={\endgroup}
\XeTeXinterchartoks\CharNumbers\CharBound={\endgroup}
\begin{document}
0123

abc123

123abc

abc123def

123abc456
\end{document}


BTW, this does not affect math fonts.

-
I second the suggestion of ucharclasses, somewhat reluctantly. It increased my compilation time by a factor of twenty, from ten seconds to two minutes. So now I need conditional compilation, with, among other things, special handling for the CMU Serif “~” bug. The lack of handling of missing characters in math fonts is particularly annoying, especially for a feature that should be (as it is on OSX) a built-in feature. – Flash Sheridan Mar 29 '11 at 16:00

I don't know if this works in LaTeX, but at least with Plain:

\font\mydigitsfont="Helvetica"
\font\mydigitssfont="Helvetica/S=7" at 7pt
\font\mydigitsssfont="Helvetica/S=5" at 5pt
\newfam\mydigitsfam
\textfont\mydigitsfam=\mydigitsfont
\scriptfont\mydigitsfam=\mydigitssfont
\scriptscriptfont\mydigitsfam=\mydigitsssfont
\XeTeXmathcode0="0\mydigitsfam0
\XeTeXmathcode1="0\mydigitsfam1
\XeTeXmathcode2="0\mydigitsfam2
\XeTeXmathcode3="0\mydigitsfam3
\XeTeXmathcode4="0\mydigitsfam4
\XeTeXmathcode5="0\mydigitsfam5
\XeTeXmathcode6="0\mydigitsfam6
\XeTeXmathcode7="0\mydigitsfam7
\XeTeXmathcode8="0\mydigitsfam8
\XeTeXmathcode9="0\mydigitsfam9
\def\mydigits{\fam\mydigitsfam\mydigitsfont}
$1^22^{23^3}3^1$ 123 {\mydigits 123}
\bye


So, automatic only for mathmode.

-
This works only for math fonts, which may not be what @Gregthom want. In LaTeX, math fonts for digits can be easily changed using \DeclareMathSymbol in NFSS scheme, without help of XeTeX. – Leo Liu Feb 4 '11 at 17:14
@Leo Liu: I managed to edit in that fact pretty much the same minute you left your comment :‑) – morbusg Feb 4 '11 at 17:18