使用 emacs org-mode 畫圖


概述

org畫圖主要有以下相關模塊

  1. artist-mode/picture-mode: 生成 ascii 圖
  2. ditaa: 根據ascii圖生成圖片
  3. graphviz: 生成關系圖
  4. plantuml: 基於graphiz之上,生成uml圖

當采用 org-mode 時,可以使用 #+BEGIN_SRC #+END_SRC 標簽生成ditaa,graphiz,plantuml圖

配置及相關准備工作

語言設置

設置 org-babel-load-languages 變量 增加允許語言設置, ditaa 為 t

(org-babel-do-load-languages
  'org-babel-load-languages
  '(;; other Babel languages
    (ditaa . t)
    (plantuml . t)
    (dot . t)
    (xxx . t)
    ))

這樣設置后,可以按照以下方式調用

#+BEGIN_SRC ditaa :file some_filename.png :cmdline -r -s 0.8
#+BEGIN_SRC plantuml :file export-file-name :cmdline -charset UTF-8

如果不進行以上設置,通過begin_src將不會成功,需要按照以下方式調用:

#+begin_ditaa test.png -r -s 0.8

不進行安全性提示

生成圖像時不予提示

(setq org-confirm-babel-evaluate nil)

建立 yas 快捷鍵

  1. dita
#+BEGIN_SRC ditaa :file ${1:export-file-name} :cmdline -r -s 0.8 
${0}
#+END_SRC 
  1. dot
#+BEGIN_SRC dot :file ${1:export-file-name}.png :cmdline -Kdot -Tpng
title ${0}
#+END_SRC 
  1. uml
#+BEGIN_SRC plantuml :file ${1:export-file-name} :cmdline -charset UTF-8
title ${0}
#+END_SRC

jar 包路徑設置

(setq org-plantuml-jar-path
      (expand-file-name "~/.emacs.d/scripts/plantuml.jar"))
(setq org-ditaa-jar-path (format "%s%s" (if *cygwin* "c:/cygwin" "")
                                       (expand-file-name "~/.emacs.d/elpa/contrib/scripts/ditaa.jar")) )

預覽圖像

(add-hook 'org-babel-after-execute-hook 'bh/display-inline-images 'append)

; Make babel results blocks lowercase
(setq org-babel-results-keyword "results")

(defun bh/display-inline-images ()
  (condition-case nil
      (org-display-inline-images)
    (error nil)))

此時輸入 C-c C-c 生成圖像, C-c C-x C-v 開關內聯圖像顯示

ditaa

實現原理

先通過artist-mode或者picture-mode生成ascii文本,然后

通過調用java 包 ditaa.jar 生成 圖片,並將圖片插入到 html 代碼中. 如以下示例,實際調用的腳本是

java -Dfile.encoding=UTF-8 -jar /path/ditaa.jar -r -s 0.8 c\:/Users/ADMINI\~1/AppData/Local/Temp/babel-124Lpi/ditaa-124xjt e\:/gitroot/doc/org/share/emacs/ditaa_test.png

當前版本為 0.9

ditaa使用說明

參數說明
param long param desc
-e –encoding 指定編碼
-E –no-separation 嵌套矩形是否分隔,缺省有分隔,設置后無
-r –round-corners 圓角矩形
-s –scale 矩形大小 ,比如 0.8
-o –overwrite 如果有同名文件覆蓋
-S –no-shadows
-A –no-antialias
-v –verbose
-h –help
-t –tabs
-h –html

圖形規則

  1. 圓角矩形: 用/ \ 作為圖形的四個頂角
  2. color
    • cxxx xxx別表示RGB
    • 也可以采用預設的值: cRED cGRE cBLK cBLU cPNK cYEL
  3. tags 在矩形內部標記:
    • {d} document
    • {s} storage
    • {io} input/output
  4. dashed lines 虛線 豎虛線 :: : 橫虛線 :: =
  5. *
  6. 矩形內的文本: o :: · 號

示例

#+begin_src ditaa :file test_ditaa.png :cmdline -r -s 0.8

    +-----------+        +---------+
    |    PLC    |        |         |
    |  Network  +<------>+   PLC   +<---=---------+
    |    cRED   |        |  c707   |              |
    +-----------+        +----+----+              |
                              ^                   |
                              |                   |
                              |  +----------------|-----------------+
                              |  |                |                 |
                              v  v                v                 v
      +----------+       +----+--+--+      +-------+---+      +-----+-----+       Windows clients
      |          |       |          |      |           |      |           |      +----+      +----+
      | Database +<----->+  Shared  +<---->+ Executive +<-=-->+ Operator  +<---->|cYEL| . . .|cYEL|
      |   c707   |       |  Memory  |      |   c707    |      | Server    |      |    |      |    |
      +--+----+--+       |{d} cGRE  |      +------+----+      |   c707    |      +----+      +----+
         ^    ^          +----------+             ^           +-------+---+
         |    |                                   |
         |    +--------=--------------------------+
         v
+--------+--------+
|                 |
| Millwide System |            -------- Data ---------
| cBLU            |            --=----- Signals ---=--
+-----------------+

#+END_SRC

生成結果

https://images0.cnblogs.com/blog/485361/201301/27161349-3c9f33a1613947078d7c1037246ba62a.jpg

graphviz

  1. homepage 當前版本 2.28.0

示例

#+BEGIN_SRC dot :file test_graphviz.png :cmdline -Kdot -Tpng
digraph G {
  size="8,6"
  ratio=expand
  edge [dir=both]
  plcnet [shape=box, label="PLC Network"]
  subgraph cluster_wrapline {
    label="Wrapline Control System"
    color=purple
    subgraph {
    rank=same
    exec
    sharedmem [style=filled, fillcolor=lightgrey, shape=box]
    }
    edge[style=dotted, dir=none]
    exec -> opserver
    exec -> db
    plc -> exec
    edge [style=line, dir=both]
    exec -> sharedmem
    sharedmem -> db
    plc -> sharedmem
    sharedmem -> opserver
  }
  plcnet -> plc [constraint=false]
  millwide [shape=box, label="Millwide System"]
  db -> millwide

  subgraph cluster_opclients {
    color=blue
    label="Operator Clients"
    rankdir=LR
    labelloc=b
    node[label=client]
    opserver -> client1
    opserver -> client2
    opserver -> client3
  }
}

#+end_src

https://images0.cnblogs.com/blog/485361/201301/27161352-25d17a200fdf4b2c9049799d71dbf9df.jpg

plantuml

  1. homepage 當前版本 7952

配置

示例

#+BEGIN_SRC plantuml :file test_uml.png  :cmdline -charset UTF-8
title Example Sequence Diagram
activate Client
Client -> Server: Session Initiation
note right: Client requests new session
activate Server
Client <-- Server: Authorization Request
note left: Server requires authentication
Client -> Server: Authorization Response
note right: Client provides authentication details
Server --> Client: Session Token
note left: Session established
deactivate Server
Client -> Client: Saves token
deactivate Client
#+END_SRC

https://images0.cnblogs.com/blog/485361/201301/27161355-092b3993d45c4577ab92050c1c052236.jpg

致謝

感謝 Open Source 提供的 cnblogs 插件, 並非常熱心的及時解決了圖片的發送問題!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM