# XeTeX: How to *globally* replace an ASCII character with another symbol?

A few months ago, I asked whether the “EB Garamond” font contains a short-arched version of the letter f. I have now found out how to access this letter because it actually exists. Writing

\newcommand\fshort{\XeTeXglyph\the\XeTeXglyphindex "f.short" \relax}


makes it possible to access this letter by writing \fshort.

Writing

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\myreplace}{m}
{
\tl_set:Nn \l__maxd_argument_tl { #1 }
\tl_replace_all:Nnn \l__maxd_argument_tl { f } { \fshort }
\tl_use:N \l__maxd_argument_tl
}
\tl_new:N \l__maxd_argument_tl
\ExplSyntaxOff


will result in the replacement of f with the short-arched version of it when the text is encompassed by \myreplace{}.

This, however, is not very convenient. I would like to globally replace every f character with the short-arched f.

I already tried using catcodes, but they do not work when it comes to (certain) ASCII characters. This answer which contains Cyrillic characters works perfectly fine:

\catcode\Ё=\active
\defЁ{E}
\catcode\ё=\active
\defё{e}


But writing something like

\catcode\f=\active
\deff{\fshort}


or

\catcode\f=\active
\def f{\fshort}


does not work. Also this question did not solve my problem.

David Carlisle’s solution works!

If you also want to use Q.short in combination with f.short, use the following code which has to be written in that order:

\newcommand\fshortgobble[1]{\XeTeXglyph\the\XeTeXglyphindex "f.short" \relax}

\newXeTeXintercharclass\fclass
\XeTeXcharclass f =\fclass
\XeTeXinterchartoks 0 \fclass  {\fshortgobble}
\XeTeXinterchartoks \fclass \fclass  {\fshortgobble}
\XeTeXinterchartoks 4095 \fclass  {\fshortgobble}

\newcommand\Qshortgobble[1]{\XeTeXglyph\the\XeTeXglyphindex "Q.short" \relax}

\newXeTeXintercharclass\Qclass
\XeTeXcharclass Q =\Qclass
\XeTeXinterchartoks 0 \Qclass  {\Qshortgobble}
\XeTeXinterchartoks \Qclass \Qclass  {\Qshortgobble}
\XeTeXinterchartoks 4095 \Qclass  {\Qshortgobble}

• If the font has not a suitable feature, you need a TECkit mapping. See e.g. tex.stackexchange.com/a/95418/2388 Dec 30, 2019 at 18:34
• f.short does not seem to be mapped properly in the current EB Garamond version. Therefore, I do not know the hex number for f.short. But accessing it with \newcommand\fshort{\XeTeXglyph\the\XeTeXglyphindex "f.short" \relax} works. Dec 30, 2019 at 18:43

You could use a character class

\documentclass{article}
\usepackage{fontspec}
\newcommand\fshortgobble[1]{\XeTeXglyph\the\XeTeXglyphindex "f.short" \relax}

\newXeTeXintercharclass\fclass
\XeTeXcharclass f =\fclass
\XeTeXinterchartoks 0 \fclass  {\fshortgobble}
\XeTeXinterchartoks \fclass \fclass  {\fshortgobble}
\XeTeXinterchartoks 4095 \fclass  {\fshortgobble}
\begin{document}

\setmainfont{EB Garamond}

f   ff  difficult football diff biff fence

\XeTeXinterchartokenstate=1

f   ff  difficult football diff biff fence

\end{document}

• Your solution works! Thank you very much. But I do not know for what 0 and 4095 stand for. Do I have to use other numbers when I am also using Q.short` as shown in the example I just posted? Dec 30, 2019 at 20:39
• @Nemgathos see the xetex manual (texdoc xetex) 0 is the default class (so most letters) and 4095 is the non-letter class (so start of string for example) Dec 30, 2019 at 20:43