25
votes

I know this is kind of minor, but it's been bugging me. I'm using Org-mode for a project and I tend to export to either PDF or HTML rather frequently and it leaves my directory littered with PDF, Tex, and HTML files. Is there a way to have Org-mode export to another location, perhaps a subdirectory called ./exports?

6
See emacs.stackexchange.com/a/7989/8541 for a clean solution that exports to a subfolder.Mark

6 Answers

22
votes

In addition to the use of publishing by modifying your org-publish-project-alist variable as @user1248256 suggested, you can directly specify the org-export-publishing-directory variable within your file:

#+bind: org-export-publishing-directory "./exports"

* This is a test headline
Some text here.  This should be exported to the "./exports" directory.

Upon export it will be placed in the "exports" directory, but only if that directory exists. If it does not exist, you will get an error message in the console.

16
votes

The original question referred to exporting of org-files, while most answers above actually have to do with publishing, which is a different concept.

I believe the best way to solve the problem posed by the OP is to add the following to your emacs initialization file (.emacs):

(defadvice org-export-output-file-name (before org-add-export-dir activate)
  "Modifies org-export to place exported files in a different directory"
  (when (not pub-dir)
      (setq pub-dir "exported-org-files")
      (when (not (file-directory-p pub-dir))
       (make-directory pub-dir))))

PS:

  1. I realize a 5 year old question might no longer be relevant to the OP, but hopefully people searching for similar stuff will benefit from this answer.

  2. This is a slight modification of a code snippet found in http://rwx.io/posts/org-export-configurations/

  3. The original solution found in the above blog allows for setting up different directories for each exported format. However, if the goal is to avoid having one's directory "littered with PDF, Tex, and HTML files", I think it is best to have only one directory containing exported files of all formats, which is the essence of the modification I offered above.


Edit: The emacs manual (https://www.gnu.org/software/emacs/manual/html_node/elisp/Porting-old-advice.html#Porting-old-advice) states that the defadvice mechanism was made obsolete by the new advice-add. So here is a code snipet with the same effect, using the recommended advice-add:

(defun org-export-output-file-name-modified (orig-fun extension &optional subtreep pub-dir)
  (unless pub-dir
    (setq pub-dir "exported-org-files")
    (unless (file-directory-p pub-dir)
      (make-directory pub-dir)))
  (apply orig-fun extension subtreep pub-dir nil))
(advice-add 'org-export-output-file-name :around #'org-export-output-file-name-modified)

As before, this should be placed in your .emacs file.

6
votes

This probably wasn't possible when the question was first asked, but the simplest solution would be to add the directory to the :EXPORT_FILE_NAME: property:

:PROPERTIES:
:EXPORT_FILE_NAME: exports/<filename>
:END:

Just as in the accepted answer, the directory must exist in order for this to work.

3
votes

I believe you can get that with org-publish. Add to you emacs configuration file something like that:

(setq org-publish-project-alist
  '(("html"
     :base-directory "~/org/"
     :base-extension "org"
     :publishing-directory "~/org/exports"
     :publishing-function org-publish-org-to-html)
    ("pdf"
     :base-directory "~/org/"
     :base-extension "org"
     :publishing-directory "~/org/exports"
     :publishing-function org-publish-org-to-pdf)
    ("all" :components ("html" "pdf"))))

Eval this expression (or restart emacs), press C-c C-e X at org-mode, then choose a project from a list.

You can see more information at http://orgmode.org/worg/org-tutorials/org-publish-html-tutorial.html and http://orgmode.org/manual/Publishing.html#Publishing

2
votes

You have to put the following line at the beginning of your org file :

#+EXPORT_FILE_NAME: PATH/filename

Where PATH is the path to the folder where you want your file to be exported (e.g. ~/exports) and filename the name you want to give to your exported file (e.g. tutorial.html).

0
votes

As stated in the section "Export settings", we can use the EXPORT_FILE_NAME within a file in order to set the output directory. The quote shown below is the relevant part of the documentation

‘EXPORT_FILE_NAME’

The name of the output file to be generated. Otherwise, Org generates the file name based on the buffer name and the extension based on the back-end format.