0

I had latex preview working with inlineimages as in the following .org file:

#+STARTUP: latexpreview
#+STARTUP: inlineimages

$A \to B$

The latex formula is correctly changed into a png image as follows:

correct

However, once I start to add a bibtex line, the image preview breaks down. With this slightly changed .org file:

#+STARTUP: latexpreview
#+STARTUP: inlineimages

#+LATEX_HEADER: \addbibresource{test.bib}

$A \to B$

, org-mode generates an incorrect image for the same formula with the name of the bibtex file (test.bib) in it:

enter image description here

Does anyone know what's happening here and how to fix it?

By the way, this is with the default Emacs 26 in Ubuntu 20.04. And I used the setup of this tutorial. When the .org file is exported to ODT format, the formula are actually correct, and bibliography (not shown in this minimal example) also works well. It's just the preview in emacs that went wrong (for every formula).

  • I can't reproduce it with recent Org mode (Org mode version 9.3.6 (release_9.3.6-631-g48b223) so it is likely a bug in your version. Please add the Or mode version string (M-x org-version RET will provide that) to your question. – NickD Jun 2 at 0:49
  • Actually I can reproduce it with LATEX_HEADER: you are supposed to use LATEX_HEADER_EXTRA if you don't want it to be processed for previews, but even that had a bug. See my answer below. – NickD Jun 2 at 3:41
1

You need to use LATEX_HEADER_EXTRA instead of LATEX_HEADER. You also need Org mode release 9.1 or later. Be sure to clean out the ltximg/ subdirectory in order to regenerate clean preview images after you apply the necessary fixes.

The documentation says

The LaTeX export back-end appends values from ‘LATEX_HEADER’ and ‘LATEX_HEADER_EXTRA’ keywords to the LaTeX header. The docstring for ‘org-latex-classes’ explains in more detail. Also note that LaTeX export back-end does not append ‘LATEX_HEADER_EXTRA’ to the header when previewing LaTeX snippets (see *note Previewing LaTeX fragments::).

Pre-9.1 releases contained a bug that was fixed with the following commit:

commit e903288e5080775cbd4d87c69deeba3268cda5c1
Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Date:   Sun Jun 25 09:39:32 2017 +0200

    ox-latex: Fix LATEX_HEADER_EXTRA keyword

    * lisp/ox-latex.el (org-latex-make-preamble): Do not include
      LATEX_HEADER_EXTRA keywords' contents when previewing a LaTeX
      fragment.

    Reported-by: Mario Román <mromang08@gmail.com>
    <http://lists.gnu.org/archive/html/emacs-orgmode/2017-06/msg00477.html>

It was fixed in release 9.1 (I'm not sure when it was introduced). The easiest thing to do is upgrade Org mode. You can get the current stable version from ELPA or check these instructions from the Org mode site.

Or you can apply the following patch:

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index f11a8a63a..ec4b49585 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1623,15 +1623,15 @@ non-nil, only includes packages relevant to image generation, as
 specified in `org-latex-default-packages-alist' or
 `org-latex-packages-alist'."
   (let* ((class (plist-get info :latex-class))
-    (class-options (plist-get info :latex-class-options))
-    (header (nth 1 (assoc class (plist-get info :latex-classes))))
     (class-template
      (or template
-         (and (stringp header)
-          (if (not class-options) header
-            (replace-regexp-in-string
-             "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
-             class-options header t nil 1)))
+         (let* ((class-options (plist-get info :latex-class-options))
+            (header (nth 1 (assoc class (plist-get info :latex-classes)))))
+       (and (stringp header)
+            (if (not class-options) header
+              (replace-regexp-in-string
+           "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
+           class-options header t nil 1))))
          (user-error "Unknown LaTeX class `%s'" class))))
     (org-latex-guess-polyglossia-language
      (org-latex-guess-babel-language
@@ -1644,7 +1644,9 @@ specified in `org-latex-default-packages-alist' or
     snippet?
     (mapconcat #'org-element-normalize-string
            (list (plist-get info :latex-header)
-             (plist-get info :latex-header-extra)) ""))))
+             (and (not snippet?)
+                  (plist-get info :latex-header-extra)))
+           ""))))
       info)
      info)))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.