16

In blogs, textbooks, etc, it's really common to display the results of some code as comments, e.g.:

(+ 1 1) ;=> 2

Is there a way to display results like this automatically using Org-Mode's Babel feature?

Specifically, what I'd like is to be able to specify a header such that this:

#+begin_src scheme <some headers>
(+ 1 1)
(/ 4 (* 2 2))
#+end_src

Would turn into the following when I C-c C-c:

#+begin_src scheme <some headers>
(+ 1 1) ;=> 2
(/ 4 (* 2 2)) ;=> 1
#+end_src

And then if I made some changes, like so...

#+begin_src scheme <some headers>
(+ 1 5) ;=> 2
(/ 8 (* 2 2)) ;=> 1
#+end_src

...and C-c C-c, it would replace the comments with updated ones:

#+begin_src scheme <some headers>
(+ 1 5) ;=> 6
(/ 8 (* 2 2)) ;=> 2
#+end_src

Or, short of what I've dreamed up above, is there an easy way to have results displayed by github's org file renderer, so the following would be visible (ideally with formatting that I specify at the document level):

#+RESULTS:
: 2
2
  • Interesting idea, but I don't think there's anything ready-made. I'd try to modify the specific Babel backend to place results in the predetermined place. Though this seems complicated, when the results have multiple lines for example.
    – wvxvw
    Mar 27, 2016 at 12:51
  • You may be interested in github.com/sulami/literate-calc-mode.el
    – unhammer
    Aug 3, 2020 at 11:02

3 Answers 3

12

try this

Use named code blocks and noweb header to turn your code into a literate program.

#+NAME: my-code
#+BEGIN_SRC elisp :exports none 
(+ 1 1)
#+END_SRC

#+NAME: my-other-code 
#+BEGIN_SRC elisp :exports none 
(/ 4 (* 2 2))
#+END_SRC

Create an org code block to format your code with your result

#+BEGIN_SRC org :results drawer replace :noweb yes :exports results 

  ,#+NAME: my-code-with-answer
  ,#+BEGIN_SRC elisp  :exports code 
  <<my-code>> ;=> <<my-code()>>
  <<my-other-code>> ;=> <<my-other-code()>>
  ,#+END_SRC


#+END_SRC

Do a C-c C-c on org code block and you should see results similar to the code below:

#+RESULTS:
:RESULTS:

#+NAME: my-code-with-answer
#+BEGIN_SRC elisp  :exports code 
(+ 1 1) ;=> 2
(/ 4 (* 2 2)) ;=> 1
#+END_SRC

:END:

If you post code to Github, you should see

(+ 1 1) ;=> 2
(/ 4 (* 2 2)) ;=> 1

This code was tested with
GNU Emacs 24.5.1 (x86_64-unknown-cygwin, GTK+ Version 3.14.13)
Org-Mode Version: 8.3.2
and github.

3
  • Thanks. This is a helpful approach and seems flexible enough to allow formatting of the results in whatever way I want. But, I think it (like my own solution) is too verbose to be convenient for quick notes or blog posts, which is what I was hoping to find. Perhaps some modifications to babel building on this functionality could get me there, though. Apr 4, 2016 at 22:41
  • 1
    @parkeristyping - I agree. It's much too verbose. If it's something I going to do fairly often, e.g. blog, I usually write some template code to generate the formatted org code in step 2. Then I just update a variable passed into the template generator, e.g. :var my_code='("my-code" "my-other-code"). Even so it's still more cumbersome than I would like. Would you like me to update my answer?
    – Melioratus
    Apr 4, 2016 at 23:02
  • @parkeristyping - I just figured out another way but it requires that you export the original org file and post the export to GitHub.
    – Melioratus
    Apr 4, 2016 at 23:11
4

The closest that I've been able to accomplish with existing Babel functionality is the following:

First, I define an emacs-lisp function to prepend results with ";=> ":

#+name: commentify
#+begin_src emacs-lisp :var result="" :exports none
(concat ";=> " (format "%s" result))
#+end_src

Then I process results using the :post header:

#+begin_src scheme :post commentify(*this*) :results code :exports both
(+ 5 7)
#+end_src

This returns the following on C-c C-c, which does display on github due to the :exports both header, but it's in a separate code block.

#+RESULTS:
#+BEGIN_SRC scheme
;=> 12
#+END_SRC

I think this strategy leaves the org file too cluttered to be worth it, though. And, unless I disable the code evaluation confirm, I have to accept two "Are you sure you want to evaluate code?" prompts (one for the Scheme block and one for the elisp commentify).

2

You can do something like this:

  1. Write your named Multi-Line source code block as usual

  2. Change the wrapping from BEGIN/END_SRC to …-EXAMPLE:

    #+NAME: real-source
    #+BEGIN_EXAMPLE emacs-lisp
    (+ 3 4)
    (* 4 47)
    #+END_EXAMPLE
  1. Paste and execute this block under the first code block.
    #+NAME: LispBlock
    #+HEADER: :var lcmds=real-source
    #+BEGIN_SRC emacs-lisp :results output :wrap src emacs-lisp
    (dolist (cmd (split-string lcmds "\n"))
      (unless (string= "" cmd )
        (princ
         (format "%s \t; → %s\n"
                 cmd (eval (car (read-from-string cmd)))))))
    #+END_SRC
  1. Result:
    #+RESULTS: LispBlock
    #+BEGIN_src emacs-lisp
    (+ 3 4)     ; → 7
    (* 4 47)    ; → 188
    #+END_src
  1. Add additional multi-line source blocks with individual names as needed into your text

  2. Add #+CALL: lines where you give the name of the Block as lcmds variable. The above transforming block is only needed once per document.

Note that the Transforming block needs to be written in the language that you are treating.

I first tried "automating" the good solution from @melioratus, but ran into problems with that, when i ran into the above solution.

@jpkotta thx for pointing out the fix needed to avoid number-list-with-code-block-problem.

2
  • 1
    Very clever workaround! Thanks for posting!
    – Melioratus
    Feb 11, 2018 at 4:13
  • 1
    Thanks for the :wrap src MODE trick, been wondering how to do that!
    – unhammer
    Mar 18, 2021 at 14:56

Your Answer

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

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