org-mode notes

Home

org-mode-logo.png

Table of Contents

Org mode notes

Further reading

Tutorials and blog posts

Basic markup

  • _foo_ will underline the text: foo
  • *foo* will make the text bold: foo
  • /foo/ will italicise the text: foo
  • [[target][label]] will generate links

Lists, enumerations and descriptions

  • Use - or * to start a list and then S-<right> to move cycle through the various list types: ordered or unordered.
  • For a description style list use :: to separate the name and its description.

TODO Command line usage

Literate programming

If README.org is a literate program, the source can be tangled out with the following

emacs README.org --batch --eval="(org-babel-tangle)"

And to weave some HTML use the following

emacs README.org --batch --eval="(org-html-export-to-html)"

Creating slides

emacs slides.org --batch --eval="(org-beamer-export-to-latex)"

Configuration

This configuration is explained in detail in the Summary of In-Buffer Settings. A sensible header for an org-mode file should contain the following:

  • #+title:
  • #+Time-stamp: <>
  • #+startup: overview
  • #+options: toc:nil with #+toc: headlines 2
  • #+options: num:nil

You might use #+startup: noinlineimages if you do not want images to display in the window by default. This can be very useful if you have figures too big to fit on the screen.

You can also use #+html_head: to add snippets to the head during HTML export. This is particularly helpful if you want to include additional CSS in the exported pages. This is explained in more detail here.

TODO Citation engine

As stated here.

Org 9.5 provides a new library oc.el which provides tooling to handle citations in Org, e.g., activate, follow, insert, and export them, respectively called "activate", "follow", "insert" and "export" capabilities. Libraries responsible for providing some, or all, of these capabilities are called "citation processors".

The manual contains a few pointers to let you start and you may want to check this blog post. If you need help using this new features, please ask on the mailing list.

Thanks to Nicolas Goaziou for implementing this, to Bruce D'Arcus for helping him and to John Kitchin for paving the way with org-ref.el.

Define the bibliography

Use #+bibliography: <my-refs.bib> to define the bibtex file to use.

TODO Define the citation processor

Use #+cite_export: to define the processor to use. It looks like this should be customised subtantially, but the csl option seemed to work nicely out of the box.

Inserting references

There is the org-cite-insert function to insert a reference. Note that once you have selected the citations to include use M-RET to actually include them in the text. Alternatively, they can be included manually as a semicolon seperated list of @key values.

[cite:@authorYYYYtitle;@authorYYYYtitle]

Print the bibliography

Use #+print_bibliography: at the end of the document to print the bibliography there.

Using LaTeX

Amazingly, you can use latex environments without any issue and they should be handled correctly in both latex and HTML export.

Source code

The following table is derived from this documentation.

Language Identifier
Emacs Lisp emacs-lisp
LaTeX latex
Org mode org
Python python
Snakemake snakemake
R R
shell sh
Haskell haskell
Graphviz dot

HTML export from header

org-mode has functionality for exporting to HTML. The default maths support is good. If you want to include CSS this can be done by including a HTML_HEAD definition. For example, this page has the following line at the top of the file.

#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../css/stylesheet.css" />

If you were to use a different CSS file you would need to change the href. For example, on some other pages I use the following:

#+HTML_HEAD: <link rel="icon" type="image/png" href="../../resources/nicemacs-favicon.png">
#+HTML_HEAD: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.css">
#+HTML_HEAD: <link rel="stylesheet" href="../../microgram.css">

HTML export from body

If you want to incorporate a small snippet of HTML into the exported HTML use the #+begin_export html snippet to include that elements.

HTML export: attributes

There is the #+attr_html: declaration to specify how things should be exported to HTML. For example

#+attr_html: :width 700px
./path/to/image.png

will export the image with the additional attribute that the width should be 700 pixels.

Images

See Figures.

Publishing to HTML: How this site is generated

The code is used to configure the generation of this site looks roughly like the following snippet. For the version of the code actually used, check out my nicemacs.

(require 'ox-publish)

(setq org-publish-project-alist
      '(
        ("org-notes"
         :base-directory "~/public-site/org/"
         :base-extension "org"
         :publishing-directory "~/aezarebski.github.io/"
         :recursive t
         :publishing-function org-html-publish-to-html
         :headline-levels 4
         :auto-preamble t
         )
        ("org-static"
         :base-directory "~/public-site/org/"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf\\|txt\\|cur\\|svg\\|csv\\|json"
         :publishing-directory "~/aezarebski.github.io/"
         :recursive t
         :publishing-function org-publish-attachment
         )
        ("org"
         :components ("org-notes" "org-static"))))

