6.1 KiB
Anki: Org mode
- org-babel
- org-babel modes
- Creating files with Org mode
- Dynamic content with Org mode
- Asciidoctor-styled callouts
- Timestamps and durations
- Deadlines and schedules
- File navigation
- Editing source code blocks
org-babel
Front
What makes Org mode popular for reproducible research?
Back
org-babel, the library that enables superpowers for Org mode source code blocks.
Among the list of features, org-babel makes the following things easier for creating lab notebooks.
- Execute the source code block and print results.
- Create files from source code blocks, making it possible to create an entire computational report with a single Org mode document.
- Metaprogramming with noweb-inspired system making dynamic content possible.
- Individual control over source code blocks with sessions, export options, and variables.
- Pass values between different source code blocks even in different programming languages.
org-babel modes
Front
How to make org-babel pass values between different source code blocks?
Back
First, configure org-babel to work in functional mode (i.e., :results value
) in a source code block.
With functional mode, it will return values which will be handled by org-babel.
#+name: num
#+begin_src python :results value
return 53
#+end_src
The value cannot be passed unless it has a name that others can reference yet so add a name property to the source code block (i.e., #+name: ${NAME}
).
Now here's a different source code block written in a different language.
To pass a value, you have to configure with :var ${VARNAME}=${NAME}
.
#+begin_src ruby :var num=num :results output
print(num)
#+end_src
Creating files with Org mode
Front
How to create files with Org mode source code blocks?
Back
The :tangle
option enables extracting code blocks into files.
Accepted values include yes
, no
, or a relative path to the Org document where the file will be written.
Dynamic content with Org mode
Front
Is creating dynamic content possible? If so, how?
Back
Yes!
With source code blocks and the noweb option enabled, you can make meta-programming in Org.
You can declare a function by assigning a name on the code block (i.e., #+name: ${FUNC_NAME}
).
Here's an example of a source code block with a default argument.
#+name: greeting
#+header: :var name="World"
#+begin_src sh
echo "Hello ${name}"
#+end_src
You can then call the function in different ways:
- For calling it inline,
call_${FUNC_NAME}()
. - For creating a block,
#+call: ${FUNC_NAME}()
. - For invoking inside a code block,
<<${FUNC_NAME}()>>
, but you have to enable noweb (e.g.,:noweb yes
).
You can then pass header arguments by appending in square brackets ([]
) before invoking it — e.g., call_greeting[:results replaces]()
, #+call: greeting[:results replace]()
, <<greeting[:results replace]()>>
.
Asciidoctor-styled callouts
Front
Are callouts possible? If so, how?
Back
Surprisingly, yes! It is just hidden on the documentation. Specifically, on the Literal examples section of the Org mode manual.
Here's an example to do it.
#+begin_src python
print("Hello world") # (ref:hello)
print(2 + 5) # (ref:num)
#+end_src
In [[(hello)][line 1]], we have printed the traditional "Hello world" program.
In [[(num)][the second line]], we've done a simple arithmetic and printed it into the console.
To create Asciidoctor-styled callouts, create a reference inside of the code block and refer to it (i.e., (${ref})
).
Timestamps and durations
Front
How to denote timestamps and durations in Org mode?
Back
# A timestamp looks like this.
<2021-05-07 Fri>
# To make a duration, just put two dashes between two timestamps.
<2021-05-07 Fri>--<2021-05-08 Sat>
To make creating timestamps easier, execute org-time-stamp
(or whatever keybinding you've set).
Deadlines and schedules
Front
How to make deadlines and schedules?
Back
Just prepend the keywords DEADLINE
and SCHEDULED
, respectively.
DEADLINE: <2021-06-30 Wed>
SCHEDULED: <2021-06-29 Tue>
File navigation
Front
Give some ways how to navigate Org mode documents quickly.
Back
- I don't need to explain what
org-babel-next-src-block
andorg-babel-previous-src-block
does. org-backward-heading-same-level
is the same asorg-forward-heading-same-level
but moves one headline backwards.org-forward-heading-same-level
moves one headline forward in the same level. Useful for navigating sections and subsections.org-goto
creates an interface for showing the outline and it is a great navigation function. Highly recommend to use it with a completion interface (e.g.,counsel-org-goto
,counsel-org-imenu
).org-num-mode
adds a (non-persistent) counter to the document. Very helpful in navigating larger files.org-sort
will sort the entries into your preferred criteria. It also works on a list of items which is very useful if one of the list items has more than one line.
Editing source code blocks
Front
What function creates a buffer for certain elements in org-mode but it is especially useful for editing source code blocks where it will open with the correct major mode?
Back
org-edit-special