Monday, May 27, 2013

XeLaTeX for Sanskrit: update

In a post on 5 July 2010, I gave an example of how to use XeLaTeX with various fonts and various ways of inputting text.   Some time later, the commands in the Fontspec and Polyglossia packages were updated, and my example didn't work as advertised any more.  Here is an update that works again.

Here's the output:

And here's the updated input:

% This is a Unicode file.
\usepackage{multicol} % just to get narrow columns on one page
\usepackage{polyglossia} % the multilingual support package
% for XeLaTeX - includes Sanskrit.
% Next, from the polyglossia manual:
\setdefaultlanguage{english} % this is mostly going to be English text, with
\setotherlanguage{sanskrit} % some Sanskrit embedded in it.
% These will call appropriate hyphenation.
\usepackage{xltxtra} % standard for nearly all XeLaTeX documents
\defaultfontfeatures{Mapping=tex-text} % ditto
\setmainfont{Gandhari Unicode} %could be any Unicode font

% Now define some Devanagari fonts:
% At least *one* font family must be called \sanskritfont or \devanagarifont,
% so that XeTeX loads all hyphenation and other stuff for Sanskrit. 
% Once the Sanskrit ``intelligence'' is loaded, it can be invoked at
% other places as needed using \selectlanguage{sanskrit}
% John Smith's Nakula, input using Velthuis transliteration
\sanskritfont [Script=Devanagari,Mapping=velthuis-sanskrit]{Nakula}
% John Smith's Sahadeva, input using Velthuis transliteration
\sahadevafont [Script=Devanagari,Mapping=velthuis-sanskrit]{Sahadeva}
% John's Sahadeva, input using scholarly romanisation in Unicode
\sahadevaunicodefont [Script=Devanagari,Mapping=iast]{Sahadeva}
% Microsoft's Mangal font (ugh!), input using standard romanisation in Unicode.
\mangalfont [Script=Devanagari,Mapping=iast]{Mangal}
% Daniel Stender's is used above to get the mapping
% from Unicode to Devanāgarī. Zdenek Wagner's
% is used to get the Velthuis to Devanāgarī mapping. These are the files
% that XeTeX uses to make all the conjunct consonants without needing
% any external preprocessor (like the old devnag.c program).
% % Set up the font commands:
\newcommand{\velt}[1]{{{\selectlanguage{sanskrit}\sanskritfont #1}}}
\newcommand{\sahauni}[1]{{{\selectlanguage{sanskrit}\sahadevaunicodefont #1}}}
\newcommand{\mangaluni}[1]{{{\selectlanguage{sanskrit}\mangalfont #1}}}
% \textssanskrit, above, is a Polyglossia command that gets Sanskrit hyphenation right.
% ... and here we go!
\begin{multicols}{2} % narrow cols to force plenty of hyphenation
\large % ditto.
With Xe\LaTeX\ it's easy to typeset Velthuis encoded Devanagari like the following example, without using a preprocessor:
\velt{sugataan sasutaan sadharmakaayaan pra.nipatyaadarato 'khilaa.m"sca vandyaan|
sugataatmajasa.mvaraavataara.m kathayi.syaami yathaagama.m samaasaat||} Bodhicaryāvatāra 1,1.
NB: automatic hyphenation.
A different Devanāgarī font:
\saha{sugataan sasutaan sadharmakaayaan pra.nipatyaadarato 'khilaa.m"sca vandyaan|
sugataatmajasa.mvaraavataara.m kathayi.syaami yathaagama.m samaasaat||} Bodhicaryāvatāra 1,1.
Another sentence: \saha{|||} 2,10.
Now, thanks to Stender's, we can input in Unicode, using standard scholarly transliteration, and get Devanāgarī generated for us automatically:
\sahauni{āsīdrājā nalo nāma vīrsenasuto balī||\par }
Plain Unicode input, no tricks:
āsīdrājā nalo nāma vīrsenasuto balī||
Plain Unicode romanisation input, no tricks:
\mangaluni{āsīdrājā nalo nāma vīrsenasuto balī||\par }
Plain Unicode Devanāgarī input, no tricks:
{\mangalfont आसीद्राजा नलो नाम वीरसेनसुतो बली|\par}

English and Devanāgarī are both doing okay. The only thing that isn't hyphenating well yet is Sanskrit in roman transliteration.

Other nice stuff becomes easy. E.g., define a command \verb|\example| that prints a romanised word in Nāgarī, and then repeats it in romanisation, in parentheses:


Input: \verb|\example{ekadhā}|

Output: \example{ekadhā}
%that's all folks!

1 comment:

  1. Thank you very much for this superb contribution, it really helped in a number of ways. I am just wondering a few things if you could kindly explain the individual elements of the following section:

    \newcommand{\velt}[1]{{{\selectlanguage{sanskrit}\sanskritfont #1}}}

    a few things I wonder: what does the [1] segment do? what does the #1 segment do? why are there three curly brackets in the second section of the command? Again, thank you for the work, it has greatly helped me access a diversity of fonts for the same writing systems (Tibetan and Sanskrit). Kind greetings from Hamburg, Michael Zrenner