I'm trying to set up a buffer-local behavior so that optionally, only after I execute the babel section, Org exports to LaTeX asynchronously after each save. I tried the following:

(defun kdm/org-save-and-export ()
  (org-latex-export-to-latex t))

(add-hook 'after-save-hook 'kdm/org-save-and-export)

But it creates file.tex.tex.tex.tex indefinitely. How do I modify this to only save 1x, and be a buffer-local change?

2 Answers 2


Probably, Emacs is creating a file.tex buffer and then saving it, triggering the after-save-hook again indefinitely. Try checking major-mode before calling the export function:

(defun kdm/org-save-and-export ()
  (if (eq major-mode 'org-mode)

(add-hook 'after-save-hook 'kdm/org-save-and-export nil t)
  • Thanks. Works, although the async doesn't seem to work, so I removed t from org-latex-export-to-latex.
    – mankoff
    Commented Mar 8, 2015 at 14:52
  • I edited your answer to make the hook buffer-local.
    – mankoff
    Commented Mar 8, 2015 at 15:47
  • I tried the above solution, it didn't take any effect, no export taking place.
    – Yu Shen
    Commented Aug 30, 2017 at 18:06

I made a little edit since it was a pain to have every org file i saved be exported to latex. This way it exports every time is saved with the condition it was exported manually at least once, this also works for other file extensions if you edit the ".tex" part

(defun kdm/org-save-and-export ()
(if (and (eq major-mode 'org-mode)
    (ido-local-file-exists-p (concat (file-name-sans-extension (buffer-name)) ".tex")))

(add-hook 'after-save-hook 'kdm/org-save-and-export)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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