ASCII Art Diagrams in Emacs org-mode
ASCII art can do a lot more than just make silly pictures. See how Emacs and a little Java-written tool called ditaa can level up your ASCII creations.
Join the DZone community and get the full member experience.Join For Free
yesterday i wrote about ascii art diagrams and gave four reasons you might want to use this ancient approach to creating simple diagrams:
- it could be quicker than creating a graphical image.
- you can paste them into plain text documents like source code files.
- they can be produced programmatically.
- there is software to turn ascii art into more polished images.
today i’ll post a few notes about how to create graphical versions of ascii diagrams in emacs with org-mode.
running code inside org-mode
you can embed and execute source code in org-mode files. i wrote a couple posts about this, one showing how to run python and r inside org-mode and another showing how to mix languages in org-mode . the latter shows perl calling r and python, all in 14 lines of code.
there are currently 39 programming languages that org-mode can call by default. in addition to conventional programming languages like the ones listed above, org-mode also supports ditaa , the language that treats ascii art as a specification language to produce graphics.
you can edit code blocks just as you would other text in an org-mode file. but if you’d like to edit a code block in its language’s mode, type
from inside the code block. if you’re editing python code, for example, this will open a new window, in python mode, containing just that code block. if you type
inside a ditaa code block, emacs opens a new window in “artist mode,” a mode specifically for editing ascii art.
you can run code inside org-mode two ways: interactively and for export. with your cursor inside a block of code, type
to execute the code and report the results. you can also export an entire org-mode document and have the results of code execution embedded in the final document. this works much the same as “*weave” projects like
. but while each of these is specific to a particular programming language (r, python, and haskell respectively), org-mode works with dozens of languages, including ditaa.
running ditaa inside org-mode
you embed ditaa code just like you’d embed any other code. in the first post mentioned above, i gave this example of calling r:
#+begin_src r sqrt(42) #+end_src
here’s the analogous code for ditaa:
#+begin_src ditaa :file foo.png +-------+ | hello | +-------+ #+end_src
the markers to begin and end a source code segment can be upper or lower case. i used lower case in my previous post, but it may be more readable to use upper case so that the markers stand out better from their arguments.
the r example above didn’t use any header arguments, though it could have. with ditaa, you must provide a header argument: the name of the file to save the graphics in.
if you run the ditaa code by navigating inside the code block and running
, emacs will add a couple lines after the code block:
this is the literal text, what you’d see if you opened the file in another editor. but org-mode uses the double brackets for links. you wouldn’t see the double brackets. instead you’d see a hyperlink with text
. clicking on that link opens the image.
you can export the org-mode file with the command
. this brings up a menu of export options:
for latex, etc. within each of these are further options. for example, you could type
for latex and then type
again to select export to latex or type
to have org-mode run latex on the file and produce a pdf. if you know you want a pdf, you could do this all in one command:
c-c c-l l p
you can control whether org-mode exports code or the results of running code (or both or neither). org-mode exports the results of ditaa code, i.e. graphics files, by default. this makes sense: your pdf file will have a nice image version of your diagram, not the ascii art used as its source code.
configuration and troubleshooting
by default, the only programming language you can run inside org-mode is emacs lisp. this makes sense. you want to be deliberate about what code you run, but if you don’t want to run emacs lisp you’d better not run emacs!
inside your emacs configuration file, you specify what languages org-mode is allowed to run. here’s an example allowing python and ditaa:
(org-babel-do-load-languages 'org-babel-load-languages '( (python . t) (ditaa . t)) )
recent versions of emacs are supposed to ship with ditaa, but the ditaa jar file was missing from the two computers i experimented with. emacs complained
org-babel-execute:ditaa: could not find ditaa.jar at ...
and so i copied
to the place where emacs said it was looking for it. that worked, but it’s kind of a kludge because now i had two copies of the jar file. a better solution is to tell emacs where you already have ditaa.
i like to use the exact same
file on every machine i use and so i added a section where i put os-specific and machine-specific configuration. here i put a different path to ditaa for each os.
;;---------------------------------------------------------------------- ;; os specific (cond ((string-equal system-type "windows-nt") ; microsoft windows (progn (setq-default ispell-program-name "c:/bin/aspell/bin/aspell.exe") # etc. (setq org-ditaa-jar-path "c:/bin/ditaa/ditaa.jar") ) ) ((string-equal system-type "gnu/linux") ; linux (progn (setq x-select-enable-clipboard t) # etc. (setq org-ditaa-jar-path "/usr/bin/ditaa") ) ) )
Published at DZone with permission of John Cook, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.