Wednesday, November 5, 2014

LaTeX Devanagari-II

 
In last article about LaTeX Devanagari (click here for the article) I explained how to make simple documents in Devanagari using LaTeX. However, the LaTeX users know that one of the biggest power of LaTeX is editing the documents efficiently. For example, if one wants to write a book using LaTeX, then, creating table of contents, chapter names or names of many parts of the book, listing tables or graphics, writing appendices, creating list of special words this all can be done by LaTeX within a stroke of line! Here I am giving and examples for writing a book in Deanagari using LaTeX and XeLaTeX. I try to incorporate as many features, as possible. In the end, I discuss some good and bad issues related to this piece of work.

XeLaTeX Devanagari

Following are the things I wish to achieve:
  1. Use the book environment for LaTeX Devanagari
  2. Include graphics, tables and customise their original commands so that the name the graphics and the tables appear in Devanagari. Then customise the commands \listoffiures and \listoftables for LaTeX Devanagari.
  3. Customise the frontmatter (the title page, the index, the preface part) and the backmatter (the appendices, various lists, glossaries for LaTeX Devanagari.
  4. Customize all numberings to Devanagari.
  5. Customise the mainmatter (chapter names, sections etc) for Devanagari.

Using XeLaTeX
The best way to learn LaTeX is play to with it! :) Following is my code for a XeLaTeX writing. Marathi readers, forgive me for so many typos in the tex file. I am still not very comfortable with the transliteration.

  1. Copy and paste the code to your LaTeX editor.  Save this file to a folder with an ending .tex . 
  2. Add a .png photo to this same folder. Rename the photo as Image.png
  3. Then set the editor to XeLaTeX. NOT on LaTeX or pdfLaTeX.
  4. Run the file two or three times, at least. See the beautiful output.
  5. Some precautions are:
  • There could be some typos. I shall keep updating the articles, until all the typos are eliminated. But take care about "s which stands for श्  . Few LaTeX editors change the character " to `` automatically. This produces ``s instead of "s and the output becomes "स् or ``स् .
  • The sixth line has command \setmainfont which sets the main font for whole of the document. The extra parameter Mapping in this command can take many values. And they all behave differently. For example, Mapping= devanagarinumerals would let you give the input in Devanagari Unicode itself! Which means to get an output रोहित I need not type rohita but simply type रोहित with a Unicode keyboard! 
  • However if you use some other mapping, then the rules of transliteration change! Click here to see the different types of mappings and the corresponding transliteration rules. 

Screenshots of this output are given bellow. Comments close to many commands explain what these commands are meant for.

\documentclass[a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fontspec}

\usepackage[english, hindi]{babel}
\setmainfont[Script=Devanagari,Mapping=velthuis]{Sanskrit 2003}
%\setmainfont[Script=Devanagari,Mapping=devanagarinumerals]{Sanskrit
%2003} this mapping let you give input in Devangari itself

%Changing the titles of standard parts in LaTeX to Devanagari

