0

If I have a code block in orgmode, I can export its evaluation by using the :exports both option.

#+begin_src cpp -n :includes <iostream> :exports both
std::cout << "Hello there";
#+end_src

#+RESULTS:
: Hello there

When I export to html (C-c C-e h o), the result block follows the code block. However, I would like to more explicitly show that the 2nd block is the result of the first, with say, a simple label.

If I modify the above, like so:

#+begin_src cpp -n :includes <iostream> :exports both
std::cout << "Hello there";
#+end_src

Output:

#+RESULTS:
: Hello there

then the label "Output:" appears, but the result block appears twice -- once before the label, and once after. What's worse is that if I run the code within orgmode (C-c C-c), then a second result block is placed before the text label, "Output:". I suspect this is what's happening on export as well.

I have also noticed that when exporting to html, the result blocks are placed in a pre tags of class example. I thought I could modify the css with something like:

pre.example::before { content: "Output:"; }

but unfortunately, this places the text inside the pre block, and I can't add any line breaks.

Is there any simple way to add text labels to result blocks in either orgmode itself, or perhaps through css? I'd like to avoid javascript if possible.

2 Answers 2

2

This should work on reasonably recent org:

    #+name: foo
    #+begin_src cpp -n :includes <iostream> :exports both
    std::cout << "Hello there";
    #+end_src

    Output:

    #+RESULTS: foo
    : Hello there
0

You can use a derived backend for this like this:

(defun my-results (fixed-width contents info)
  "Transform a results block to make it more visible."
  (let ((results (org-element-property :results fixed-width))
    (format (elt (plist-get info :back-end) 2))
    (value (org-element-property :value fixed-width)))
    (cond 
     ((eq 'html format)
      (format "<pre>Output:<br> %s</pre>" value)))))


(org-export-define-derived-backend 'my-html 'html
  :translate-alist '((fixed-width . my-results)))

(browse-url (org-export-to-file 'my-html (concat (file-name-base (buffer-file-name)) ".html")))
4
  • If I put this code in my .emacs file, I get the error message Symbol's function definition is void: org-export-define-derived-backend . I supposed it could have been due to the fact that my org-mode version was 8.2.something, so I upgraded to 8.3.4. I still get this error after upgrading. Jun 26, 2016 at 19:07
  • That should be defined in ox.el. Maybe add (require 'ox) above that code. Jun 27, 2016 at 0:50
  • that isn't where you need to add it. you need to put it in your .emacs file. Jun 29, 2016 at 1:57
  • Sorry, I misinterpreted what you said. If I add your code to my .emacs with (require 'ox) beforehand, I get an error along the lines of Wrong argument type, stringp nil Jun 29, 2016 at 23:43

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.