(org-publish "org")

The org-publish-project-alist is the main variable that needs to be set for publishing. Each element of this list describes one destination of the publishing process. Destinations form a hierarchy as can be seen in the "org" element which takes "org-notes" and "org-static" as components.

The base-directory is where to look for source files and the publishing-directory is destination for the results.

Computational notebooks

Notebook example 1

The following demonstrates how org-mode can be used as a notebook for R. Note that

Evaluate an expression and cache the values as =x=.

#+name: x
\#+begin_src R :cache yes
mean(1:10)
\#+end_src

#+RESULTS[57c60ad6be3794c58f0a94dc3c107a692489cb2b]: x
: 5.5

Carry out an action using the variable =x=

\#+begin_src R :var x = x
print(x)
\#+end_src

#+RESULTS:
: 5.5

Notebook example 2

Another example which makes use of sessions and output files to facilitate viewing plots within the file is given below. To make it easier to specify which R session you want to evaluate the code in there is the rename-buffer function. To acutually evaluate evaluate the code there is org-babel-execute-buffer. If you have set :session this will evaluate it in that specified session.

#+header: :width 300 :height 300
\#+begin_src R :session *R-demo* :file 2.png :results graphics file :tangle demo.R
  library(ggplot2)

  g <- ggplot(iris) +
    geom_point(mapping = aes(x = Sepal.Length,
                             y = Petal.Width,
                             colour = Species))

  print(getwd())
  print(g)
\#+end_src

Notebook example 3

In this example we use :exports both to ensure that both the code and the resulting output are exported.

#+header: :exports both
\#+begin_src maxima :results output
  a : (x* exp(-aa) + y + y * exp(-aa))/ (y*exp(-aa) + x);
  b : ratsimp(a);
  c : collectterms(expand(num(b)), exp(aa)) / denom(b);
  d : block(
    [ea: exp(-aa)],
    collectterms(expand(ea * num(b)), ea) / expand(ea * denom(b))
    );
  display(a, b, c, d);
\#+end_src

#+RESULTS:
#+begin_example
                                - aa           - aa
                              %e     y + y + %e     x
                          a = -----------------------
                                     - aa
                                   %e     y + x
                                    aa
                                 (%e   + 1) y + x
                             b = ----------------
                                          aa
                                    y + %e   x
                                    aa
                                  %e   y + y + x
                              c = --------------
                                          aa
                                    y + %e   x
                                  - aa
                                %e     (y + x) + y
                            d = ------------------
                                     - aa
                                   %e     y + x
#+end_example

Literate programming with noweb

The default behaviour when using noweb syntax is that it will be expanded when tangling and exporting. If you want to keep expansion on tangle but not on export set :noweb no-export as shown in the following example.

{
    "foo": "bar"
}

which is produced by the following orgmode

#+name: foobar
#+begin_src javascript :noweb yes
  {
      "foo": "bar"
  }
\#+end_src

Note that the block gets the name foobar which we can then reference in the following block

{
    "fleeb":
        <<foobar>>
}

This was itself produced by the following

\#+name: mainjs
\#+begin_src javascript :noweb no-export :tangle ../misc/silly-example.json
   {
       "fleeb":
           <<foobar>>
   }
\#+end_src

Tangling this produces the following JSON

{
    "fleeb":
        {
            "foo": "bar"
        }
}

Figures

org-toggle-inline-images will toggle the display of figures in an org document. To change the size of the displayed image use the following

#+attr_org: :width 450px
#+attr_html: :width 450px

For export there are other commands you might want to issue. See for example the HTML attributes.

Linking

Use #+name: fig:cool-name to label a figure and then [[fig:cool-name]] to reference it. You can use this to name an equation and then link back to it from else where in the document.

Beamer

The command org-beamer-export-to-latex produces a TEX file from your ORG file. So, given your presentation is in slides.org use the following to create a PDF

emacs slides.org --batch --eval="(org-beamer-export-to-latex)"
pdflatex slides.tex

There are slightly shorter ways to do this, but having the intermediate TEX is good for fine tuning the resulting slides. NOTE if there are TODO items in the slides, these seem to break the export.

