0

Running code within an Org file greatly simplifies writing. It's possible to see the output and confirm everything works as expected.

** Exercises

Implement the following function.

#+BEGIN_SRC python
   ... exercise
   ... exercise
#+END_SRC

It would be even nicer to write the solutions to exercises within the same document, and to run the solution to confirm all is as expected—all without getting out of org-mode.

I can of course comment the solution in/out.

#+begin_comment
#+BEGIN_SRC python
   ... solution
   ... solution
#+END_SRC
#+end_comment

Might there be a way that doesn't require these unnecessary edits?

How can I maintain invisible solutions to exercises within an org-file?

1
  • The :ignore or :noexport tags works well in this situation. Just add it to the solution sub-heading. May 15, 2023 at 17:00

1 Answer 1

1

To easily comment/uncomment the solution, you can use C-c ; (org-toggle-comment) on the "Solution" headline. If you don't want the "Solution" headline to appear in the exported document, you add the :ignore: tag:

** Exercises

Implement the following function.

#+BEGIN_SRC python
   ... exercise
   ... exercise
#+END_SRC

*** Solution :ignore:
#+BEGIN_SRC python
   ... solution
   ... solution
#+END_SRC

To make :ignore: work you have to add some lines to your init. E.g.:

(use-package org-contrib
  :config
  (require 'ox-extra)
  (ox-extras-activate '(ignore-headlines)))

Note that instead of org-toggle-comment You could also use the :noexport: tag:

*** Solution :noexport:
#+BEGIN_SRC python
   ... solution
   ... solution
#+END_SRC
4
  • Fantastic, but then it's even simpler. It's enough to write *** COMMENT Solution, and we hit two birds with one stone. The subsubsection will not appear in the export, and the code block can still be run as usual through C-c C-c, and then it's unnecessary to comment the solution out/in.
    – Sam7919
    May 14, 2023 at 8:30
  • Also, it could then be possible to use the :noexport: tag on *** Solution. Personally I prefer the *** COMMENT Solution option, since there is a keybinding for it. I just mention :noexport: FYI. You can accept the solution if it solves your issue.
    – crocefisso
    May 14, 2023 at 8:35
  • Right... *** COMMENT Solution and *** Solution :noexport: seem to be indeed wholly equivalent in this context. Feel free to blend all this into your answer so that future readers can more clearly access this Q&A as a reference, but I myself am content.
    – Sam7919
    May 14, 2023 at 8:45
  • I just added the :noexport: option to the solution.
    – crocefisso
    May 14, 2023 at 9:02

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.