From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Schulte Subject: Re: [BUG?][babel] Re: Including source when exporting in PDF Date: Fri, 20 Jan 2012 11:12:41 -0700 Message-ID: <87k44mnqp8.fsf@gmx.com> References: <8739bt1dhe.fsf@gmx.com> <4F0DAAAC.50607@gmail.com> <4F0EA3B5.6080903@gmail.com> <4F140821.4090509@gmail.com> <4F168AC7.4030409@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([140.186.70.92]:33401) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RoJid-0002IJ-Hl for emacs-orgmode@gnu.org; Fri, 20 Jan 2012 14:01:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RoJib-0001v8-4s for emacs-orgmode@gnu.org; Fri, 20 Jan 2012 14:01:47 -0500 Received: from mailout-us.gmx.com ([74.208.5.67]:47037 helo=mailout-us.mail.com) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1RoJia-0001uw-Pc for emacs-orgmode@gnu.org; Fri, 20 Jan 2012 14:01:45 -0500 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: R.M.Krug@gmail.com Cc: emacs-orgmode@gnu.org, Frozenlock --=-=-= Content-Type: text/plain Rainer M Krug writes: > On 18/01/12 01:57, Frozenlock wrote: >> I'm sorry, but I don't quite understand... >> >> Would it be possible to send a minimum working example of your org >> file? >> > > The example is below: > This is likely the most evil snippet of Org-mode code I have ever seen. :) --=-=-= Content-Type: text/x-org Content-Disposition: inline; filename=evil.org #+Options: ^:nil * name src_emacs-lisp{(buffer-file-name)} #+BEGIN_SRC emacs-lisp :results org :exports code :var buffer-file-name=(buffer-file-name) (org-babel-tangle) #+END_SRC --=-=-= Content-Type: text/plain I can confirm that it does modify the value of the file on disk as part of the export process. Additionally, the src_emacs_lisp in the header does not export any results because by the time (buffer-file-name) is called the code is living in a temporary export buffer which is not visiting a file, note that a heading like the following would probably give the result you're after * name src_emacs-lisp{org-current-export-file} similarly you want the tangling to be done in the original Org-mode file (not the temporary export file), so that should be placed in a header argument as well. Try the following less evil version on for size and let me know if it doesn't solve your problems. --=-=-= Content-Type: text/x-org Content-Disposition: inline; filename=less-evil.org #+Options: ^:nil * name src_emacs-lisp{org-current-export-file} One block to tangle. #+BEGIN_SRC emacs-lisp :tangle yes (message "I am tangled") #+END_SRC One block to export. #+BEGIN_SRC emacs-lisp :exports results :var foo=(org-babel-tangle) (message "I just tangled %S during export" foo) #+END_SRC --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable > > ################################################ > * name src_emacs-lisp{(buffer-file-name)} > #+BEGIN_SRC emacs-lisp :results org :exports results :var > buffer-file-name=3D(buffer-file-name) > (org-babel-tangle) > #+END_SRC > ################################################ > > If I put the text in the ### lines in a new org file and export it, it > is modified as described below. > > My guess is that it has to do with the fact that on export, a > temporary copy is created, and some confusion is happening with which > file is tangled. > > Cheers, > > Rainer > >>=20 >>=20 >> On Mon, Jan 16, 2012 at 6:21 AM, Rainer M Krug >> wrote: On 15/01/12 06:29, Frozenlock wrote: >>>>> This babel block should do the following: >>>>>=20 >>>>> - When exporting, automatically tangle the babel blocks. - >>>>> Then take the resulting files and add them to a zip file, >>>>> along with the original .org file. - Add the necessary >>>>> \usepackage and latex commands to insert the zipfile. >>>>>=20 >>>>> Let me know if something isn't like you want. >>>>>=20 >>>>> Here is the babel block to add to your source (org) file: >>>>>=20 >>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #+BEGIN_SRC >>>>> emacs-lisp :results org :exports results :var=20 >>>>> buffer-file-name=3D(buffer-file-name) (let ((filename=20 >>>>> (file-name-nondirectory (file-name-sans-extension=20 >>>>> buffer-file-name)))) (shell-command (concat "zip -j " >>>>> filename ".zip " filename".org " (mapconcat '(lambda (arg)=20 >>>>> (convert-standard-filename (expand-file-name arg)))=20 >>>>> (org-babel-tangle) " "))) (concat=20 >>>>> "#+LATEX_HEADER:\\usepackage{attachfile2}\n" "#+LATEX: >>>>> \\vfill \\textattachfile[print=3Dfalse,color=3D0.5 0.5=20 >>>>> 0.5]{"filename".zip}{Source (.org) \\& other files...}\n"))=20 >>>>> #+END_SRC =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>=20 >> Thanks for this code - it looks good, but there is a serious >> problem: >>=20 >> src_emacs_lisp{} expressions are evaluated and replaced with the=20 >> result in the org file, which does not happen when tangling from >> the buffer itself. Example: >>=20 >> ################################################ * name >> src_emacs-lisp{(buffer-file-name)} #+BEGIN_SRC emacs-lisp :results >> org :exports results :var buffer-file-name=3D(buffer-file-name)=20 >> (org-babel-tangle) #+END_SRC=20 >> ################################################ >>=20 >> If I export the file above, the file get changed to: >>=20 >> ################################################ * name >>=20 >> #+BEGIN_SRC emacs-lisp :results org :exports results :var=20 >> buffer-file-name=3D(buffer-file-name) (org-babel-tangle) #+END_SRC=20 >> ################################################ >>=20 >> Is there something wrong with the code? >>=20 >> Rainer >>=20 >>=20 >>>>>=20 >>>>>=20 >>>>> On a related note, I've added some improvements to my own way >>>>> to export in multiple format and add the files along with the >>>>> source. The latex commands and usepackage are included, so no >>>>> need to add them elswhere in the org file: >>>>>=20 >>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D # Make sure this babel block >>>>> is the last one in the buffer; # block after this one won't >>>>> be evaluated before the txt and html export. #+BEGIN_SRC >>>>> emacs-lisp :results org :exports results :var >>>>> buffer-file-name=3D(buffer-file-name) (let=20 >>>>> ((org-export-babel-evaluate nil)); don't evaluate in >>>>> 'recursive' exports (save-window-excursion ;; avoid leakage >>>>> when more than one org export in block (org-export-as-ascii=20 >>>>> org-export-headline-levels)) (save-window-excursion=20 >>>>> (org-export-as-html org-export-headline-levels)) (let >>>>> ((filename (file-name-nondirectory (file-name-sans-extension=20 >>>>> buffer-file-name))) (to-include-files (directory-files=20 >>>>> (file-name-directory buffer-file-name)))) (shell-command >>>>> (concat "zip -j " filename ".zip " (mapconcat '(lambda (arg) >>>>> arg) (remove-if '(lambda (file) (string-match=20 >>>>> "\\.$\\|\\.pdf$\\|\\.atfi$\\|\\.tex$\\|#" file)) ;; don't >>>>> include useless files to-include-files) " "))) (concat=20 >>>>> "#+LATEX_HEADER:\\usepackage{attachfile2}\n" "#+LATEX: >>>>> \\vfill \\textattachfile[print=3Dfalse,color=3D0.5 0.5=20 >>>>> 0.5]{"filename".zip}{Source (.org) & html, txt...}\n"))) >>>>> #+END_SRC =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>>>>=20 >>>>>=20 >>>>> Enjoy! >>>>>=20 >>>>>=20 >>>>>=20 >>>>> On Thu, Jan 12, 2012 at 8:59 AM, Rainer M Krug >>>>> wrote: >>>>>> On 12 January 2012 14:54, Frozenlock >>>>>> wrote: >>>>>>> The code block I previously sent only require you to >>>>>>> specify which _extension_ you want or don't want. No need >>>>>>> to include any specific filename (other than the .zip >>>>>>> file in the latex "include" command). >>>>>>>=20 >>>>>>> Am I to understand you want something to include *all and >>>>>>> only* tangled files? >>>>>>=20 >>>>>> Yes - and the files do have different extensions (e.g. .R, >>>>>> .sh, .sub, none) and are not tangled necessarily in a >>>>>> specific directory. >>>>>>=20 >>>>>> and the original org file, but that is not a problem. >>>>>>=20 >>>>>> Rainer >>>>>>=20 >>>>>>>=20 >>>>>>> -- >>>>>>>=20 >>>>>>> I tried ":exports result" with no success. I'll try to >>>>>>> send a minimum working example shortly. >>>>>>>=20 >>>>>>> On Thu, Jan 12, 2012 at 4:11 AM, Rainer M Krug=20 >>>>>>> wrote: >>>>> On 12/01/12 03:06, Frozenlock wrote: >>>>>>>>>> To include multiple files, I export all of the >>>>>>>>>> required files before the PDF creation and zip >>>>>>>>>> them. This way, I only need to include a single zip >>>>>>>>>> file. >>>>>=20 >>>>> Good idea. >>>>>=20 >>>>>>>>>>=20 >>>>>>>>>> #+BEGIN_SRC emacs-lisp :exports none ;; various >>>>>>>>>> exports (save-window-excursion >>>>>>>>>> (org-export-as-ascii org-export-headline-levels)) >>>>>>>>>> (save-window-excursion (org-export-as-html >>>>>>>>>> org-export-headline-levels)) >>>>>>>>>>=20 >>>>>>>>>> ;;zip the required files (let ((filename=20 >>>>>>>>>> (file-name-sans-extension (file-name-nondirectory=20 >>>>>>>>>> (buffer-file-name))))) (shell-command (concat "zip >>>>>>>>>> " filename ".zip " (mapconcat '(lambda (arg) arg)=20 >>>>>>>>>> (remove-if '(lambda (filename) (string-match=20 >>>>>>>>>> "\\.$\\|\\.pdf$\\|\\.atfi$\\|#" filename))=20 >>>>>>>>>> (directory-files (file-name-directory=20 >>>>>>>>>> (buffer-file-name)))) " ")))) #+END_SRC >>>>>>>>>>=20 >>>>>>>>>> (I've added this code in a babel block to evaluate >>>>>>>>>> just before the PDF export.) >>>>>=20 >>>>> I actually did not want to fiddle with the file names, as it >>>>> is to easy to forget some: this concerns in my case literate >>>>> programming of a simulation model in R, resul;ting in several >>>>> files of different extensions - it is to easy to forget one. >>>>>=20 >>>>> If I tangle, I get all the names of the tangled files, but I >>>>> do not know how I can feed them into the zip file. >>>>>=20 >>>>>>>>>>=20 >>>>>>>>>> As you can see, I make sure I don't include a >>>>>>>>>> previous PDF, or any other useless file. >>>>>=20 >>>>> True. >>>>>=20 >>>>>>>>>>=20 >>>>>>>>>> If you wish to add only a single type of file, >>>>>>>>>> simply replace "remove-if" by "remove-if-not" and >>>>>>>>>> change the value in the string-match function. For >>>>>>>>>> example, "\\.$\\|\\.pdf$\\|\\.atfi$\\|#" would >>>>>>>>>> become "\\.lisp$" to include all your exported lisp >>>>>>>>>> files. >>>>>>>>>>=20 >>>>>>>>>> Hope this helps! >>>>>>>>>>=20 >>>>>>>>>>=20 >>>>>>>>>> By the way, I can't get the code block to be >>>>>>>>>> evaluated automatically when I export to PDF, any >>>>>>>>>> clue on how to do that? >>>>>=20 >>>>> I assume, it is caused by the :exports none - so no >>>>> evaluation is done on export. Try changing it to :exports >>>>> result and then generate an empty result, or a list of files >>>>> included in the zip file. >>>>>=20 >>>>> Cheers, >>>>>=20 >>>>> Rainer >>>>>=20 >>>>>=20 >>>>>>>>>>=20 >>>>>>>>>>=20 >>>>>>>>>>=20 >>>>>>>>>> On Wed, Jan 11, 2012 at 10:28 AM, Rainer M Krug=20 >>>>>>>>>> wrote: On 06/01/12 08:45, >>>>>>>>>> Eric Schulte wrote: >>>>>>>>>>>>> Frozenlock writes: >>>>>>>>>>>>>=20 >>>>>>>>>>>>>> I am a strong advocate in keeping the >>>>>>>>>>>>>> source of everything. >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>> However, =C3=82 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. >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>=20 >>>>>>>>>>>>> This sounds like a great Reproducible >>>>>>>>>>>>> Research practice. >>>>>>>>>>>>>=20 >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>> Here is how I attach my org source to >>>>>>>>>>>>>> every document I export to PDF: >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>> ;; 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))=20 >>>>>>>>>>>>>> (set-buffer latex-buffer) (while=20 >>>>>>>>>>>>>> (re-search-forward "\\\\usepackage{.+}" nil >>>>>>>>>>>>>> t)); go to the end of packages (insert=20 >>>>>>>>>>>>>> "\n\\usepackage{attachfile2}"); the >>>>>>>>>>>>>> package needed to attach files (when >>>>>>>>>>>>>> (re-search-forward "\\\\end{document}" nil >>>>>>>>>>>>>> t) (forward-line -1) (insert (concat >>>>>>>>>>>>>> "\\vfill\n" "\\footnotesize\n" "The source >>>>>>>>>>>>>> of this document is an Org-Mode file=20 >>>>>>>>>>>>>> attached here:" "\n\\attachfile" "{" >>>>>>>>>>>>>> filename "}"))) (save-buffer))))) >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>> This is by no mean a patch, but rather a >>>>>>>>>>>>>> quick hack. Perhaps someone with a working >>>>>>>>>>>>>> knowledge of the org-export could find a >>>>>>>>>>>>>> way to add a source option? >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>=20 >>>>>>>>>>>>> I think this practice may not actually >>>>>>>>>>>>> require any changes to the Org-mode core. =C3=82 >>>>>>>>>>>>> The attached small Org-mode file will attach >>>>>>>>>>>>> itself to pdf exports using only features >>>>>>>>>>>>> already present in Org-mode. >>>>>>>>>>=20 >>>>>>>>>> Following this idea - how can I easily attach all >>>>>>>>>> files created by tangling? Is there a programmatic >>>>>>>>>> way, without having to specify them manually? >>>>>>>>>>=20 >>>>>>>>>> Thanks, >>>>>>>>>>=20 >>>>>>>>>> Rainer >>>>>>>>>>=20 >>>>>>>>>>=20 >>>>>>>>>>>>>=20 >>>>>>>>>>>>>=20 >>>>>>>>>>>>>=20 >>>>>>>>>>>>>=20 >>>>>>>>>>>>>=20 >>>>>>>>>>>>> Thanks for sharing this idea! >>>>>>>>>>>>>=20 >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>> Cheers! >>>>>>>>>>>>>>=20 >>>>>>>>>>>>>=20 >>>>>>>>>>=20 >>>>>>>>>>=20 >>>>>=20 >>>>>>=20 >>>>>>=20 >>>>>>=20 >>>>>> -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc=20 >>>>>> (Conservation Biology, UCT), Dipl. Phys. (Germany) >>>>>>=20 >>>>>> Centre of Excellence for Invasion Biology Stellenbosch=20 >>>>>> University South Africa >>>>>>=20 >>>>>> Tel : +33 - (0)9 53 10 27 44 Cell: +33 - (0)6 >>>>>> 85 62 59 98 Fax (F): +33 - (0)9 58 10 27 44 >>>>>>=20 >>>>>> Fax (D): +49 - (0)3 21 21 25 22 44 >>>>>>=20 >>>>>> email: Rainer@krugs.de >>>>>>=20 >>>>>> Skype: RMkrug >>=20 --=20 Eric Schulte http://cs.unm.edu/~eschulte/ --=-=-=--