Quotation

This is achieved with #+begin_quote and #+end_quote

Everything should be made as simple as possible, but not any simpler —Albert Einstein

Tables

org-table-create

Table of contents

Removing the table of contents

To remove the table of contents from an org-mode export add the following definition to the top of the file.

#+OPTIONS: toc:nil

Configuring the table of contents

If you want to label headers down to level x and include them down to level y in your table of contents, the following configuration can be added to the top of the org file.

#+options: num:x toc:y

Macros

Define a macro at the top of the file

#+MACRO: name   replacement text $1, $2 are arguments

Use it with {{{name(arg1, arg2)}}}.

Agenda

The agenda files are used to track TODO items. Use org-agenda-file-to-front to add an org-mode file to the list of agenda files. If you are ever in doubt about which files are in the adgenda, use SPC h d v and request org-agenda-files.

Items in a TODO list can be prioritised by adding an annotation of [#<A|B|C>]. There are only three levels available by default, and if no annotation is used it is assumed to be level B.

To make it easier to schedule items, bind org-schedule to SPC o o s ([o]wner-[o]rg-[s]chedule) which will bring up a calendar to select the date. You can hold S and then use the arrow keys to navigate the calendar. Times can be added in 24-hour format to the end of the date. This will add a SCHEDULED property under that item. If you want to add a time to the task, put it after the date. The +1w in the example below means that you want the task to be repeated each week. There are y,m,w,d for year, month, week and day (see here for details).

SCHEDULED: <2022-09-18 Sun 14:00 +1w>

The following settings ensure that a sensible amount of material is shown in the agenda: items from up to 3 days ago and over a 30 day span in total.

(setq org-agenda-start-day "-3d")
(setq org-agenda-span 30)
(setq org-agenda-start-on-weekday nil)

The org-todo-keywords variable is used to specify the different TODO types. The pipe, |, in the following example is used to designate that "DONE" is the only keyword which indicates that a task is finished. It is probably easiest to use customise to set these variables.

'(org-todo-keywords '((sequence "TODO" "BLOCKED" "DONE")))

Agenda view keys

  • To navigate up and down lines in the agend view use n/p.
  • v d will show the day view.
  • v w the week view.
  • v m the month view.
  • v SPC resets the view.
  • . goes to today.
  • j will jump to a date (selected via calendar).

Links

On the org-mode website's page on hyperlinks they have the following nice table:

file:~/code/main.c::255 Find line 255
file:~/xx.org::My Target Find <<My Target>>
file:~/xx.org::#my-custom-id Find entry with a custom ID

Using Properties for Custom Identifiers in Org-mode

In org-mode, you can add custom identifiers to sections using properties. A custom identifier is a unique tag that you assign to a section.

Custom identifiers make it easier to link to parts of a document and Org will use the custom identifier you've assigned instead of generating a random ID when exporting the HTML. This feature allows for clearer referencing of sections.

Here's how you can use it:

/** Foobar
/:PROPERTIES:
/:CUSTOM_ID: my-custom-id
/:END:

The section is referenced [[#my-custom-id][here]].

With this approach, if you link to "here", it will take you to the "Foobar" section. And when you export this Org document to HTML, the link will still correctly point to the "Foobar" section thanks to the CUSTOM_ID property.

With tags

To link items within a document, create a tag with <<my-tag-id>> and then reference it with [[my-tag-id][what gets shown]]. By default, when you follow a link in an org file to another org file it will open the file in a new window. This behaviour is specified by the variable org-link-frame-setup and can be customized to visit the file in the current window.

To follow a link, by default emacs will attempt to use external programs before defaulting to opening it in emacs. When this happens it consults the variable org-link-frame-setup for a specification of how to manage frames and windows. There is good documentation for this variable, I use the following setting

'(org-link-frame-setup
  '((vm . vm-visit-folder-other-frame)
    (vm-imap . vm-visit-imap-folder-other-frame)
    (gnus . org-gnus-no-new-news)
    (file . find-file)
    (wl . wl-other-frame)))

Headers in org-mode files

To link to the Hackage section of my haskell notes I would use the following

[[file:./haskell-notes.org::*Hackage][this]]

Which will generate a link like this.

Author: Alexander E. Zarebski

Created: 2024-11-05 Tue 12:36

Validate