Attach source to PDF when exporting in Org-Mode

I am a strong advocate in keeping the source of everything.

However, a source can easily be lost if it doesn’t follow the document. In LaTeX, there’s a package to attach a file to a PDF (like when you attach a file to an email). By doing so, the source will follow the PDF even if the common reader have no clue what it’s for, or even how to use it.

Here is how I attach my org source to every document I export to PDF:

;; Include the source file for every exported PDF (org-mode)
(eval-after-load "org-exp" 
  '(defadvice org-export-as-latex (around org-export-add-source-pdf activate)
     "Add the source (org file) to the resulting pdf file"
     (let ((filename (buffer-name)))
       ad-do-it ;do the function
       (let ((latex-buffer ad-return-value))
         (set-buffer latex-buffer)
         (while (re-search-forward "\\\\usepackage{.+}" nil t)); go to the end of packages
         (insert "\n\\usepackage{attachfile2}"); the package needed to attach files
         (when (re-search-forward "\\\\end{document}" nil t)
           (forward-line -1)
             "The source of this document is an Org-Mode file attached here:"
             "{" filename "}")))

Simply add this code to your .emacs and you’re good to go! Please note that is simply a quick hack. Hopefully an org-mode developper will make a more integrated approach.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: