This whole question is an org-mode document that can be pasted into your local Emacs for a type-along question.

First evaluate the below code block to

  1. enable caching of org-babel results,
  2. include both source code and results in exports to for example HTML,
  3. enable shell, ditaa end elisp evaluation and
  4. prevent emacs from asking questions before evaluating code.

You evaluate the block by pressing C-c C-c with your pointer in it.

#+begin_src elisp
  (setq org-babel-default-header-args
    (cons '(:cache . "yes")
          (assq-delete-all :cache org-babel-default-header-args)))
  (setq org-babel-default-header-args
    (cons '(:exports . "both")
          (assq-delete-all :exports org-babel-default-header-args)))

     '((ditaa      . t)
       (shell      . t)
       (emacs-lisp . t)))
  (setq org-confirm-babel-evaluate (lambda (lang body) nil))

| lambda | (lang body) | nil |

The first time you export this document, for example to HTML by pressing C-e h h, you will notice that it takes a significant amount of time. Something like five seconds. This is because of the below code block.

#+begin_src sh
sleep 5

Despite us having added :cache yes to the default babel header arguments the result of the code block is not cached on export. We must explicitly hit C-c C-c in the code block for the result #+RESULTS[b34d...]: to be printed in the document and for the cache to be used on the next export.

Why does org-export not write out the #RESULTS into the document?

This was tested on Ubuntu 18.04 running GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.21, cairo version 1.16.0) and Org mode version 9.3 (release_9.3 @ /nix/store/3s5jjymm0znp37dj6m04rak5mkrvfgyy-emacs-27.1/share/emacs/27.1/lisp/org/) retrieved from Nix.

  • 1
    That's documented in the Org mode manual: C-h i g(org)Evaluating Code Blocks - see the last subsection Cache results of evaluation. Cache invalidation is a hard problem, so Org Babel basically depends on the user to tell it when it can trust cached results (by evaluating the code block and making sure the result is in the buffer, so it - Org Babel - can check that the checksum is still valid).
    – NickD
    Commented Sep 22, 2021 at 17:46
  • I realize that I was unclear when I stated my question. My question was: Why does org-export not write out the #RESULTS into the document?
    – Rovanion
    Commented Sep 23, 2021 at 7:38
  • 1
    Because org-export does not allow itself to make any modification to the original file: all it is supposed to do is produce the exported artifact. Modifying the original file in a batch process like export or publish is fraught with danger: any bug can produce a change that may be hidden: you only find out about it later, requiring a potentially long debugging session to fix.
    – NickD
    Commented Sep 23, 2021 at 12:32
  • 1
    BTW, you can always write a function that runs all the code blocks, saves the buffer and then runs export, getting essentially what you want.
    – NickD
    Commented Sep 23, 2021 at 12:42
  • Right, that makes total sense. Back to trying to learn elisp then. Thank you for taking the time to answer my question!
    – Rovanion
    Commented Sep 24, 2021 at 18:51


Your Answer

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

Browse other questions tagged or ask your own question.