xelatex in Org-mode 8+
Since I’ve switched to spacemacs I
started moving more and more of my workflows in Emacs,
org-mode specifically. One of my big items (kept
in org-mode) is actually moving this blog from markdown to .org
format, but that is not the topic of this post. The topic is how to use
xelatex from Org-mode’s export, or even more precise, how to be able to
switch between different processors per file basis.
I had a need to export some .org
files to PDF, using xelatex. I came
across this nice post from Kieran Healy, but it didn’t work, as I’m using Org-mode
8.3.
Luckily, after some searching through the documentation and after stumbling to this post, I’ve came up with a really neat solution.
First and foremost, I’m going to use time-saving latexmk as the default builder for TeX/LaTeX files:
(latex :variables
latex-build-command "LatexMk"
latex-enable-auto-fill t)
Next, the idea is that I place #+LATEX_CMD: processor
in an .org
file, and during the export Org-mode should pick up the right processor.
I also wanted to chose between pdflatex and xelatex, so I’ve created
this:
;; Default packages included in every tex file, pdflatex or xelatex
(setq org-latex-packages-alist
'(("" "graphicx" t)
("" "longtable" nil)
("" "float" nil)))
;; source: https://lists.gnu.org/archive/html/emacs-orgmode/2013-06/msg00240.html
(defun my-auto-tex-cmd (backend)
"When exporting from .org with latex,
automatically run latex, pdflatex, or xelatex as appropriate,
using latexmk."
(let ((texcmd))
(setq texcmd "latexmk -pdf %f")
(if (string-match "LATEX_CMD: pdflatex" (buffer-string))
(progn
(setq texcmd "latexmk -pdf -pdflatex='pdflatex -file-line-error --shell-escape -synctex=1' %f")
(setq org-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)))))
(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(progn
(setq texcmd "latexmk -pdflatex='xelatex -file-line-error --shell-escape -synctex=1' -pdf %f")
(setq org-latex-default-packages-alist
'(("" "fontspec" t)
("" "xunicode" t)
("" "url" t)
;; ("" "rotating" t)
;; ("" "memoir-article-styles" t)
;; ("american" "babel" t)
;; ("babel" "csquotes" t)
;; ("" "listings" nil)
("svgnames" "xcolor" t)
("" "soul" t)
("xetex, colorlinks=true, urlcolor=FireBrick, plainpages=false, pdfpagelabels, bookmarksnumbered" "hyperref" nil)
))
(setq org-latex-classes
(cons '("memarticle"
"\\documentclass[11pt,oneside,article]{memoir}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
org-latex-classes))))
(setq org-latex-pdf-process (list texcmd))))
(add-hook 'org-export-before-parsing-hook 'my-auto-tex-cmd)
The above code also does a really nice setup of default packages and classes, to make exports look really nice.