Graphics in Org mode using ditaa and TikZ
Contents
Org mode is well known for its powerful taking notes and task management capabilities, but for me it is always (or at least now) the document preparation mechanism that truly attracts me. Just imagine how amazing that we can just write content in org mode and then convert it to either PDF through LaTeX or HTML. Actually since Org mode is so good at these LaTeX tasks, I am even using the Org Beamer rather than the original LaTeX Beamer to prepare presentations. Another strong point of Org mode is Babel, which enables the execution of source code in Org file. This makes Org mode a good tool for literature programming.
There are different ways to include graphics in a Org originated document. For example, we can draw figures using TikZ directly, or we can use other programming languages or tools, such as python and ditaa embedded in Org mode’s source code block to generate figure. Following are some examples to include graphics in Org mode.
Source code in org-mode
Source code can be embedded and evaluated in org file through `src'
block. To start with source code evaluation, we first need to enable
interested languages in .emacs
configuration file (by default only
emacs-lisp is enabled):
|
|
The source code block can be exported either as the code block it self
or the evaluation results of it depending on the value of
:exports
.
The values could be code
(the default), results
, both
or none
.
Which kind of results are generated after evaluation depending on the
header argument
:results.
:exports code
If the following python code block is included in the org file:
|
|
the HTML output is like this:
|
|
:exports results
If the :exports
is specified as results
and :results
has value of
output
:
|
|
after evaluation of the source code block (C-c C-c) the HTML output is
|
|
If the results of a source code block needs to be exported, chances are we already evaluated the code to check its results. So when export, it is better to used the pre-evaluated results directly and avoid repeated evaluation again. By setting the following variable, when export, only inline code blocks will be evaluated. Non-inline code blocks are assumed to have their results already inserted in the buffer. An extra advantage of using =‘inline-only= is potential hazardous code may be avoided when export org file from unknown sources.
|
|
Using ‘ditta’ to draw diagrams
ditaa can convert diagrams drawn using ascii art into bitmap graphics. Example of using ditaa is shown below:
|
|
Include ‘tikz’ figures
pgf/tikz is another great software to draw professional graphics in
LaTeX document, and it’s also easy to be included in a LaTeX environment
in org file. Although tikz code can be put in a
#+BEGIN_LaTeX ... #+END_LaTeX
directly, but then the figure can only
be viewed when export the whole org file to pdf. The second method is to
put the tikz code of each figure in an individual TeX file, and include
figures generated by the code. The downside of this method is then we
have to keep all of those files and it may mess up easily.
The magic way to include, to me, is to put tikz in a LaTex source code
block, and when execute this code block, figures can be generated and
kept in a seperate ‘images’ folder without worrying about where to store
the TeX code. Since tikz generates pdf
format figure, imagemagic
needs to be used if we want to preview the figure in org mode (though we
will use pdf
later when export to html or pdf file).
|
|
The following emacs configuration code included several useful packages so we do not need to specify them for each latex source code block.
|
|
An source code block example for tikz
Simply using the following code, when evaluation the results will be
wrapped to a BEGIN_LaTeX ... END_LaTeX
environment.
|
|
|
|
The workaround is to specify :results
to use raw
instead of the
default latex
format.
|
|
|
|
Author Fanpeng Kong
LastMod 2015-10-23