Create diagrams in emacs org mode

Being able to explain something with your words only prevails. For those other cases, here’s a way to do it with diagrams in emacs org mode.

Figure 1: Gantt Diagram

Figure 1: Gantt Diagram

Figure 1 was generated from this text:

#+name: gantt_diagram
#+BEGIN_SRC mermaid :file /tmp/gantt.png
gantt
    dateFormat  YYYY-MM-DD
    title  Acme Project Plan

    section Period 3
        COMP112358            :done,  des1, 2099-01-29, 2099-03-07

    section Period 4
        Supervisory Meetings (term) :active, sup1, 2099-03-08, 2099-05-04
        POP Submission :sub1, 2099-05-10, 2099-05-11

    section Exams
        Exam Period :exams, 2099-05-06, 2099-06-06
        Semester End : 2099-06-08, 2099-06-09

    section Summer
        Supervisory Meetings : sup2, 2099-06-09, 2099-09-07
        Dissertation Submission :sub2, 2099-09-06, 2099-09-07
#+END_SRC

Mermaid-cli1

yay mermaid-cli

Mermaid for org mode2

Clone layer into ~/.emacs.d/private:

git clone git@github.com:arnm/mermaid-layer.git ~/.emacs.d/private/mermaid

Add mermaid layer to dotspacemacs-configuration-layers

;; ~/.spacemacs.d/init.el
   dotspacemacs-configuration-layers
   '(..
     mermaid
     ..)

Get mmdc executable path:

which mmdc
/usr/bin/mmdc

Set mmdc executable path:

;; ~/.spacemacs.d/init.el
(setq ob-mermaid-cli-path "/usr/bin/mmdc")

Usage

Open an org-mode buffer, create an org-babel source block and run org-babel-execute-src-block.

#+begin_src mermaid :file /temp/test.png
sequenceDiagram
A-->B: Works!
#+end_src

Export the org-mode document or invoke the org-babel-execute-src-block function to generate a diagram.

Problem solving

  • Check your Messages buffer

  • Test if mermaid-cli works independently from emacs

    printf "graph TD;\nA-->B;" > /tmp/input.mmd
    mmdc -i /tmp/input.mmd -o /tmp/output.png
    echo "file://"/tmp/output.png