\addto\captionshindi{%
\renewcommand{\abstractname}{sArA.m"sa}%
\renewcommand{\contentsname}{ anukrama.nikA}%
\renewcommand{\chaptername}{dha.dA}%
\renewcommand{\tablename}{taktA}%
\def\appendixname{parI"sI.s.ta}%
%\def\figurename{}%
\def\listfigurename{citrA.mcI yAdI}%
\def\listtablename{taktyA.mcI yAdI}%
}
\catcode`\~=12

%Document Info
\author{rohita dilIpa hoLakara}
\title{ksele.tek ni bebela}
\date{phebruvarI 1, 2015}

%Page Layout
\usepackage[inner=2.4cm,outer=2cm]{geometry}%left-right margin
%\onehalfspacing %increases spacing between two lines to 1.5


\begin{document}
\let\MakeUppercase\relax
\frontmatter
\maketitle
\tableofcontents

\mainmatter
\chapter{eka}
\section{vibhAga}
mitraho, malA uttara sApa.dale Ahe. udAhara.nAdakhala he likhA.na pa.thavata Ahe.\\
hyA .tAIpase.tI.mgacA phAyadA.
\begin{enumerate}
    \item kI ithe sArakhe  devanAgarIcI kamA.m.da dyAvI lAgata nAhI. 
    \begin{enumerate}
       \item eka
       \item dona
           \begin{enumerate}
       \item he
       \item ase
       \item Ahe
    \end{enumerate}

       \item tina
    \end{enumerate}
    \item sagaLe Aka.de devanAgarIta yetAheta. 
    \item phAIla .tarmInalavara vegaLI ka.mpAIla karAyacI garaja nAhI.
    \item bharapUra phA/.ta upalabdha Aheta (hA sa.msk.rta 2003 Ahe).
    \item pur.navirAmAcA pra"sna su.talAya! :)
        \begin{enumerate}
        \item A.ni hI eka bhArI bAta, ekAta eka Aka.de Ale kI barobara na.mbarI.mga badalate!
        \item ithe ga.nitacii sutre lihi.nyaabaabata maatra tho.daa
go.mdhaLa aahe. kaara.na khaaliila sutra uttama disate,
\[
\int f(x)\, d(x) = \phi^{k(t)^2}
\]
para.mtu nityaacyaa niyamaapramaane jara ".dii" laa .teks.ta-apa kele tara maatra haa go.mdhaLa hoto,
\[
\int f(x)\, \textup{d}(x) = \phi^{k(t)^2}
\]

      \end{enumerate} 
\end{enumerate}

\subsection{upa-vibhAga}
dona mahAtvAce pra"sna rAhileta.
\begin{enumerate}
    \item devanAgarI ni ii.mgrajI kase ekatra lihAyace he malA  ajuna kaLale nahIye.
    \item durdaivane hyA paddhatInehI "bImara" devanAgarImadhe banavAtA yeta nahIye! :(
    
\end{enumerate}

\chapter{aseca}
astu, malA mAgIla drAvi.dI pra.nayAmAhUna hec bare va.tateya! varIla paikI pahilA pra"sna su.tela lavakara a"sI A"sA Ahe! ethe kAhI citre .takUna pAhUyAta kI tyA.mcI nAve ka"sI yetIla. "sIvAya eka taktAhI banavUna .takatoya, he.nekarUna takte A.ni tyA.ncI nAve nI.ta disatIla kI nAhI he kaLela. sarvAta "seva.tI \verb+\listoffigures+ A.ni \verb+\listoftables+ vAparUna citre ni takyA.mcI yAdI suddhA banavalI Ahe.

\begin{figure}[htb]
  \centering
\includegraphics[scale=0.1]{Image}
  \caption{eka citra}
\end{figure}

\begin{table}[htb]
  \centering
  \begin{tabular}[htb]{|c|c|c|}
\hline
hi & ha & hi\\
\hline
hi & ha & hi\\
\hline
  \end{tabular}  \caption{eka taktA}
  \label{tab:2}
\end{table}


\chapter{kAhItarI}
ethe bebela pyAkejamaDIla i.mgrajI BA.sA vAparUna  lihI.nyAcA prayatna
karatoya. matra kAhI sAdhya hota nAhIye.

 \begin{enumerate}
\item
\begin{verbatim} 
\foreignlanguage{english}{This text should be in English}
\end{verbatim}
vAparUna he miLateya,\\
\foreignlanguage{english}{This text should be in English}
\item 
\begin{verbatim}
\selectlanguage{english}{This text should be in English}
\end{verbatim}
vAparUna he miLateya,\\
\selectlanguage{english}{This text should be in English}
\item
\begin{verbatim}
\begin{otherlanguage}{english}
This text should be in English\end{otherlanguage}
\end{verbatim}
vAparUna he miLateya,\\
\begin{otherlanguage}{english}
This text should be in English
\end{otherlanguage}
\end{enumerate}

\chapter{"sere}
\label{cha:sere}

\begin{enumerate}
\item eTe bebela vAparale tarI i.mgrajI vA itara bhA.seta kase
  lihAyace hA pra''sna ajuna su.talA nAhIye.
\item inara lis.ta a, ba, ca, da, a"sIca ye.nare, kAra.na
  devanAgarIsa.thI ajuna k, kha, ga, ... a"sI inara lis.ta
  .devhalapararsanIca .tAkalI nahiye.
\item inara lis.ta prama.neca \verb+\frontmatter+ A.ni
  \verb+\backmatter+ maDila pAna.mce kramA.mka romana Aka.dyA.mce
  devanAgarI Ba.sA.mtara mhanu.naca ye.naret. tyAla kAhI upAya nAhI
\item appara kesa ril~aks kelI kI \verb+\today+ bebela hi.mdIsA.thI
  cAlate. matra bebela maDe ajuna mara.thI nahIye.
\item jhedneka vAgnerane pA.thavalele \verb+xindy+ vAparun pahAyace
  Ahe.
\item eka uttama bAba hI kI \verb+mapping=devanagarinumerals+
  karuna .teka fAIla maDe yuniko.da devanAgarI .tAkUna lihItA yete! he
  phAraca uttama Ahe!
\end{enumerate}


\backmatter
\appendix
\chapter{a}
iTe eka pari"sI.s.ta surU hote.

\listoffigures
\listoftables

\end{document}

Once you give the information \author, \title and \date the \maketitle produces this title page. The command \today for date produces a date in Hindi and not in other language, neither Sanskrit nor Marathi nor Nepali. Babel does not support Marathi yet.

Using Babel I have chosen Hindi as the main language of the text. The table of contents produces name विषय सूची, if \renewcommand{\contentsname}{anukrama.nikA}is not used. विषय सूची is fine for Marathi (as well as Nepali, if think!). But I want to use the work अनुक्रमणिका. The renewed command above does it for me. One had to run XeLaTeX twice or thrice to generate the index properly. LaTeX cannot produce detailed index in a single run.

Following is the first chapter. If you use \chapter*{prastavanA} in the front matter, it would produce the preface page. Using \chapter{eka} in the main matter gives following.  \renewcommand{\chaptername}{dha.dA} makes the name of a chapter appear as "धडा" a standard word for Chapter in Marathi. Changing dha.dA in the command to aDyAya would say अध्याय instead of धडा or pA.tha would yield पाठ. The listing is a problematic issue. For the first list, LaTeX counts properly in devanagari. But for inner lists, it simply translated the standard English innerlist numbers to devanagari. We count the second list as क, ख, ग, घ, च, ... or अ, आ, इ, ई, ... but here it counts as अ =a, ब =b, च =c, द् =d, ... The third list translation of roman counting numbers i, ii, iii,... I contacted the developers, but they replied that these lists we not added at all! They are like to add them now.


Here is an example of graphics and tables. As you can see, the name of the table and graphics as well as their counting appears in devanagari. See the preamble to observe how it is done.

Here I tried using English as a foreign language and I failed! I don't feel comfortable with Babel yet.  I shall try to fix this problem. If you come up with a solution please post here.
In the backmatter, adding \chapter after \appendix stops counting the chapter name, removes the word Chapter (here धडा) and makes only the title visible. Here is an appendix. Appendices are counted differently in the index.

\listoffigures and \listoftables is customized to produce चित्रांची यादी and तक्यांची यादी respectively. For Hindi the default is set but you may modify it to चित्रों की सूची and तालिकांओ की सूची.


Some remarks:

  1. In the Preamble, change \setmainfont [Script=Devanagari, Mapping=velthuis]{Sanskrit 2003} to \setmainfont [Script=Devanagari, Mapping=devanagarinumerals]{Sanskrit 2003}. Then remove all the text inside document. That is, write nothing between the commands \begin{document} and \end{document}. Then, use a unicode Devanagari keyboard and write and input in Devanagari. Then XeLaTeX it and see that LaTeX can process Devanagari input! This is cool. This would help me LaTeXify most of my writing. This is a very valuable feature of XeLaTeX. But use a good editor like Emacs or Aquamacs. Some editors might not like your input and give you errors. For example, on my machine, Aquamacs gives a nice output (its a sturdy editor) but TeXShop confuses!
  2. Devanagari LaTeX is unable to count nested lists as seen above. It does the same error for counting the pages in frontmatter and the backmatter. But these things need to be changed by the Developers. I do not know any other solution, at the moment.
  3. The command \today give only Hindi output. One of the developer told me that Polyglossia package can handle marathi, but I could not make it work for Marathi!
In the next article I shall post a code for LaTeX file and discuss few differences about XeLaTeX and LaTeX files.

                                            
                                                                    ∆  ∆






5 comments:

  1. windows cha compiler sapadla. CTAN chya website var tyanni BIN folder madhye thevla ahe. Tyat apan pahije te output filename deu shakto :)

    ReplyDelete
  2. Hi Rohit,

    Nice article. You mention two problems in the article. I am using XeLaTeX for a few days. Here are proposed solutions. I am sure experts can come up with better solutions. You might have already solved the problems.

    1. For \textup problem. Add the following code in the preamble:
    \newfontfamily\latinfont[Script=Latin]{FreeSerif}
    \newcommand\eng[1]{{\latinfont #1}}

    Then use it in the file:
    \[
    \int f(x)\, \textup{\eng{d}}(x) = \phi^{k(t)^2}
    \]

    2. For beamer problem. It seems the problem occurs because beamer uses sans serif font. Add the following code immediately after the \documentclass line.
    \usefonttheme{serif}
    Then you can create beamer in Devanagari.

    ReplyDelete
    Replies
    1. Hi Rahul! Thank you for the suggestion.
      I shall try them and update the post accordingly.
      I overcame this problem by using Polyglossia. This is a babel package for XeLaTeX and I think that this is the best one to write indic languages.

      Delete
    2. Hi Rahul! An update is as follows: I have totally shifted to XeLaTeX and package polyglossia for writing devanagari documents. This combination works great. Polyglossia allows to choose languages, like Babel; so one may put marathi as language of the document, then it allows hyphenation patters for marathi, many titles are translated directly so one need not write (re)newcommands often. This package explain why the issue of math environment was appearing above. The reason is that, by default, math environment does not allow non-english characters; whereas the above use of XeTeX forces the contrary! In polyglossia one cannot use devanagari charaters inside math directly, however, \textup and \textnormal behave well.

      Thank you for the usefontheme command. Untill very recent, I did not realize that most of the devanagari fonts used are serif. However, though I have not searched deeply, even san-serif devanagari fonts need the serif theme. Which is strange, unless technically these san-serif fonts are serif!

      Delete