Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include code snippet in org file

Tags:

org-mode

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?

like image 953
Dan Avatar asked Apr 13 '16 17:04

Dan


People also ask

How do you enter codes in Org mode?

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.

How do I use Org mode in Emacs?

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.


1 Answers

I think something like this could work:

#+include: "./code/foo.py" :lines "45-50"

Link to manual: http://orgmode.org/manual/Include-files.html

like image 157
brittAnderson Avatar answered Jan 03 '23 21:01

brittAnderson