You can include literal examples that should not be subjected to markup. Such examples are typeset in monospace, so this is well suited for source code and similar examples.
#+BEGIN_EXAMPLE Some example from a text file. #+END_EXAMPLE
There is one limitation, however. You must insert a comma right before lines starting with either ‘*’, ‘,*’, ‘#+’ or ‘,#+’, as those may be interpreted as outlines nodes or some other special syntax. Org transparently strips these additional commas whenever it accesses the contents of the block.
#+BEGIN_EXAMPLE ,* I am no real headline #+END_EXAMPLE
For simplicity when using small examples, you can also start the example lines with a colon followed by a space. There may also be additional whitespace before the colon:
Here is an example : Some example from a text file.
If the example is source code from a programming language, or any other text that can be marked up by Font Lock in Emacs, you can ask for the example to look like the fontified Emacs buffer114. This is done with the code block, where you also need to specify the name of the major mode that should be used to fontify the example115, see Structure Templates for shortcuts to easily insert code blocks.
#+BEGIN_SRC emacs-lisp (defun org-xor (a b) "Exclusive or." (if a (not b) b)) #+END_SRC
Both in ‘example’ and in ‘src’ snippets, you can add a ‘-n’ switch to the end of the ‘#+BEGIN’ line, to get the lines of the example numbered. The ‘-n’ takes an optional numeric argument specifying the starting line number of the block. If you use a ‘+n’ switch, the numbering from the previous numbered snippet is continued in the current one. The ‘+n’ switch can also take a numeric argument. This adds the value of the argument to the last line of the previous block to determine the starting line number.
#+BEGIN_SRC emacs-lisp -n 20 ;; This exports with line number 20. (message "This is line 21") #+END_SRC #+BEGIN_SRC emacs-lisp +n 10 ;; This is listed as line 31. (message "This is line 32") #+END_SRC
In literal examples, Org interprets strings like ‘(ref:name)’ as labels, and use them as targets for special hyperlinks like ‘[[(name)]]’—i.e., the reference name enclosed in single parenthesis. In HTML, hovering the mouse over such a link remote-highlights the corresponding code line, which is kind of cool.
You can also add a ‘-r’ switch which removes the labels from the source code116. With the ‘-n’ switch, links to these references are labeled by the line numbers from the code listing. Otherwise links use the labels with no parentheses. Here is an example:
#+BEGIN_SRC emacs-lisp -n -r (save-excursion (ref:sc) (goto-char (point-min)) (ref:jump) #+END_SRC In line [[(sc)]] we remember the current position. [[(jump)][Line (jump)]] jumps to point-min.
Source code and examples may be indented in order to align nicely with the surrounding text, and in particular with plain list structure (see Plain Lists). By default, Org only retains the relative indentation between lines, e.g., when exporting the contents of the block. However, you can use the ‘-i’ switch to also preserve the global indentation, if it does matter. See Editing Source Code.
If the syntax for the label format conflicts with the language syntax, use a ‘-l’ switch to change the format, for example
#+BEGIN_SRC pascal -n -r -l "((%s))"
See also the variable
HTML export also allows examples to be published as text areas (see Text areas in HTML export).
Because the ‘#+BEGIN’ … ‘#+END’ patterns need to be added so often, a shortcut is provided (see Structure Templates).
Edit the source code example at point in its native mode. This works by switching to a temporary buffer with the source code. You need to exit by pressing C-c ' again. The edited version then replaces the old version in the Org buffer. Fixed-width regions—where each line starts with a colon followed by a space—are edited using Artist mode117 to allow creating ASCII drawings easily. Using this command in an empty line creates a new fixed-width region.
org-store-link (see Handling Links) while editing a source
code example in a temporary buffer created with C-c '
prompts for a label. Make sure that it is unique in the current
buffer, and insert it with the proper formatting like ‘(ref:label)’ at
the end of the current line. Then the label is stored as a link
‘(label)’, for retrieval with C-c C-l.
This works automatically for the HTML backend (it requires
version 1.34 of the ‘htmlize.el’ package, which you need to install).
Fontified code chunks in LaTeX can be achieved using either the
listings package or the minted package. Refer to
org-latex-listings for details.
Source code in code blocks may also be evaluated either interactively or on export. See Working with Source Code for more information on evaluating code blocks.
Adding ‘-k’ to ‘-n -r’ keeps the labels in the source code while using line numbers for the links, which might be useful to explain those in an Org mode example code.
You may select a different mode with the variable