emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: John Kitchin <jkitchin@andrew.cmu.edu>
To: emacs-orgmode@gnu.org
Subject: Re: example filter for code blocks?
Date: Thu, 26 Sep 2013 13:57:35 -0400	[thread overview]
Message-ID: <CAJ51ETq0Y7di4opjqGdDxoYP37QaKythGZfaWXNzmGST3bK9ZQ@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 3765 bytes --]

Hi everyone,
Thanks for  the tips in using export filters for code blocks. I thought I
would share my current solution. The goal was to export all the code blocks
in an org-file to files systematically named part1/script-%d.py where %d is
a number. I didnot want to tangle exactly, because I wanted to avoid naming
the code block tangle files.

Then, I wanted to insert a pdf link that would open the file, after the
syntax highlighted code.

I wanted this because it is not convenient to copy and paste the
syntax-highlighted code into an editor. I teach from the pdf that is
generated, and it would be convenient to just open the code, edit and rerun
to explore solutions.

So, here is the solution:

At the top of my orgfile, I have this definition which creates a pdf link.

#+LATEX_HEADER: \newcommand{\LaunchBinary}[2]{%
#+LATEX_HEADER:   % #1: layer name,
#+LATEX_HEADER:   % #2: link text
#+LATEX_HEADER:   \leavevmode%
#+LATEX_HEADER:   \pdfstartlink  attr{/C [0.9 0 0] /Border [0 0 2]} user {
#+LATEX_HEADER:     /Subtype /Link
#+LATEX_HEADER:     /A <<
#+LATEX_HEADER:       /F <<
#+LATEX_HEADER:          /DOS (#1)
#+LATEX_HEADER:       >>
#+LATEX_HEADER:       /S /Launch
#+LATEX_HEADER:     >>
#+LATEX_HEADER:   } #2%
#+LATEX_HEADER:   \pdfendlink%
#+LATEX_HEADER: }


Then, I use the code snippet below to export the file to latex. It is
stored in a noexport section at the end of the document. basically I set a
counter, and wrote a filter function for src blocks. the function captures
the lines between the first and last (first is \begin{minted}... and last
is \end{minted} in this case. I write those lines to a file named according
to the counter, and finally insert \LaunchBinary... into the string
returned by the filter. everything else in this let block is just
fine-tuning the latex packages, and export behavior.

(let (
      ;; these packages are loaded in the latex file
      (org-latex-default-packages-alist
       '(("utf8" "inputenc" nil)
     ("T1" "fontenc" nil)
     ("" "fixltx2e" nil)
     ("" "natbib" t)
     ("" "url" t)
     ("" "graphicx" t)
         ("" "textcomp" t)
         ("" "underscore" t)
     ("" "amsmath" t)
         ("version=3" "mhchem" t)
         ("tight,pdftex" "web" nil)
         ("" "exerquiz" nil)
         ("ImplMulti" "dljslib" nil)
        ))
      (async nil)
      (subtreep nil)
      (visible-only nil)
      (body-only nil))

(setq counter 0)

  (defun ox-mrkup-filter-src-block (text back-end info)
    (setq counter (+ counter 1))

    (let ((filename (format "part1-scripts/script-%d.py" counter)))
      (with-temp-buffer
        (insert (mapconcat 'identity (butlast (cdr (split-string text "\n"
t))) "\n"))
        (write-region (point-min) (point-max) filename))

      (format "%s

\\LaunchBinary{%s}{Open the python script (%s).}

" text filename filename)))

(let ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block)))
  (org-latex-export-to-latex async subtreep visible-only body-only
                 '(:with-author t
                                            :with-date t
                                            :with-title t
                                            :with-timestamps t
                                            :with-todo-keywords t
                                            :with-toc nil))))


After building the pdf with pdflatex, I get a link with a red box around it
that I can click on, and on my system it opens the python file in the
python editor I have configured to open the file!


Thanks again!

John

-----------------------------------
John Kitchin
Associate Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu

[-- Attachment #2: Type: text/html, Size: 4810 bytes --]

             reply	other threads:[~2013-09-26 17:57 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-26 17:57 John Kitchin [this message]
2013-09-26 19:43 ` example filter for code blocks? Thomas S. Dye
2013-09-26 19:48   ` John Kitchin
2013-09-26 20:58     ` Thomas S. Dye
2013-09-27  7:42 ` Sebastien Vauban
2013-09-30  0:33 ` Thomas S. Dye
2013-09-30  0:45   ` John Kitchin
2013-09-30  2:58     ` Thomas S. Dye
2013-09-30  7:34     ` Achim Gratz
2013-09-30 17:48       ` Thomas S. Dye
  -- strict thread matches above, loose matches on Subject: below --
2013-09-27 18:44 John Kitchin
2013-09-27 12:59 John Kitchin
2013-09-27 15:24 ` Alan Schmitt
2013-09-27 15:34   ` John Kitchin
2013-09-30  7:49     ` Alan Schmitt
2013-09-26  0:03 John Kitchin
2013-09-26  8:40 ` Daniele Pizzolli
2013-09-26  9:04 ` Marcin Borkowski
2013-09-26 12:18 ` Eric Schulte
2013-09-26 18:31   ` Cook, Malcolm
2013-09-26 19:34     ` John Kitchin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJ51ETq0Y7di4opjqGdDxoYP37QaKythGZfaWXNzmGST3bK9ZQ@mail.gmail.com \
    --to=jkitchin@andrew.cmu.edu \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).