I'm writing a paper using org-mode, using the LaTeX export function to generate the .tex file I need to submit to the journal.

How can I run an external shell command every time I to the C-c C-e l l for LaTeX export? I need to apply a patch to the generated .tex file.


You might do this by using Org's publishing functions, and setting up one for your project. Take a look in particular at the setting publishing-function, which allows you to specify an arbitrary elisp function by which an org file within a given project is exported.

The publishing function might do a standard export to latex then run a shell command on the resulting file like this:

(defun publish-latex-and-patch (plist filename pub-dir)
  "Export a latex file then patch it by reversing lines"
  (let ((outfile
         (org-publish-org-to 'latex filename ".tex" plist pub-dir)))
    (shell-command (format "tac %s > %s-patched.tex"
                           (file-name-sans-extension outfile)))))

And you would then export your file by doing C-c C-e P f ("publish this file"). Obviously replace tac with your patching command, and exercise hygiene (e.g. escaping) if passing untrusted input to the shell.

There are also many org-export hooks, but they are more designed to modify specify elements within the parse tree during the export process.

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.