Wednesday, September 01, 2010

XeLaTeX, Velthuis encoding, and palatal nasals

When using the Velthuis input coding for Devanāgarī, and wanting to have it handled by XeLaTeX, one finds the palatal ñ disappears in the Nāgarī.

input: sa~njaya

output: स न्जय

That's because the Velthuis input code for ञ् is ~n, and the "~" is a special code in TeX, meaning "hard space".

Here's the workaround. I define a font-switching command \dev that will turn Velthuis into Devanāgarī. \dev is mostly made up of "\textsanskrit" which is set up using the standard XeLaTeX/polyglossia \newfontfamily commands. \textsanskrit does the work of invoking the mapping-conversion (from XeTeX's velthuis-sanskrit.tec file).

But just before \textsanskrit, we change tilde into a normal character. And after \textsanskrit, we turn tilde back into an "active" hard space. We use the \aftergroup command so that the "active" version of tilde is activated after the closing of the group that contains the Devanāgarī.

Here's the code:

\newfontfamily\textsanskrit [Script=Devanagari,Mapping=velthuis-sanskrit]{Nakula}

% Make the tilde into a normal letter of the alphabet
\def\maketildeletter{\catcode`\~=11 }

% Return tilde to being the default TeX "active" character for hard space
\def\maketildeactive{\catcode`\~=13 }

\def\dev{\maketildeletter\textsanskrit \aftergroup\maketildeactive}

Here's how you use it:

input: {\dev sa~njaya uvaaca}. What did Dr~Sañjaya say?

output: सञ्जय उवाच. What did Dr Sañjaya say?

where that space betwen "Dr" and "Sañjaya" is hard, and you can't break a line there.



  1. Hi
    I have a text something like shown below
    "1. tapasvii = sagacious thinker; vaalmikiH = Sage [Poet] Valmiki; tapaH = in thoughtful-meditation; and; sva adhyaaya = in self, study [of scriptures]; niratam = always - who is eternally studious in scriptures; and; vaak= in speaking [in enunciation]; vidaam = among expert enunciators; varam = sublime one - with Narada; muni pungavam = with sage, paragon, with such a paragon sage Naarada; naaradam = with [such a sage] Naarada; pari papracCha = verily [inquisitively,] inquired about; [ellipt. sarva guNa samiSTi ruupam puruSam = all, merited endowments, composite, in form - about such a man.]"

    Is there a way I can convert the sanskrit and leave the english using Xetex
    I would appreciate your help

  2. Certainly. What you want to do can be one rather easily with XeLaTeX. You need to enclose the English, Romanized Sanskrit and Devanagari Sanskrit in separate macros, and then you can easily switch around encodings and scripts using single commands in your LaTeX preamble.

    However, I'm afraid that I cannot help you further with your specific task. The information is available in my examples.

    Dominik Wujastyk

  3. Hi Dominik,

    Have you had luck in getting the ~na to work in footnotes? It appears that LaTeX does not apply the \catcode changes in footnotes?


    1. I haven't returned to the problem. But Schwartz circulated a macro fnfix.sty in 1992, to solve some other problems that arose because Lamport made footnotes use restricted horizontal mode instead of normal horizonal mode.

  4. During an email discussion of this point on 13 October 2014, Zdenek Wagner produced the following solution:

    \setromanfont[Mapping=tex-text]{TeX Gyre Termes}
    {\catcode`\~=12 \gdef\inactivetilde{~}}
    {\dev pata~njali\footnote{{\dev pata~njali}}}
    {\dev mu~nji}\footnote{{\dev mu~nji}}
    {\dev nirj~naata}\footnote{\dev nirj~naata}

  5. Note David Carlisle's comment at

    where he suggests avoiding a catcode change and just redefining ~ :

    David Carlisle via
    15 Aug 2016

    If you base it on catcode changes it will never work in the argument
    of another command, not just footnotes. There is no need to switch

    leave ~ as active always and when you want it locally to be a normal ~ do


    so the active ~ just expands to a non-active one.