Emacs Org-mode - Export to another directory?
Asked Answered
A

7

32

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?

Anglia answered 4/3, 2012 at 23:13 Comment(2)
See emacs.stackexchange.com/a/7989/8541 for a clean solution that exports to a subfolder.Dorcus
Does this answer your question? in org-mode, how to specify name of exported file?Reinhard
A
25

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.

Atonal answered 5/3, 2012 at 1:17 Comment(4)
Thanks. This more directly answers my question, I think. There is one odd thing though. If I export to HTML or ASCII, the file goes to ./exports, but if I export to TEX (or PDF) it stays in the current dir. Maybe its something in my config?Anglia
Actually, I just saw today that this question was on the org-mode mailing list - unless that was you that posted?Atonal
Does this work with PDFs now? I read through the mailing list exchange there but it was not clear if the export for pdfs had been fixed.Gynaeco
Oddly enough this setting doesn't work for me. Still exports to the same path as the .org file (default behavior)Luvenialuwana
C
26

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.

Cartouche answered 16/12, 2017 at 23:22 Comment(3)
after adding this around advice, while the file is exported to the pub-dir, it does not use the file name used in a property drawer EXPORT_FILENAME when exporting a subtree. Any idea how to make that work?Prodigy
While this seems to work to export to orgmode, html and LaTeX, exporting to LaTeX and thence pdf does not work because the pdflatex compilation fails.Hyonhyoscine
That advice-add updated snippet worked great for me, including LaTeX/PDF files. The make-directory part is very helpful too.Peggi
A
25

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.

Atonal answered 5/3, 2012 at 1:17 Comment(4)
Thanks. This more directly answers my question, I think. There is one odd thing though. If I export to HTML or ASCII, the file goes to ./exports, but if I export to TEX (or PDF) it stays in the current dir. Maybe its something in my config?Anglia
Actually, I just saw today that this question was on the org-mode mailing list - unless that was you that posted?Atonal
Does this work with PDFs now? I read through the mailing list exchange there but it was not clear if the export for pdfs had been fixed.Gynaeco
Oddly enough this setting doesn't work for me. Still exports to the same path as the .org file (default behavior)Luvenialuwana
R
11

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.

Readymade answered 29/4, 2016 at 16:43 Comment(4)
Have you tried this? It did not work for me. I am trying to export the pdf to a specific directory up a level. I had read else where that this is only for subtree export and not the entire file. Is that still the case?Gynaeco
Does not work. Running Emacs 26.1 and Orgmode 9.0.9. The folder exports/ exist. It seems the :EXPORT_FILE_NAME: property does won't work at all, whether it is a full path or a relative path, with or without quotes.Matted
@Jesse, even for a subtree export it did not work for me.Prodigy
The accepted solution did not work, but this did. Emacs 27.1, org 9.5.5Ciro
F
8

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).

Forecourse answered 13/12, 2020 at 10:47 Comment(0)
D
3

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

Donatelli answered 5/3, 2012 at 0:57 Comment(3)
You can also use this sort of setup to export parts of an org-mode file by using properties: orgmode.org/worg/org-tutorials/org-latex-export.html#sec-9Atonal
Thanks. I'm going to try this eventually. I had thought about trying to use org-publish, but wasn't sure if it fit what I was doing. I'm still learning.Anglia
I have to replace org-publish-org-to-html to org-html-publish-to-html to make it work.Ginsberg
A
0

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.

Accent answered 6/11, 2020 at 23:5 Comment(0)
K
0

In my setting(Emacs 28.2, OrgMode 9.5.5), I follow this answer

#+export_file_name: path/filename

and it works well. Noting that the filename do not contain a extension name(.tex, .html).

Hope this will help you.

Kirmess answered 20/6, 2023 at 11:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.