44

Assume that I have in a certain directory several org-mode files: foo1.org, foo2.org, etc. I would like to have a script (maybe a makefile) that I could invoke something like

$ generate-pdfs

and foo1.pdf, foo2.pdf, etc. will be generated.

I thought that something like emacs --batch --eval <MAGIC> is a good start, but I don't know the magic.

A solution that is solely inside emacs could be of interest as well.

3 Answers 3

53

As you said, Emacs has the --batch option to perform operations with Emacs from the shell. In addition to that, you can use the -l flag to load Emacs Lisp code from a file and execute it, and the -f flag to execute a single Lisp function.

Here is a basic example, which exports a single org-mode file to HTML:

emacs myorgfile.org --batch -f org-html-export-to-html --kill

Perhaps you want something more advanced like exporting/publishing a full org-mode project. I do not have sample code for that, but it should not be too complicated.

I also have a sample Makefile I wrote some time ago to export all org-mode files in the directory to HTML (and also copy the HTML files to another directory):

OUT_DIR=/some/output/dir/html
# Using GNU Make-specific functions here
FILES=$(patsubst %.org,$(OUT_DIR)/%.html,$(wildcard *.org))

.PHONY: all clean install-doc

all: install-doc

install-doc: $(OUT_DIR) $(FILES)

$(OUT_DIR):
        mkdir -v -p $(OUT_DIR)

%.html: %.org
        emacs $< --batch -f org-html-export-to-html--kill

$(OUT_DIR)/%.html: %.html
        install -v -m 644 -t $(OUT_DIR) $<
        rm $<

clean:
        rm *.html

EDIT:

With Org-mode 8 and the new export engine the function for HTML export has changed.

To make the previous examples work with Org 7 or older, replace org-html-export-to-html with org-export-as-html.

4
  • 1
    The --batch implies -q that in turn doesn't load my .emacs. How can I tell emacs to load a specific init file?
    – Dror
    Feb 28, 2014 at 11:23
  • 1
    I guess you could use --batch -l my_init_file.el -f org-latex-export-to-pdf which would load your init file and then call the export function.
    – florianlh
    Feb 28, 2014 at 11:30
  • 2
    Also the shorthand that I used, having the target file as the first argument to emacs, is not the cleanest way to do it. The correct way to do it would rather be emacs --batch --visit myorgfile.org -f […]. This would allow you to process several files with the same command (by adding other --visit options in the command)
    – florianlh
    Feb 28, 2014 at 11:33
  • 1
    emacs -q disabled the loading of newer version of org (which I installed using ELPA). Therefore, I had to use the -l option and load an init file which had (package-initialize). This allowed to use the newer version of org in batch mode.
    – Dror
    Mar 1, 2014 at 22:39
3

I expect to publish (by the end of this week-end) OrgMk, a suite of Makefile and standalone Bash scripts (usable as well under Cygwin) just to do that! Even more: generation of HTML, Ascii, Beamer, etc.

You'll find it on my GitHub account: https://github.com/fniessen/ (where I already have Emacs configuration files, color themes and other stuff such as an Org Babel refcard -- in progress).

0
2

Mark a few org files in dired and call this:

(defun dired-org-to-pdf ()
  (interactive)
  (mapc
   (lambda (f)
     (with-current-buffer
         (find-file-noselect f)
       (org-latex-export-to-pdf)))
   (dired-get-marked-files)))

If you know what async is, wrap the call as it can take a while.

update:

Here's a version that combines the awesome dired approach with the lame other one:)

(defun dired-org-to-pdf ()
  (interactive)
  (let ((files
         (if (eq major-mode 'dired-mode)
             (dired-get-marked-files)
           (let ((default-directory (read-directory-name "dir: ")))
             (mapcar #'expand-file-name 
                     (file-expand-wildcards "*.org"))))))
    (mapc
     (lambda (f)
       (with-current-buffer
           (find-file-noselect f)
         (org-latex-export-to-pdf)))
     files)))
2
  • +1. But I want something more automatic. Maybe this function can ask for a directory and then process all .orgs in the given directory?
    – Dror
    Feb 27, 2014 at 15:31
  • It's doable. Although for me dired is the default state. Do you know that you can mark all org files in dired with *%org?
    – abo-abo
    Feb 27, 2014 at 15:35

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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