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?
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.
.org
file (default behavior) –
Luvenialuwana 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:
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.
This is a slight modification of a code snippet found in http://rwx.io/posts/org-export-configurations/
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.
EXPORT_FILENAME
when exporting a subtree. Any idea how to make that work? –
Prodigy 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.
.org
file (default behavior) –
Luvenialuwana 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.
: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 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
).
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
org-publish-org-to-html
to org-html-publish-to-html
to make it work. –
Ginsberg 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.
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.
© 2022 - 2025 — McMap. All rights reserved.