1

I'm trying to get literate programming with :noweb syntax working in org-mode. I think I'm down to about the minimum case:

#+TITLE: Console Tic Tac Toe
#+SUBTITLE: A Literate Program in EMACS Org-Mode
#+AUTHOR: Charlie Martin
#+STARTUP: showall

#+BEGIN_SRC python :tangle yes :noweb 
  import sys
  import os

  def main(args):
      <<initialize-the-game-board>>

  if __name__ == "__main__":
      main(sys.argv)
#+END_SRC

#+NAME: initialize-the-game-board
#+BEGIN_SRC python :tangle yes :noweb
  board = [[-1 for x in range(3)] for y in range(3)]
#+END_SRC

but when I tangle it I get:

import sys
import os

def main(args):
    <<initialize-the-game-board>>

if __name__ == "__main__":
    main(sys.argv)

board = [[-1 for x in range(3)] for y in range(3)]

I've tried permuting the argument, flags, and so on to no avail.

1

I agree that the specification is slightly finicky.

The following should do what you want:

#+TITLE: Console Tic Tac Toe
#+SUBTITLE: A Literate Program in EMACS Org-Mode
#+AUTHOR: Charlie Martin
#+STARTUP: showall

#+BEGIN_SRC python :tangle yes :noweb tangle
  import sys
  import os

  def main(args):
      <<initialize-the-game-board>>

  if __name__ == "__main__":
      main(sys.argv)
#+END_SRC

#+NAME: initialize-the-game-board
#+BEGIN_SRC python
  board = [[-1 for x in range(3)] for y in range(3)]
#+END_SRC

The two changes are:

  1. You need to specify :noweb tangle rather than just :noweb. (:noweb yes also works.)

  2. For the second block that is being included, you must not include :tangle yes or it will be included twice in the output (once inserted into the first block, as desired and the second as a block in its own right). You can use :noweb or :noweb yes for the second block, but it neither helps nor hinders, so I've also removed it. (:noweb-ref initialize-the-game-board could serve as an alternative to the #+NAME: line.)

Some more minimal noweb examples:

http://www.draketo.de/light/english/minimal-example-for-literate-programming-noweb-emacs-org-mode

https://stackoverflow.com/questions/26493227/emacs-org-mode-how-to-create-literate-programs-with-noweb-syntax

0

Okay, with some help from the org-mode list, I found the magical incantation: :tangle yes :noweb yes in the root source block.

#+TITLE: Console Tic Tac Toe
#+SUBTITLE: A Literate Program in EMACS Org-Mode
#+AUTHOR: Charlie Martin
#+STARTUP: showall

#+BEGIN_SRC python :tangle yes :noweb yes
  import sys
  import os

  def main(args):
      <<initialize-the-game-board>>

  if __name__ == "__main__":
      main(sys.argv)
#+END_SRC

#+NAME: initialize-the-game-board
#+BEGIN_SRC python 
  board = [[-1 for x in range(3)] for y in range(3)]
#+END_SRC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.