0

I am working on a document using XeLaTeX and Polyglossia for typesetting Arabic/Persian text. I'm trying to set a specific font for numbers (0-9) while keeping the main font (Vazirmatn) for Arabic/Persian characters.

Furthermore, I have defined my fonts as follows:

\documentclass{article}
\usepackage{polyglossia}
\usepackage{fontspec}

\setmainlanguage{farsi}

\newfontfamily\persianfont[Script=Arabic]{Vazirmatn}
\newfontfamily\persiandigitsfont[Scale=MatchUppercase]{TeX Gyre Pagella}

\setmainfont{Vazirmatn}

However, this doesn't seem to affect the numbers in my text. How can I tell Polyglossia to use \persiandigitsfont for rendering numbers in my document while keeping \persianfont for Arabic/Persian characters?

Minimal Working Example:

\documentclass{article}
\usepackage{polyglossia}
\usepackage{fontspec}

\setmainlanguage{english}
\setotherlanguage{persian}

\newfontfamily\persianfont[Script=Arabic]{Vazirmatn}
\newfontfamily\persiandigitsfont[Scale=MatchUppercase]{TeX Gyre Pagella}

\begin{document}
    \begin{enumerate}
        \item testing
        \item still testing
    \end{enumerate}
    \begin{enumerate}
        \begin{persian}
            \item این نمونه فارسی است.
            \item این نمونه فارسی است.
        \end{persian}
    \end{enumerate}
\end{document}

two types of numbers are used, depending on the language environment

I appreciate any help or guidance on how to achieve this. Thank you!

P.S.: This is a follow-up of sorts to my previous question, as xepersian fixes this, even for mixed language documents. Unfortunately, it introduces other issues and is generally not apt for documents mixing languages.

Also, a search has yielded two questions that seem to be mostly identical. One has not been answered yet, the other is beyond the scope, replacing digits in all fonts, if I am not mistaken.

6
  • Just to clarify, you want to replace the font only when the language is Persian? You can use the solution you linked and de/activate this feature when you switch between languages.
    – Udi Fogiel
    Sep 22 at 10:47
  • yes, exactly. Which solution do you mean?
    – Floris
    Sep 22 at 12:27
  • 1
    your mwe i about arabic numerals, while the pictures shows hindu-arabic, can you clarify that?
    – Udi Fogiel
    Sep 23 at 8:36
  • yes, you are right. The mwe does not make sense. I will edit to clarify.
    – Floris
    Sep 25 at 15:13
  • The new MWE illustrates my issue more clearly.
    – Floris
    Sep 25 at 15:23

1 Answer 1

1

The following is similar to the answer you linked to. It limit the font changes to when the Persian language is in use (via \inlineextras@persian and \blockextras@persian from polyglossia) and it also accounts for character classes bidi defines.

Note that bidi already set \XeTeXinterchartokenstate to 1, so the is no need to do that. Additionally this solution can break the unicode bidi algorithm XeTeX is applying on short text, but you shouldn't really rely on that in the first place.

I don't have Vazirmatn installed on my system, so I used Amiri. The colors are just for testing.

\documentclass{article}
\usepackage{polyglossia}
\usepackage{fontspec}
\usepackage{xcolor}

\setmainlanguage{english}
\setotherlanguage{persian}


\newfontfamily\persianfont[Script=Arabic,Color=red]{Amiri}
\newfontfamily\persiandigitsfont[Scale=MatchUppercase,Color=blue]{TeX Gyre Pagella}

\newXeTeXintercharclass\CharNumbers
\XeTeXcharclass`0 = \CharNumbers
\XeTeXcharclass`1 = \CharNumbers
\XeTeXcharclass`2 = \CharNumbers
\XeTeXcharclass`3 = \CharNumbers
\XeTeXcharclass`4 = \CharNumbers
\XeTeXcharclass`5 = \CharNumbers
\XeTeXcharclass`6 = \CharNumbers
\XeTeXcharclass`7 = \CharNumbers
\XeTeXcharclass`8 = \CharNumbers
\XeTeXcharclass`9 = \CharNumbers

\newcommand*\BegDigits{}
\newcommand*\EndDigits{}
\makeatletter
\XeTeXinterchartoks 0 \CharNumbers = {\BegDigits}
\XeTeXinterchartoks 4095 \CharNumbers = {\BegDigits}
\XeTeXinterchartoks \bidi@sepmark@charclass \CharNumbers = {\DigitsDotDashInterCharToks\BegDigits}
\XeTeXinterchartoks \bidi@digits@charclass \CharNumbers = {\BegDigits}
\XeTeXinterchartoks \CharNumbers 0 = {\EndDigits}
\XeTeXinterchartoks \CharNumbers 4095 = {\EndDigits}
\XeTeXinterchartoks \CharNumbers \bidi@sepmark@charclass = {\EndDigits}
\XeTeXinterchartoks \CharNumbers \bidi@digits@charclass = {\EndDigits}
\addto\blockextras@persian{\def\BegDigits{\begingroup\persiandigitsfont}\def\EndDigits{\endgroup}}
\addto\inlineextras@persian{\def\BegDigits{\begingroup\persiandigitsfont}\def\EndDigits{\endgroup}}
\makeatother

\begin{document}
    \begin{enumerate}
        \item testing
        \item still testing
    \end{enumerate}
    \begin{enumerate}
        \begin{persian}
            \item این نمونه فارسی است.
            \item این نمونه فارسی است.
        \end{persian}
    \end{enumerate}

\begin{enumerate}
    \item testing
    \item still testing
\end{enumerate}
\begin{enumerate}
    \begin{persian}
        \item این نمونه فارسی است.
        \item این نمونه فارسی است.
    \end{persian}
\end{enumerate}

Short persian text: \textpersian{Test 123 Test}

\end{document}\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .