4

I am using fontspec, Polyglossia and XeLaTeX to typeset a document in mostly English, but with a few Urdu phrases using the NotoNastaliq font. The problem is that the Urdu phrases get rendered correctly, but left-to-right, instead of right-to-left. I would like to use a specific font, which I have found is best loaded by using \myfont as I have done here.

\documentclass[12pt]{article}  
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry}
\usepackage{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage{english}
\setotherlanguage{urdu}
\newfontfamily\myfont[Script=Arabic,Scale=1.2,Path=/System/Library/Fonts/]{NotoNastaliq.ttc}
\begin{document}
Some text in English within which the Urdu appears incorrectly as left-to-right. Here's an example: {\myfont نقش فریادی ہے کس کی شوخیّ تحریر کا } After this, I want it to continue left-ro-right.

\end{document}  

which renders as

MWE

I would appreciate any help with this. I believe I may not be using polyglossia correctly at all. I have tried the solution posted here but it doesn't quite work..

2
  • 1
    You would only require \setotherlanguage{urdu} when you want to have Urdu words for say, chapter, appendix or table of contents etc., which I suppose you would not require since your document is in English language.
    – Niranjan
    Dec 3, 2023 at 17:26
  • 1
    Note that polyglossia automatically loads fontspec. No need to load it separately.
    – Niranjan
    Dec 3, 2023 at 17:26

3 Answers 3

5

I advise to use lualatex with babel. It identifies the scripts which require RTL typesetting. In this example I have used Awami which better supports Urdu. Here is an example:

\documentclass{article}
\usepackage[english,bidi=basic]{babel}
\usepackage{fontspec}
\newfontfamily\myfont[%
  Renderer     = {Graphite}%
]{AwamiNastaliq-Regular.ttf}

\begin{document}
Some text in English within which the Urdu appears
\emph{correctly} as right-to-left. Here's an example:
{%
  \myfont
  نقش فریادی ہے کس کی شوخیّ تحریر کا%
} After this it continues to be left-to-right.
\end{document}  

1

6
  • 1
    Thanks! Could you also help me figure out how to insert an English word in the middle of the Urdu text? Dec 3, 2023 at 17:51
  • Sure. Try: نقش فریادی ہے {\normalfont english} کس کی شوخیّ تحریر کا
    – Niranjan
    Dec 3, 2023 at 17:53
  • unfortunately this doesn't work very well. your example is for a line that 'starts out' in English, for which the Urdu appears left to right in the editor (I am using TeXShop on a Mac). I may not be able to explain this problem in a comment, but is there a more comprehensive solution to this that covers, for example, the case when an Urdu phrase needs to be inserted into an English sentence/paragraph and when an English phrase needs to be inserted into an Urdu sentence/paragraph? Dec 3, 2023 at 18:12
  • 1
    @EmadMasroor Sorry, I don't understand your question. Editors and LaTeX are separate software. My answer deals with the LaTeX part of it. If you are facing a problem with your editors, then that is out of the scope of this answer. As far as I think this answer indeed is a comprehensive solution for intermingled Urdu and English texts. If you think otherwise, please show us 1) in which context do you think it fails? 2) how do you expect it to look?
    – Niranjan
    Dec 3, 2023 at 18:43
  • 1
    okay, I will ask a new question because I am raising a separate issue --- you've definitely answered my original question! Dec 3, 2023 at 18:58
6

In LuaLaTeX, you can even configure Babel to detect the language you are typing in and select the correct font (and line-breaking) automatically.

\documentclass[12pt]{article}
\tracinglostchars=3 % Make it an error if a character is missing.
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry}
\usepackage[bidi=basic, layout=counters tabular]{babel}
\usepackage{fontspec}

\babelprovide[main, onchar=fonts ids]{english}
\babelprovide[import=ur, onchar=fonts ids]{urdu}

\babelfont{rm}
          {Noto Serif}
\babelfont{sf}
          {Noto Sans}
\babelfont[urdu]{rm}
          [Renderer=HarfBuzz]{Noto Nastaliq Urdu}
\babelfont[urdu]{sf}
          [Renderer=HarfBuzz]{Noto Sans Arabic}


\begin{document}
Some text in English within which the Urdu appears \textit{correctly} as right-to-left. Here's an example:
\foreignlanguage{urdu}{نقش فریادی ہے کس کی شوخیّ تحریر کا }
After this, the English continues left-to-right.  The next paragraph is right-to-left, but contains a few English phrases:

\begin{otherlanguage}{urdu}
نوٹو نستعلیق اردو نوٹو 
\foreignlanguage{english}{(Noto Nastaliq Urdu)}
 میں شامل ایک فونٹ ہے۔گوگل ڈیویلپرز بلاگ پر گوگل کے سوفٹ ویئر انجینئر بہداد اسفہبد نے 10 نومبر 2014ء کو شائع کردہ تحریر میں اردو زبان کے لیے نستعلیق فونٹ جاری کیا۔ یہ فونٹ ایرانی نستعلیق کی طرز پر بنایا گیا ہے اور اسے ترسیموں کی بجائے حرفی بنیاد پر تخلیق کیا گیا ہے۔ 
\end{otherlanguage}
\end{document}

Noto Serif + Noto Nastaliq Urdu sample

I copied the second paragraph from Urdu Wikipedia and apologize for any errors.

This document would also have displayed without any \foreignlanguage commands, but the word spacing would have been slightly wrong. For many other pairs of languages, it would have worked.

To change which language is the main one, set the main option of \babelprovide appropriately.

0

You need to use the polyglossia command for switching languages—not just the fontspec command for switching fonts.

The the MWE below, what's changed is that the font is \urdufont and the command for switching languages is \texturdu{}. (Also I made the font Adobe Arabic, but only because your font wasn't on my system.)

\documentclass[12pt]{article}  
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry}
\usepackage{polyglossia}
\setdefaultlanguage{english}
\setotherlanguage{urdu}
\newfontfamily\urdufont[Script=Arabic,Scale=1.]{Adobe Arabic}
\begin{document}
Some text in English within which the Urdu appears incorrectly as left-to-right. Here's an example: \texturdu{ نقش فریادی ہے کس کی شوخیّ تحریر کا } After this, I want it to continue left-to-right.
\end{document}  

You must log in to answer this question.

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