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.

Enjoy.

6 comments:

  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
    -A

    ReplyDelete
  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.

    Sincerely,
    Dominik Wujastyk

    ReplyDelete
  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?

    Best,
    Anshuman

    ReplyDelete
    Replies
    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.

      https://www.dropbox.com/s/ngcktst82igewd3/fnfix.sty

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

    \documentclass{article}
    \usepackage{fontspec}
    \setromanfont[Mapping=tex-text]{TeX Gyre Termes}
    \newfontfamily{\devanagarifont}[Script=Devanagari,Language=Sanskrit,Mapping=velthuis-sanskrit]{Sanskrit
    2003}
    \DeclareRobustCommand{\dev}{%
    \let~\inactivetilde
    \devanagarifont}
    {\catcode`\~=12 \gdef\inactivetilde{~}}
    \begin{document}
    {\dev pata~njali\footnote{{\dev pata~njali}}}
    {\dev mu~nji}\footnote{{\dev mu~nji}}
    {\dev nirj~naata}\footnote{\dev nirj~naata}
    \end{document}

    ReplyDelete
  5. Note David Carlisle's comment at
    http://tug.org/pipermail/xetex/2016-August/026797.html

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

    David Carlisle d.p.carlisle@gmail.com via tug.org
    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
    catcodes.

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

    \edef~{\string~}

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

    ReplyDelete