Search code examples
unicodeemacslatexorg-mode

Configure org mode to use LuaLaTeX


I'm using emacs' org-mode to typeset notes and export them in pdf via LaTeX. Everything works fine, except when I use Unicode symbols, like in

\begin{equation}
  \left( \frac{P^2}{2m} + V(x,y,z) \right) Ψ = E Ψ
\end{equation}

where I use Ψ. Using #+latex_compiler: lualatex and

(setq org-latex-pdf-process
      '("lualatex -shell-escape -interaction nonstopmode %f"
        "lualatex -shell-escape -interaction nonstopmode %f")) 

I have managed to export with more or less problems to PDF using LuaLaTeX, but the previews of the formulae (C-c C-x C-l) are completely broken for the ones with Unicode symbols.

What changes do I need to make in my emacs configuration to be able to use LuaLaTeX for everything related to LaTeX in org-mode?


Solution

  • Add the following code to your init file (N.B. it adds an element to the value of org-preview-latex-process-alist, so this code must be added after org is loaded - you may also need to (require 'org) or use (eval-after-load 'org ...) to avoid use-before-definition problems):

    ;; lualatex preview
    (setq org-latex-pdf-process
      '("lualatex -shell-escape -interaction nonstopmode %f"
        "lualatex -shell-escape -interaction nonstopmode %f")) 
    
    (setq luamagick '(luamagick :programs ("lualatex" "convert")
           :description "pdf > png"
           :message "you need to install lualatex and imagemagick."
           :use-xcolor t
           :image-input-type "pdf"
           :image-output-type "png"
           :image-size-adjust (1.0 . 1.0)
           :latex-compiler ("lualatex -interaction nonstopmode -output-directory %o %f")
           :image-converter ("convert -density %D -trim -antialias %f -quality 100 %O")))
    
    (add-to-list 'org-preview-latex-process-alist luamagick)
    
    (setq org-preview-latex-default-process 'luamagick)
    

    I had to add a math-enabled font (XITS), so the org file itself looks like this now:

    #+LATEX_HEADER: \usepackage{unicode-math}
    #+LATEX_HEADER: \setmainfont{XITS}
    #+LATEX_HEADER: \setmathfont{XITS Math}
    #+LATEX_HEADER: \setmathfont[range={\mathcal,\mathbfcal},StylisticSet=1]{XITS Math}
    
    * A formula for lua
    \begin{equation}
      \left( \frac{P^2}{2m} + V(x,y,z) \right) Ψ = E Ψ
    \end{equation}
    

    Both export to PDF and preview seem to work.