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

March 11, 2012

(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)))
    (add-hook 'org-export-latex-after-initial-vars-hook 'my-auto-tex-cmd)
    ;; 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"
                            ("\\section{%s}" . "\\section*{%s}")
                            ("\\subsection{%s}" . "\\subsection*{%s}")
                            ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                            ("\\paragraph{%s}" . "\\paragraph*{%s}")
                            ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
    (add-hook 'org-export-latex-after-initial-vars-hook 'my-auto-tex-parameters)
    (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.
    #+LATEX_HEADER: \usepackage[Devanagari]{ucharclasses}
    #+LATEX_HEADER: \setmainfont{Garamond}
    #+LATEX_HEADER: \setTransitionTo{Devanagari}{\fontspec[Script=Devanagari]{Sarai}}
  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: ग्राफ का नमूना
    #+Latex: {\footnotesize
    #+LATEX: }
    #+BEGIN_SRC R :results output silent
          tikz(file="lorcurve.tex",width=6,height=3,engine="xetex",standAlone = FALSE)

To leave a comment for the author, please follow the link and comment on their blog: V. » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)