0

I'm using xelatex with xepersian package. What I'm trying to do is to use a specific font for Persian parts of the text (Persian/Arabic Unicode characters) and another one for English parts (English Unicode characters).

In HTML + CSS, I can easily do:

p { font-family: persianfont, englishfont }

Using correct fonts where persianfont only supports Persian characters and englishfont only supports English characters, HTML renders text correctly.
It tries to render the character with the first font, but if it doesn't exist in it, it moves to the next one. (We can say englishfont is a fallback font here)

Now I'm trying to achieve this in TeX.

I tried the code below, but only the last font is applied:

\settextfont[Mapping=english]{Garamond}
\settextfont[Mapping=arabic]{B NAZANIN.ttf}

Also, the Mapping doesn't seem to work.

  • I don't want to use a single font which supports both languages
  • I don't want to use \begin ... \end for every English font in the document! I want it to be automatic.

2 Answers 2

1

You can do this in LuaTeX with the onchar=fonts ids command from the babel package.

\documentclass{article}
\tracinglostchars=3 % Errot if the font and language do not match!
\usepackage[bidi=basic, english]{babel}
\usepackage{fontspec}

\babelprovide[import=fa, onchar=fonts ids]{persian}

\defaultfontfeatures{ Scale=MatchLowercase, Ligatures=TeX }

\babelfont{rm}
          [Scale=1.0]{NewCM10-Book}
\babelfont{sf}
          {NewCMSans10-Book}
\babelfont{tt}
          {NewCMMono10-Book}
\babelfont[persian]{rm}
          {Noto Naskh Arabic}
% Sans-Serif Persian font goes here, if needed.
\babelfont[persian]{tt}
          {ALM Fixed}
          
\begin{document}

\section*{Farsi (فارسی)}

Persian (/ˈpɜːrʒən, -ʃən/), also known by its endonym Farsi (فارسی, Fārsī, [fɒːɾˈsiː], is a Western Iranian language belonging to the Iranian branch of the Indo-Iranian subdivision of the Indo-European languages.

\begin{otherlanguage}{persian}
\section*{مقدمه}


از آنجا که شناسائی حیثیت ذاتی کلیهٔ اعضای خانواده بشری و حقوق یکسان و انتقال ناپذیر آنان اساس آزادی و عدالت و صلح را در جهان تشکیل میدهد،

از آنجا که عدم شناسائی و تحقیر حقوق بشر منتهی به اعمال وحشیانه‌ای گردیده است که روح بشریت را بعصیان واداشته و ظهور دنیائی که در آن افراد بشر در بیان و عقیده آزاد و از ترس و فقر فارغ باشند بعنوان بالاترین آمال بشر اعلام شده است،

از آنجا که اساساً حقوق انسانی را باید با اجرای قانون حمایت کرد تا بشر بعنوان آخرین علاج بقیام بر ضد ظلم و فشار مجبور نگردد،

از آنجا که اساساً لازم است توسعه روابط دوستانه بین ملل را مورد تشویق قرار داد،
\end{otherlanguage}

\end{document}

New Computer Modern Book + Noto Naskh Arabic sample

Mixing left-to-right and right-to-left scripts complicates things: while this approach can correctly detect individual Persian words within an English paragraph, you still need to use language tagging to change the layout of entire sections or paragraphs.

0

I found the docs of xepersian. There's a \settext for Persian text & \setlatintext. It works really well. However, I still need to separate Latin text with commands like \lr{text} but that's not a huge deal.

2
  • Welcome to TeX.SE!
    – Mensch
    Commented Oct 4, 2021 at 10:31
  • Better use LuaLaTeX or XeTeX with the fontspec package. From command line type textdoc fontspec to see the manual.
    – yannisl
    Commented Oct 4, 2021 at 11:03

You must log in to answer this question.

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