emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: David Dynerman <emperordali@block-party.net>
To: "Charles C. Berry" <ccberry@ucsd.edu>
Cc: emacs-orgmode@gnu.org
Subject: Re: Include sections of org document in tangled files
Date: Wed, 07 Dec 2016 15:46:11 -0800	[thread overview]
Message-ID: <m2d1h3qp4s.fsf@block-party.net> (raw)
In-Reply-To: <alpine.OSX.2.20.1612030950530.882@charles-berrys-macbook.local>

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" <ccberry@ucsd.edu> 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):
>        """
>        <<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,
>

  reply	other threads:[~2016-12-07 23:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-02 22:05 Include sections of org document in tangled files David Dynerman
2016-12-03 18:16 ` Charles C. Berry
2016-12-07 23:46   ` David Dynerman [this message]
2016-12-08  3:12     ` Charles C. Berry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2d1h3qp4s.fsf@block-party.net \
    --to=emperordali@block-party.net \
    --cc=ccberry@ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).