UP | HOME

How to Customize Emacs for Efficient LaTeX Export using Org-mode

Introduction

Are you looking to enhance your LaTeX export capabilities in Emacs using Org-mode? This guide takes you through how you can optimize the settings in Emacs to get the most out of your LaTeX export in Org-mode.

Step 1: Setting up General Org-mode for LaTeX Export

Emacs Lisp offers diverse functionalities for exporting LaTeX. To enable the Org-mode LaTeX export module, use the following command: require ’ox-latex. This function loads the module that allows Emacs to convert Org-mode files into LaTeX.

(require 'ox-latex)

Next, add the necessary LaTeX packages with the add-to-list commands. These packages provide additional features for LaTeX documents and are added to Org-mode with the following lines:

(setq org-latex-packages-alist '(("" "listings")
                                 ("" "booktabs")
                                 ("AUTO" "polyglossia" t ("xelatex" "lualatex"))
                                 ("" "grffile")
                                 ("" "unicode-math")
                                 ("" "xcolor")))

Setting up the command for processing LaTeX files into PDFs in Org-mode is straightforward:

(setq org-latex-pdf-process '("latexmk -xelatex -shell-escape -quiet -f %f"))

Moreover, the default LaTeX compiler can be set to XeLaTeX using setq org-latex-compiler “xelatex”.

(setq org-latex-compiler "xelatex")

Additionally, for an enhanced representation of your source code listings and tables, enable the use of the listings and booktabs packages by setting setq org-latex-listings t and setq org-latex-tables-booktabs t.

(setq org-latex-listings t)
(setq org-latex-tables-booktabs t)
(setq org-latex-images-centered t)

To customize the look of source code listings, you can set options using setq org-latex-listings-options.

(setq org-latex-listings-options
      '(("basicstyle" "\\ttfamily")
        ("showstringspaces" "false")
        ("keywordstyle" "\\color{blue}\\textbf")
        ("commentstyle" "\\color{gray}")
        ("stringstyle" "\\color{green!70!black}")
        ("stringstyle" "\\color{red}")
        ("frame" "single")
        ("numbers" "left")
        ("numberstyle" "\\ttfamily")
        ("columns" "fullflexible")))

Finally, set the input encoding for LaTeX exports to UTF-8 with setq org-latex-inputenc-alist ’((\“utf8\” . \“utf8x\”)).

(setq org-latex-inputenc-alist '((\"utf8\" . \"utf8x\")))

Step 2: Define the koma-article LaTeX class

A new LaTeX class, called koma-article, can be defined for use when exporting to LaTeX in Org-mode. The code block with-eval-after-load ’ox-latex … gets executed after the ox-latex package is loaded. It adds a new class definition to org-latex-classes. Set the newly defined koma-article class as the default LaTeX class using setq org-latex-default-class “koma-article”.

(with-eval-after-load 'ox-latex
  (add-to-list 'org-latex-classes
               '("koma-general"
                 "\\documentclass[11pt,captions=tableheading]{scrartcl}
  \\linespread{1.25}
  \\usepackage{fontspec}
  \\defaultfontfeatures{Mapping=tex-text, RawFeature={+zero}}
  \\setmainfont{Noto Sans}[BoldFont=*-Medium,ItalicFont=*-Italic]
  \\setsansfont{Noto Sans}[BoldFont=*-Medium,ItalicFont=*-Italic]
  \\setmonofont{Noto Sans Mono}[BoldFont=*-Medium,Scale=0.8]
  \\usepackage{fancyhdr}
  \\pagestyle{fancy}
  \\fancyhf{}
  \\fancyhead[L]{\\leftmark}  % Left header
  \\fancyhead[R]{\\thepage}  % Right header"
                 ("\\section{%s}" . "\\section*{%s}")
                 ("\\subsection{%s}" . "\\subsection*{%s}")
                 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                 ("\\paragraph{%s}" . "\\paragraph*{%s}")
                 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))

(setq org-latex-default-class "koma-general")
(eval-after-load 'ox '(require 'ox-koma-letter))

Step 3: Integrating into your init.el

Adding this code to your Emacs configuration is a simple three-step process:

  1. Open your Emacs init file (~/.emacs.d/init.el).
  2. Copy the provided Emacs Lisp code.
  3. Paste the code at the end of your init file.

After saving the file and restarting Emacs, these settings will be applied every time Org-mode files are exported to LaTeX.

Remember, you might need to install the necessary LaTeX packages (listings, color, booktabs, polyglossia) and the xelatex compiler and latexmk tool for PDF processing according to your LaTeX setup.

Follow these steps, and you’ll enjoy a more efficient LaTeX exporting experience in Emacs using Org-mode.

Additional Links

Author: Marcus Kammer

Email: marcus.kammer@mailbox.org

Date: Thu, 08 Jun 2023 15:36 +0200

Emacs 29.1.90 (Org mode 9.6.11)

License: CC BY-SA 3.0