emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Include sections of org document in tangled files


From: Charles C. Berry
Subject: Re: [O] Include sections of org document in tangled files
Date: Sat, 3 Dec 2016 10:16:29 -0800
User-agent: Alpine 2.20 (OSX 67 2015-01-07)

On Fri, 2 Dec 2016, David Dynerman wrote:

Dear list,

Is it possible to include sections of an org document while tangling.

I have in mind something like the following:

* Some section

[David's version deleted]

#+NAME: doc
#+BEGIN_SRC org :exports results :results replace
  This is an introduction. We're going to write some code that
  implements (a finite version of) the formula

  \[
  f(x) = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \ldots +
  \frac{x^n}{n!} + \ldots
  \]

  Here's some background about the exponential function.

#+END_SRC

#+header: :noweb (unless org-export-current-backend "yes")
#+BEGIN_SRC python :tangle myfunction.py
  from math import factorial

  def f(x,n):
      """
      <<doc>>
      """

      y = 0
      for i in range(n+1):
          y = y + x**i/factorial(i)

      return y
#+END_SRC


The idea would be that when I export this to HTML, I get a nice literate
programming math jax'd section introduction that explains what the
function is doing.

Right. The above does that.

You need `org' as a babel language. Eval'ing `(require 'ob-org)' is good enough for just trying to export this, but customizing `org-babel-load-languages' to include `org' is better if you use this regularly.


Then, when I tangle to generate the python file, the
org section introduction would be included as the python docstring of
the function.


And it does that.

---

There is an issue you might want to address if you use this approach. First, the first triple quotes must not be on the same line as the included src block reference. That is because """<src-blk-ref>>""" will prepend the quotes to every line in `src-blk-ref'.

If you do not want the newlines between the quotes and the docstring, I think there is a post-tangle hook you can use to clean the tangled version by removing those newlines.

Also, you can use an export filter to remove the quotes and the noweb reference for a cleaner looking exported doc.

Note that you need to use a unique name for each src-block.

HTH,

Chuck



reply via email to

[Prev in Thread] Current Thread [Next in Thread]