# Hindi/Devanagari presentations using orgmode, R, latex and beamer

March 11, 2012
By

(This article was first published on V. » R, and kindly contributed to R-bloggers)

I recently had to prepare a beamer presentation in hindi/devanagari. I usually use emacs-orgmode  with a lot of R source code embedded in it to prepare my beamer presentations. To adapt the entire setup to work with devanagari, this is what I needed to do.

1. Make orgmode export to latex using xetex rather than pdflatex.Instructions on how to make orgmode export through xetex are here.
2. I have following in my .emacs (taken straight from the website mentioned above)

```(require 'org-latex)
(setq org-export-latex-listings t)

;; Originally taken from Bruno Tavernier: http://thread.gmane.org/gmane.emacs.orgmode/31150/focus=31432
;; but adapted to use latexmk 4.20 or higher.
(defun my-auto-tex-cmd ()
"When exporting from .org with latex, automatically run latex,
pdflatex, or xelatex as appropriate, using latexmk."
(let ((texcmd)))
;; default command: oldstyle latex via dvi
(setq texcmd "latexmk -dvi -pdfps -quiet %f")
;; pdflatex -> .pdf
(if (string-match "LATEX_CMD: pdflatex" (buffer-string))
(setq texcmd "latexmk -pdf -quiet %f"))
;; xelatex -> .pdf
(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(setq texcmd "latexmk -pdflatex=xelatex -pdf -quiet %f"))
;; LaTeX compilation command
(setq org-latex-to-pdf-process (list texcmd)))

;; Specify default packages to be included in every tex file, whether pdflatex or xelatex
(setq org-export-latex-packages-alist
'(("" "graphicx" t)
("" "longtable" nil)
("" "float" nil)))

(defun my-auto-tex-parameters ()
"Automatically select the tex packages to include."
;; default packages for ordinary latex or pdflatex export
(setq org-export-latex-default-packages-alist
'(("AUTO" "inputenc" t)
("T1"   "fontenc"   t)
(""     "fixltx2e"  nil)
(""     "wrapfig"   nil)
(""     "soul"      t)
(""     "textcomp"  t)
(""     "marvosym"  t)
(""     "wasysym"   t)
(""     "latexsym"  t)
(""     "amssymb"   t)
(""     "hyperref"  nil)))

;; Packages to include when xelatex is used
(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(setq org-export-latex-default-packages-alist
'(("" "fontspec" t)
("" "xunicode" t)
("" "url" t)
("" "rotating" t)
("american" "babel" t)
("babel" "csquotes" t)
("" "soul" t)
("xetex" "hyperref" nil)
)))

(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(setq org-export-latex-classes
(cons '("article"
"\\documentclass[11pt,article,oneside]{memoir}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
org-export-latex-classes))))

(setq org-latex-to-pdf-process
'("xelatex -interaction nonstopmode -output-directory %o %f"
"xelatex -interaction nonstopmode -output-directory %o %f"
"xelatex -interaction nonstopmode -output-directory %o %f"))
```

Having done this, I need the following line in my org file header to tell org that it should use xetex instead of pdflatex.

```#+LATEX_CMD: xelatex
```
3. xetex can use ttf fonts. We need to specify a font that has devanagari characters. On debian, ‘apt-get install ttf-devanagati-fonts’ installs several devanagari fonts. I could use them but my problem was that I could not find a font of which I liked both the devanagari and the roman characters. I wanted to use different fonts for devanagari and roman letters and, thus, needed a way of telling xetex to use different fonts for roman and devanagari characters. The solution for this, provided by Zdenek Wagner on the xetex mailing list, lies in installing and using ucharclasses.
4. After ucharclasses was installed, this was achieved by adding following lines to the header of the org file.

```#+LATEX_HEADER: \usepackage{fontspec};
## The following lines allow me to use Garamond for roman letters and Sarai for devanagari letters.
```
5. Type devanagari in emacs. Emacs has fantastic support for this.
6. ```C-x C-m C-\ devanagari- TAB (pick whatever, I like devanagari-itrans)
C-h C-\ (gives you the keymap)
C-\ (to switch to English again)
C-\ (to switch to devanagari)
```

(thanks Jambunathan K. and Suvayu Ali for pointing this out on orgmode mailing list)

7. Modify my R src codes to produce text in devanagari rather than in english. In most cases, I only had to rename factor levels and variables names in the final tables, so that output tables had text in devanagari. orgmode and xetex handled the rest.
8. The following line in file header replaces “Table” in Table caption numbering with तालिका

```#+LATEX_HEADER: \renewcommand{\tablename}{तालिका}
```
9. For graphs to have text in devanagari, I had to use the tikz device in R rather than the standard pdf device to create the pdfs. tikz device produces code that can be understood by the TikZ graphics language. All text in a graphic output with the tikz() function can be typeset by LaTeX and therefore will match whatever fonts are currently used in the document. The output of tikz device is a tex file that, using xetex and tikz, produces the desired graphic. I include that tex file into the beamer presentation.
10. Here is sample code that achieves this:

```#+CAPTION: ग्राफ का नमूना
#+BEGIN_CENTER
#+Latex: {\footnotesize
\include{lorcurve}
#+LATEX: }
#+END_CENTER
#+BEGIN_SRC R :results output silent
require(tikzDevice)
options(tikzDefaultEngine=’xetex’)
tikz(file="lorcurve.tex",width=6,height=3,engine="xetex",standAlone = FALSE)
c(1:20)->क
क^2->ख
plot(क~ख)
dev.off()
#+END_SRC
```