0

My goal is to export org file into a pdf file . In order to achieve it I am using org-latex-export-to-pdf:

org-latex-export-to-pdf         Export current buffer to LaTeX then process through to PDF.

Hence org-latex-export-to-pdf generates additional tex file.

.
├── README.org
├── README.pdf
├── README.tex

Is it possible to prevent/delete the tex file after the pdf is generated?

5
  • Does this answer your question? Delete file (if it exists) before saving
    – nega
    Aug 16, 2023 at 15:01
  • But README.org and README.tex names differs from each other and I don't apply any save operation.
    – alper
    Aug 16, 2023 at 15:21
  • Read the first line of the first answer.
    – nega
    Aug 16, 2023 at 15:41
  • 1
    1) Look for a hook that will run at the appropriate time. If you find one, write a function to do what you want and add it to the hook. 2) If no appropriate hook is found, add an after-advice to the function that produces the PDF (e.g. possibilities are org-export-as, org-latex-export-to-pdf, org-latex-compile). 3) Piggyback on already existing cleanup code for log files and make it delete the .tex file as well. This is the standard sequence of considerations that you have to make in many (more-or-less similar) situations.
    – NickD
    Aug 16, 2023 at 16:10
  • 1
    From my POV, the .tex file is often useful (either for debugging or for tweaking and compiling by hand or to just see what Org mode did exactly), so I never want it deleted automatically. I usually write a simple Makefile with a clean action for all the bits and pieces that LaTeX produces (log files, aux files, etc) and a veryclean action that gets rid of all that plus the .tex file and I run the make by hand.
    – NickD
    Aug 16, 2023 at 16:13

1 Answer 1

1

As suggested in the comments, you might well want to consider retaining the intermediate files associated with the latex conversion to PDF until you are happy that the PDF results are what you want.

Instead of deleting straight after compilation, I use a hook when I close a buffer on an org file to delete all the extraneous files that may be generated:

;; Hook to tidy latex process files when we close the buffer
(defun aff/tidy-latex-waste-products ()
  "Clean up possible latex intermediate files when closing an org file."
  (if (and (string-equal major-mode "org-mode")
           (stringp buffer-file-name)
           (file-exists-p (file-name-directory buffer-file-name))) ;; in case deleted
      (dolist (file (directory-files
                     (file-name-directory buffer-file-name)
                     t
                     (concat (regexp-quote (file-name-base
                                            buffer-file-name))
                             "\\(?:\\.[0-9]+\\)?"
                             "\\."
                             (regexp-opt org-latex-logfiles-extensions))))
        (delete-file file) ) ) )

(add-hook 'kill-buffer-hook 'aff/tidy-latex-waste-products)

Note that this doesn't delete the .tex file as you wanted. I would consider the comments that suggest the .tex file is useful to keep around. If you do want to delete it, change the second last line of the function to:

(regexp-opt (cons "tex" org-latex-logfiles-extensions)))))

Your Answer

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

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