I use org-mode to export PDF. It does it through LaTeX ; fine.

So I get .pdf file and .tex files.

Only the PDF is interesting to me. Any setting/way that org-mode cleanup the .tex files by itself (or store it in some temporary folder) ?


You can simply customize the variable org-latex-logfiles-extensions to let org to delete those auxiliary files after export automatically. i.e.

(setq org-latex-logfiles-extensions (quote ("lof" "lot" "tex~" "aux" "idx" "log" "out" "toc" "nav" "snm" "vrb" "dvi" "fdb_latexmk" "blg" "brf" "fls" "entoc" "ps" "spl" "bbl")))

Add the extension which you want to delete to the variable.

  • good lead ! My final solution is : (add-hook 'org-mode-hook (lambda ()(add-to-list 'org-latex-logfiles-extensions "tex"))) – Jocelyn delalande Jun 21 '16 at 15:03
  • I don't think you need to add this to org mode hook. This variable should only work in org-mode. – Leu_Grady Jun 22 '16 at 8:51
  • @leu-grady : the thing is if I do use add-to-list, The var has to be pre-existent, which is only the case after org-mode has been initialized. – Jocelyn delalande Jun 22 '16 at 10:26
  • @Jocelyndelalande you are right. But maybe you can try eval-after-load. – Leu_Grady Jun 22 '16 at 11:29
  • You also need to make sure that org-latex-remove-logfiles is non-nil: it is t by default, but if the files are not deleted, its value might have been changed. Also, this only cleans the various files that latex produces. It does not clean e.g. the subdirectories that org itself (or emacs) might produce. – NickD Dec 27 '17 at 15:49

You can put advice after the function to delete the intermediate file like this:

(advice-add 'org-latex-compile :after #'delete-file)

This is a simple question but has not so simple answers. The temp files are used repeatedly in the production of pdf output. Neither AUCTeX nor ORG knows when it is safe to delete them. Hence they are left there for manual deletion after ascertaining if they are no longer needed.

Here are a few ways to speed up the manual deletion process. A simple shell command will do the job without too much fuss (add other extensions as needed):

(defun clear-latex-temp-files () (shell-command "rm -rf *.tex *.prv preview.fmt"))

Use latexmk (instead of pdflatex) for generating your pdf so you can use the -c option to delete the temp files:

latexmk -c 

and then put the following line in this file ~\.latexmkrc so as to cover all temp files, not just *.tex and *.prv:

@generated_exts = qw(aux idx ind lof lot out toc acn acr alg glg glo gls ist);

You can add other temp files (.synctex .gz .nav .vrb .snm .blg -blx .bib .bbl .run .xml) depending on your needs. This is the most customizable option.

To avoid this complication for simple org files, you can use the builtin -output-directory option of pdflatex.

pdflatex -output-directory=target <file>

Which will also put the pdf output there in the target directory. You can then move the pdf file out of there once you are satisfied it was generated properly. All the deletable files are all in one location that can be purged periodically.

Why not use a macro to delete-file after org-latex-compile? Because that may sometimes delete the temp files prematurely. You may end up with broken links, unlinked biblio entries, unlinked chapter headings in TOC, and unfinished indexes, list of tables, list of figures. See the org-mode documentation string for C-h f org-latex-compile:

The reason why this is a list is that it usually takes several runs of ‘pdflatex’, maybe mixed with a call to ‘bibtex’. Org does not have a clever mechanism to detect which of these commands have to be run to get to a stable result, and it also does not do any error checking.

  • Neat, I had only tried with a very simple example, that only required a single ".tex" file to be created – Jules Jun 15 '16 at 19:13
  • what do you think about @leu-grady solution ? – Jocelyn delalande Jun 21 '16 at 15:05
  • I just don't get why : the org-mode cleanup seems to happen after the PDF is produced.(see org-latex-remove-logfiles doc) ; how could it still need the .tex ? I'd love to understand your point, but please sow me a problematic example. – Jocelyn delalande Jun 23 '16 at 22:40
  • For others reading this answer: if you notice missing indexes, links, or TOC entries in your pdf then use this solution. For simple org files, it won't make a difference when the temp files are deleted. Otherwise it gives a false sense of completeness. The OP wants see a problematic example, but as the documentation says there is no error checking. For each run you may notice random things missing in your pdf depending on when the temp files were deleted (after first, second, or third passes). – Emacs User Jun 29 '16 at 3:47

I set org-latex-pdf-process to something like:

latexmk -outdir=/tmp/latexmk -f -pdf %f; mv %f /tmp/latexmk; mv /tmp/latexmk/%b.pdf %o

This way I keep all the intermediate files in a "hidden" place to accelerate successive compilations but only the pdf is kept at the original place.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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