Abstract

This is a fix for a problem regarding the org-preview-latex-fragment
function and its associated org-preview-latex-process-alist item
'dvipng,' which is broken by imported packages from
org-latex-packages-alist which are incompatible with the 'latex'
command

Context

Relevant Org-LaTex Configurations

org-latex-packages-alist

;;Latex export commands
;;(add-to-list 'org-latex-packages-alist '("" "minted"))
(add-to-list 'org-latex-packages-alist '("margin=1in" "geometry"))
(add-to-list 'org-latex-packages-alist '("" "fancyhdr"))
(add-to-list 'org-latex-packages-alist '("" "ltablex"))
(add-to-list 'org-latex-packages-alist '("" "fontspec" t ("xelatex")))
(add-to-list 'org-latex-packages-alist '("" "xcolor"))
(add-to-list 'org-latex-packages-alist '("" "multicol"))
(add-to-list 'org-latex-packages-alist '("" "xeCJK"))

LaTex Compiler

(setq org-latex-compiler "xelatex")

Overridding Keybind

This keybind is defined in the org-mode startup hook

(local-set-key (kbd "C-c C-x C-l") 'org-latex-preview-fix)

org-preview-latex Variables

(setq org-preview-latex-default-process 'dvipng)
(setq org-preview-latex-process-alist
      '(
       (dvipng
        :programs ("latex" "dvipng")
        :description "dvi > png"
        :message "you need to install the programs: latex and dvipng."
        :image-input-type "dvi"
        :image-output-type "png"
        :image-size-adjust (1.0 . 1.0)
        :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
        :image-converter ("dvipng -D %D -T tight -o %O %f")
        )
       )
      )

Body

The alternatives to the dvipng option in the
org-preview-latex-process-alist provided less than satisfactory
results, leaving excessive whitespace within the linked image
containing mathematics. So I wanted to pursue using dvipdf.

However, dvipng does not support PDF nor XDV which are the two
formats the my LaTex compiler outputs (xelatex). So I am forced to
use the 'latex' and 'dvipng' commands within the dvipng item of the
org-preview-latex-process-alist.

This is where my fix starts to come in. When org mode does the small
export of the latex fragment it would include the contents of
org-latex-packages-alist in the LaTex headers. Some packages (namely
'fontspec,' used for xelatex) would lead to a failure to create the
dvi file when the .tex file was compiled with the necessary
'latex' command.

To alleviate this problem I designed this wrapper function which
sets org-latex-packages-alist to nil just before executing the
org-latex-preview function.

;;Latex Preview Helper Function
(defun org-latex-preview-fix (&optional ARG)
  "A wrapper function that stops org-preview from including
  packages that break latex"
  (interactive nil)
  (let ((org-latex-packages-alist . nil))
    (org-latex-preview ARG)
    )
  )

Now I can preview LaTex fragments without having org-mode yell at
more and not work.

Considerations

This fix is not perfect. For if you are using the xelatex compiler
and you wish to use dvipng (which in my opinion is the only option
in regard to inline maths, because of the 'tight' capabilities) with
maths that include Japanese, Chinese or other Unicode characters
requiring the 'fontspec' package, you will not be able to create
fragments.

This specific Issue however is on the conversion application side
however and from this point of view seems unfixable from org-mode's
standpoint.

Salutations

Either way, here is a relatively okay fix for a problem that I
encountered in my daily work.

Cheers!

Gabriel S. X. Smith