This is a continuation of my series of introductory posts on org-mode that are focussed on simple text-based notes. We looked already at structuring your notes and adding tables and links and images to your notes. In this post we will look at formatting the text in your notes.
For today we will look at the effect of formatting on the plain text view of your notes, but very soon we will come to exporting your notes to html and pdf and we’ll see that the formatting is applied to the exported documents very nicely.
We will also look at including executable code in your notes. Hopefully you can see that by putting all these pieces together you can build a very powerful document of your research containing your data, notes, relevant links, source code and results.
As before I suggest adding the notes below to your growing org file. For technical reasons I have to display the notes as plain text below but if you paste them into your org file you’ll see them nicely formatted and coloured like this:
* Formatting text
** Simple formatting
You can apply simple formatting to your text by enclosing words in
special characters. These include
- /italicised text/
- *bold text*
- _underlines_
- =literal text=
- ~code~ (generally appears the same as literal text)
Here are the full notes:
* Formatting text
** Simple formatting
You can apply simple formatting to your text by enclosing words in
special characters. These include
- /italicised text/
- *bold text*
- _underlines_
- =literal text=
- ~code~ (generally appears the same as literal text)
** Formatted blocks of text
For longer pieces of text you can enclose the text in blocks marking
it as a specific sort of text. I commonly use these ones
#+BEGIN_EXAMPLE
This is an example block into which you can type text that you don't want org to mess with like a [[link]]. This will typically be rendered in a monospace font when exported.
#+END_EXAMPLE
#+BEGIN_QUOTE
This block encloses text that you want to appear as a quotation.
#+END_QUOTE
#+BEGIN_CENTER
This text will be centred when it is exported.
#+END_CENTER
You can save time typing out the block wrapper by using shortcuts. Go
to the start of a new line and type <e and press TAB and it will
expand to an example block. The same works for <q for quote and <c for
centre.
** LaTeX
Org-mode does a good job of understanding snippets of LaTeX (a
[[https://www.latex-project.org/][powerful typesetting language]] used in scientific and other technical
documents). For example, it will correctly export simple superscripts
x^2 or subscripts x_0 or symbols like \alpha, \beta, \gamma.
Org also understands more complex LaTeX like this
\begin{eqnarray}
x^2 + \left(\frac{y}{z}\right)^4 = 0
\end{eqnarray}
but for longer bits of LaTeX it is better to use a LaTeX block. You
start one with <l and TAB
#+BEGIN_LaTeX
LaTeX code goes here
#+END_LaTeX
** Source code blocks
It is also handy to include source code in your notes - on a new line
type <s and TAB to create a source block. You can tell org what type
of code is contained - in this case we'll put in some simple shell
code, so well put "sh" at the top of the block.
#+BEGIN_SRC sh
echo "Hello $USER! Today is `date`"
exit
#+END_SRC
You can get org to syntax highlight the text in the block by adding
the following to your [[http://pragmaticemacs.com/emacs/editing-your-emacs-config-file/][emacs config file]] (without the source block
wrapper of course).
#+BEGIN_SRC elisp
;;syntax highlight code blocks
(setq org-src-fontify-natively t)
#+END_SRC
What is more, when the cursor is inside a SRC block, you can use C-c '
to create a new temporary buffer in the major mode of the programming
language you have specified. Type some code in, and then type C-c '
again to come back to this buffer.
** Executing source code blocks
Org-mode can execute your source code blocks and add the output to
your file. This part of org-mode is called babel. I'll write more
about this later, but it is too cool not to mention here.
For example, take the simple code block we had above:
#+BEGIN_SRC sh
echo "Hello $USER! Today is `date`"
exit
#+END_SRC
Put the cursor inside the block and hit C-c C-c to execute it. You
will be asked to confirm and then you should see the output appear
like this:
#+RESULTS:
#+begin_example
Hello bjm! Today is Fri 25 Sep 2015 15:03:12 BST
#+end_example
You can do much more with this, like reading input data from a table
in the same file, creating images that appear in the file, extracting
(tangling) all the code snippets into one or more files to be executed
separately, and much more. [[http://orgmode.org/worg/org-contrib/babel/intro.html][Here are some nice examples]].
You can tell org-mode which programming languages to support by adding
something like the following to your [[http://pragmaticemacs.com/emacs/editing-your-emacs-config-file/][emacs config file]]:
#+BEGIN_SRC elisp
;; Some initial languages we want org-babel to support
(org-babel-do-load-languages
'org-babel-load-languages
'(
(sh . t)
(python . t)
(R . t)
(ditaa . t)
(perl . t)
(gnuplot t)
))
#+END_SRC