I'm wanting to use org mode to write a technical book. I'm looking for a way to insert existing code from external file into a babel code block that would give nice formatting when exporting to pdf.
For example
#+BEGIN_SRC python "./code/foo.py"
# insert_line (45,50)
#+END_SRC
would then give me the equivalent of the following from line 45 to 50 in foo.py
#+BEGIN_SRC python
def times_two(x):
y = x*2
return y
print times_two(5)
#+END_SRC
Is there anyway of doing this?
Code Blocks in Org Code blocks can be entered directly into the Org file, but it is often easier to enter code with the function org-edit-src-code , which is called with the keyboard shortcut, C-c ' . This places the code block in a new buffer with the appropriate mode activated.
To save the document, either press the save icon, or press C-x C-s, call it 1.org. Emacs does not actually understand you are editing an Org document, yet. To enable Org mode on your current document, type M-x org-mode which will enable the Org mode on the current document. Those are minuses, not underscores.
I think something like this could work:
#+include: "./code/foo.py" :lines "45-50"
Link to manual: http://orgmode.org/manual/Include-files.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With