From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Dynerman Subject: Re: Include sections of org document in tangled files Date: Wed, 07 Dec 2016 15:46:11 -0800 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:56673) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cEluk-0003Af-OB for emacs-orgmode@gnu.org; Wed, 07 Dec 2016 18:46:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cEluh-0000O0-Gm for emacs-orgmode@gnu.org; Wed, 07 Dec 2016 18:46:18 -0500 Received: from poitras.block-party.net ([107.170.248.157]:57069) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cEluh-0000Nn-AA for emacs-orgmode@gnu.org; Wed, 07 Dec 2016 18:46:15 -0500 In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: "Charles C. Berry" Cc: emacs-orgmode@gnu.org Dear Chuck, Your suggestion worked fantastically - I got it working and am very excited. Now, the next step is figuring out how to handle typesetting math in single way between org and python docstrings. Math in Python docstrings is usually done by RestructuredText markdown, e.g.: :math:`f(x) = x^2` while the org code should just be, for example, \[ f(x) = x^2 \] So the goal would be have noweb not only include a documentation block in the python code, but also call a translation function that identifies LaTeX fragments in the org source and converts them to ReST markdown. That's a separate project though, for now your suggestion is really cool! Thanks a lot, David "Charles C. Berry" writes: > #+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): > """ > <> > """ > > 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 """>""" 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, >