In literate programming, I often like to write my code in small snippets and then export them all at once via org-babel-tangle
. This works great for non-indentation sensitive code (especially with post-processing), but doesn't work quite well with languages like Haskell or Python as org-babel-tangle
removes the initial whitespace:
This is a completely self-contained example of the problem.
Copy it into an org-mode buffer and try both
=M-x org-export-dispatch= and =M-x org-babel-tangle=.
* A simple Python function
:PROPERTIES:
:header-args:python: :tangle example.py :padline no
:END:
Here's a small example function in Python:
#+begin_src python :shebang "#!/usr/bin/env python"
def foo():
#+end_src
According to org-mode's documentation[fn:1], we can use `-i'
to keep the indentation, so let's do that on the continuation
of the aforementioned example:
#+begin_src python -i
"""Says hello!"""
print("Hello, world!")
#+end_src
If we (org-export-dispatch) the file, then everything seems
nice. However, if we (org-babel-tangle) the file, then the
indentation is gone and thus our Python file invalid.
* Footnotes
[fn:1] See [[info:org#Literal Examples][info:org#Literal Examples]].
The -i
switch doesn't seem to work for org-babel-tangle
at all. While I could use <<noweb>>
to glue the code together in a hidden, non-exported source block, I would rather like to have org-babel-tangle behave correctly.
Is there some hidden option I'm missing? How can I keep the indentation when I tangle files via org-babel-tangle similarly to the -i
switch for exporting?