[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Example of Make file for Org Mode LaTeX to pdf

From: Nick Dokos
Subject: Re: [O] Example of Make file for Org Mode LaTeX to pdf
Date: Mon, 07 Mar 2016 09:20:04 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Here's a simple (GNU make) makefile to export something in batch mode:

--8<---------------cut here---------------start------------->8---
%.html: %.org
        emacs -batch -l batch-export.el --eval "(batch-org-export-as 'html)" $<

%.pdf:  %.tex
        latexmk --shell-escape -pdf -xelatex $<

%.tex: %.org
        emacs -batch -l batch-export.el --eval "(batch-org-export-as 'latex)" $<
--8<---------------cut here---------------end--------------->8---

Starting from foo.org, say

  make foo.html foo.pdf

to produce the indicated exports.

I don't remember where I picked up the batch-org-export-as function, but
whoever wrote it, thanks! You might be able to simplify it: I've been
carrying it around unchanged for a couple of years, so there may be
simpler ways to do things nowadays, but AFAIK it works well with org
8.x. Here's the bach-export.el file (obviously the path in the second
line will need to be changed to suit your circumstances - I'm not sure
if the (require 'org-loaddefs) needs to be changed if e.g. you are using
org from ELPA - you might have to add a (package-initialize) too in this

--8<---------------cut here---------------start------------->8---
;;; -*- mode: emacs-lisp -*-
(add-to-list 'load-path (expand-file-name "~/src/emacs/org/org-mode/lisp"))

(add-to-list 'auto-mode-alist '("\\.\\(org\\|org_archive\\|txt\\)$" . org-mode))

(setq org-export-backends '(html latex))

(require 'org-loaddefs)

(defun org-export-file-to (backend src dest)
    (insert-file-contents src)
    (org-export-to-file backend dest)))

(setq file-suffixes
      '((html . ".html")
        (latex . ".tex")
        (pdf . ".pdf")))

(defun org-export-dest (backend f)
  (concat (file-name-sans-extension f) (cdr (assoc backend file-suffixes))))

(defun batch-org-export-as (backend &optional noforce)
  "Run `org-export-as' with the given backend  on the files remaining on the 
command line.
Use this from the command line, with `-batch'; it won't work in
an interactive Emacs.  Each file is processed even if an error
occurred previously.  For example, invoke \"emacs -batch -f
batch-byte-compile $emacs/ ~/*.el\".  If NOFORCE is non-nil,
don't recompile a file that seems to be already up-to-date."
  ;; command-line-args-left is what is left of the command line, from
  ;; startup.el.
  (defvar command-line-args-left)       ;Avoid 'free variable' warning
  (if (not noninteractive)
      (error "`batch-org-export-as' is to be used only with -batch"))
  (let ((error nil))
    (while command-line-args-left
      (if (file-directory-p (expand-file-name (car command-line-args-left)))
          ;; Directory as argument.
          (let (source dest)
            (dolist (file (directory-files (car command-line-args-left)))
              (if (and (string-match emacs-lisp-file-regexp file)
                       (not (auto-save-file-name-p file))
                       (setq source
                             (expand-file-name file
                                               (car command-line-args-left)))
                       (setq dest (org-export-dest backend source))
                       (file-exists-p dest)
                       (file-newer-than-file-p source dest))
                  (if (null (org-export-file-to backend source dest))
                      (setq error t)))))
        ;; Specific file argument
        (let* ((source (car command-line-args-left))
               (dest (org-export-dest backend source)))
          (if (or (not noforce)
                  (or (not (file-exists-p dest))
                      (file-newer-than-file-p source dest)))
            (if (null (org-export-file-to backend (car command-line-args-left) 
                (setq error t)))))
      (setq command-line-args-left (cdr command-line-args-left)))
    (kill-emacs (if error 1 0))))

--8<---------------cut here---------------end--------------->8---


reply via email to

[Prev in Thread] Current Thread [Next in Thread]