Org mode

Table of Contents

1. Org-mode 简介

Org mode is for keeping notes, maintaining TODO lists, planning projects, and authoring documents with a fast and effective plain-text system.

org-mode manual: http://orgmode.org/manual/index.html
Reference Card: http://orgmode.org/worg/orgcard.html
Org Mode - Organize Your Life In Plain Text!: http://doc.norang.ca/org-mode.html
Emacs org-mode examples and cookbook: http://home.fnal.gov/~neilsen/notebook/orgExamples/org-examples.html
org-mode 最好的文档编辑利器,没有之一:http://www.cnblogs.com/holbrook/archive/2012/04/12/2444992.html
为什么说 org-mode 是个神器:http://www.cnblogs.com/bamanzi/archive/2012/05/12/org-mode-so-powerful.html

2. 大纲及相关快捷键

Org-mode 对大纲视图有非常好的支持,可以方便地浏览每个小节。连续几个*就表示是第几级大纲。如:

 * Top level headline
 ** Second level
 *** 3rd level
    some text
 *** 3rd level
    more text

 * Another top level headline
Table 1: org-mode 中折叠大纲相关快捷键
hotkey function description
S-TAB org-shifttab 循环切换整个文档的大纲状态(三种状态:折叠,打开下一级,打开全部)
TAB org-cycle 循环切换光标所属大纲的状态
C-c C-k show-branches 显示当前标题下的各级子标题,而所有正文均保持折叠,即只显示当前大纲下的骨架
Table 2: org-mode 中编辑大纲相关快捷键
hotkey function description
M-RET org-meta-return 插入一个同级标题
M-S-RET org-insert-todo-heading 插入一个同级 TODO 标题
M-LEFT/RIGHT org-metaleft/org-metaright 将当前标题升/降级
M-S-LEFT/RIGHT org-shiftmetaleft/org-shiftmetaright 将子树升/降级
M-S-UP/DOWN org-shiftmetaup/org-shiftmetadown 将子树上/下移
C-c * org-ctrl-c-star 将本行设为标题/正文
C-c C-w org-refile 将子树或区域移动到另一标题处(跨缓冲区)
C-x n s/w org-narrow-to-subtree/widen 只显示当前子树/返回(显示所有内容)
C-c C-x b org-tree-to-indirect-buffer 在新缓冲区显示当前分支(类似 C-x n s)
C-c / org-sparse-tree 只列出包含搜索结果的大纲,并高亮
C-c C-c org-ctrl-c-ctrl-c 取消高亮(该快捷键不同环境下有不同作用)
Table 3: org-mode 中在大纲之间移动相关快捷键
hotkey function description
C-c C-n/p outline-next-visible-heading/outline-previous-visible-heading 下/上一标题
C-c C-f/b org-forward-heading-same-level/org-backward-heading-same-level 下/上一标题(仅限同级标题)
C-c C-u outline-up-heading 跳到上一级标题
C-c C-j org-goto 切换到大纲浏览状态

3. 注释格式

org 文件中的行注释是“以井号#后加一个空格开始的行”,不会被导出。 #+BEGIN_COMMENT#+END_COMMENT 之间的内容是块注释,也不会被导出。如:

这是文本
#这是文本(井号后没有空格)
# 这是行注释
 #  这也是行注释
这是文本

#+BEGIN_COMMENT
这里都是注释
这里都是注释
这里都是注释
#+END_COMMENT

4. 定制字体样式

Org-mode 通过前后加标记的方式支持粗体,斜体,下划线,删除线,代码等宽字体等样式。
*bold*
/italic/
_underlined_
+strike-through+
~code~

效果分别如下:
bold
italic
underlined
strike-through
code

注:如果要防止转义可以在需要保护的文本前后加个=标记。

5. 插入列表

无序列表
以‘-’或‘+’开头。
有序列表
以数字加‘.’或‘)’开头。
描述列表
是一种无序列表,列表名称和描述内容用‘::’分开即可。

6. 插入超级链接

Org will recognize plain URL-like links and activate them as clickable links. The general link format, however, looks like this:

[[link][description]]
[[http://www.gnu.org/software/emacs/][GNU Emacs]]

参考:http://orgmode.org/org.html#Hyperlinks

7. 插入图片

用两个方括号包围图片文件名即可。

#+CAPTION: This is the caption for the next figure
#+NAME: fig:example
[[./images/example.jpg]]

CAPTION 和 NAME 都可以省略。如果要引用这个图片,把其名字放入到两个方括号中即可,如:参考图 [[fig:example]] 所示。

7.1. 指定导出时图片的宽度

如果插入的图片太宽,可以用类似下面的语句来限制导出 html 或 pdf(latex) 时图片的显示宽度。

#+ATTR_HTML: :width 500px
#+ATTR_LATEX: :width 500pt
[[./images/example.jpg]]

说明:latex 中不支持 px(像素,相对长度)作为长度单位,可以使用 pt(磅,绝对长度,1pt=1/72 英寸)作为长度单位。它们之间的转换和 DPI(每英寸像素数量)相关。假设 DPI 为 72,那么 1px=1pt。

7.1.1. 导出 html 时如何指定 svg 图片宽度

把 org 从 8.2.10 升级到 9.0.5 后,发现通过下面设置 svg 图片宽度的办法失效了(其它类型图片不受影响)。

#+ATTR_HTML: :width 200px
[[./images/example.svg]]

通过分析,发现在 org 8.2.10 中,上面代码在导出为 html 后会转换为带有 width 属性的 <img> 标签,能定制图片宽度。但在 org 9.0.5 中,上面代码导出 html 时会生成 <object> 标签,代码如下:

<object type="image/svg+xml" data="./images/example.svg" class="svgfig" width="200px">
Sorry, your browser does not support SVG.</object>

但问题在于 width="200px" 并不会对 object 标签中的 svg 图片生效。

如何在 org 9.0.5 中正确地设置 svg 图片在导出 html 时的宽度呢?解决办法:

#+HTML_HEAD_EXTRA: <style>.svg100px { width: 100px; }</style>     <!-- 暂时没用 -->
#+HTML_HEAD_EXTRA: <style>.svg200px { width: 200px; }</style>
#+HTML_HEAD_EXTRA: <style>.svg300px { width: 300px; }</style>     <!-- 暂时没用 -->
#+HTML_HEAD_EXTRA: <style>.svg400px { width: 400px; }</style>     <!-- 暂时没用 -->
#+HTML_HEAD_EXTRA: <style>.svg500px { width: 500px; }</style>     <!-- 暂时没用 -->
#+ATTR_HTML: :class svg200px
[[./images/example.svg]]

说明:上面这种方法需要对应的 svg 文件中存在 viewBox 属性(一般都存在 viewBox 属性,不存在时可以手动增加 viewBox 属性,它由“x y width height”四个部分组成,其中“x y”都设置为 0,“width height”设置为和 svg 对象大小相同),否则的话图片不会缩放,而是直接不显示超出宽度的部分内容。

参考:Scaling HTML-exported SVG

8. 插入表格

8.1. 转换文本为表格形式

Table 4: org-mode 中转换文本为表格形式
hotkey function description
C-c | org-table-create-or-convert-from-region Convert the active region to table(假设每列以 TAB 键或逗号分开)

8.2. 插入 horizontal separator line

| Name  | Phone | Age |
|-------+-------+-----|
| Peter |  1234 |  17 |
| Anna  |  4321 |  25 |

以上面表格为例,如何快速插入 horizontal separator line 呢?
方法 1:在第二行中键入|-后按 TAB 键。
方法 2:在第一行中键入|Name|Phone|Age 后按 C-c <RET>.

参考:http://orgmode.org/manual/Built_002din-table-editor.html#Built_002din-table-editor

8.3. 列的对齐方式

默认地,数字列是右对齐,而字符列是左对齐。不过,这可以通过在表格第一行(不会导出)加特别的标记来定制对齐方式。如:

#+CAPTION: 定制对齐方式
| <l>   |        <r> |
|       |     右对齐 |
|-------+------------|
| 10    | 这列右对齐 |
| 20000 |     右对齐 |

8.4. 让表格中的超长部分不换行

如何让 org-mode 中表格显示很长的行时不换行,而是隐藏超长部分?

M-x toggle-truncate-lines

但不在表格中的很长的行也会隐藏超长部分。

8.5. 如何在表格中插入|

To insert a vertical bar into a table field, use \vert.

参考:http://orgmode.org/manual/Built_002din-table-editor.html#fn-1

8.6. 表格内容按列排序

把光标移动到需要排序的列中,执行下面命令即可按此列排序:

M-x org-sort

8.7. 表格中的数学运算

8.7.1. 实例:统计某行

假设有下面表格,我们想要在最后一列(第 4 列)中计算出第 2 列和第 3 列的平均值。

| Student  | Maths | Physics | Mean |
|----------+-------+---------+------|
| Bertrand |    13 |      09 |      |
| Henri    |    15 |      14 |      |
| Arnold   |    17 |      13 |      |

如何实现呢?在表格下面增加“#+TBLFM: $4=vmean($2..$3)”,按下快捷键 C-u C-c * 即可把第 4 列填充为第 2 列和第 3 列的平均值(注:按快捷键前应确保光标在表格中)。

| Student  | Maths | Physics | Mean |
|----------+-------+---------+------|
| Bertrand |    13 |      09 |   11 |
| Henri    |    15 |      14 | 14.5 |
| Arnold   |    17 |      13 |   15 |
#+TBLFM: $4=vmean($2..$3)

8.7.2. 实例:统计某列

如有下面表格,我们想要在最后一行中计算前面 4 行的和。

|       | Q1 | Q2 | Q3 | Q4 |
|-------+----+----+----+----|
| item1 | 10 |  0 |  8 |  2 |
| item2 | 18 | 15 | 20 | 11 |
| item3 | 12 | 21 | 32 | 31 |
| item4 | 11 | 21 | 28 | 24 |
|-------+----+----+----+----|
| sum   |    |    |    |    |

如何实现呢?在表格下面输入下面公式后,按下快捷键 C-u C-c * 即可。

|       | Q1 | Q2 | Q3 | Q4 |
|-------+----+----+----+----|
| item1 | 10 |  0 |  8 |  2 |
| item2 | 18 | 15 | 20 | 11 |
| item3 | 12 | 21 | 32 | 31 |
| item4 | 11 | 21 | 28 | 24 |
|-------+----+----+----+----|
| sum   | 51 | 57 | 88 | 68 |
#+TBLFM: @>$2=vsum(@2$2..@5$2)::@>$3=vsum(@2$3..@5$3)::@>$4=vsum(@2$4..@5$4)::@>$5=vsum(@2$5..@5$5)

说明:
(1) :: 是多个公式的分隔符;
(2) @n$m 表示第 n 行第 m 列;
(3) @> 表示最后一行,例如在这里 @>$2 相当于 @6$2

8.7.3. 实例:使用 Elisp 函数

在公式中可以使用 Elisp 函数。如下表中第三列可以由前二列直接生成。

| First name | Last Name | Email                |
|------------+-----------+----------------------|
| John       | Doe       | John.Doe@emacs.edu   |
| Jennie     | Duh       | Jennie.Duh@emacs.edu |
#+TBLFM: $3='(concat $1 "." $2 "@emacs.edu")

8.8. 插入 tabel.el 表格

从 Emacs 22 起,内置了一个强大的基于文本的表格(tabel.el 表格),详情可参考Emacs Text-based Tables
tabel.el 表格能支持一些复杂的特性,如合并或拆分单元格( M-x table-span-cellM-x table-split-cell )。tabel.el 表格实例如下:

table-capture is a powerful command.                      
Here are some things it can do:                           
Parse Cell Items 
                 
                 
 Using row and column delimiter regexps,
 it parses the specified text area and  
 extracts cell items into a table.      

在 org-mode 中可用下面命令插入或编辑 table.el 表格。

Table 5: Insert and edit table.el table in org-mode
hotkey function description
C-c ~ org-table-create-with-tabel.el Insert a table.el table. If there is already a table at point, this command converts it between the table.el format and the Org mode format.
C-c ' org-edit-special Edit a table.el table. Works when the cursor is in a table.el table.

不过,测试时发现 tabel.el 表格不支持“#+CAPTION”的用法。

参考:http://orgmode.org/manual/Cooperation.html

8.9. 导出 html 表格时定制边框

Org mode tables are exported to HTML using the table attributes defined in org-html-table-default-attributes. The default setting makes tables without cell borders and frame. If you would like to change this for individual tables, place something like the following before the table:

#+CAPTION: 这个表格显示全部的边框
#+ATTR_HTML: :border 2 :rules all :frame border

参考:http://orgmode.org/manual/Tables-in-HTML-export.html#Tables-in-HTML-export

8.9.1. 导出 html 时只显示内部边框

#+CAPTION: 这个表格只显示内部的边框
#+ATTR_HTML: :border 1 :rules all :frame void

参考:http://stackoverflow.com/questions/1257430/css-border-only-inside-the-table

8.10. 导出 html 表格时使用“等宽”字体

在表格前面指定“#+ATTR_HTML: :style font-family:"Courier New", Courier, monospace;”可以设置为导出 html 时使用“等宽”字体。

Table 6: “等宽”字体示例
column1 column2
1 abcde
2 ABCDE

参考:http://www.w3schools.com/css/css_font.asp

8.11. 导出 pdf 表格时让内容过长的列自动换行

默认地,如果表格有某列的内容过长,在导出 pdf 时,表格会溢出。
如何让其自动换行呢?
方法一(推荐):在 align 属性中通过 p{'width'} 明确指定列的宽度。如下面例子设置 column1 为左对齐,column2/column3 所占宽度为 0.3/0.4 倍行宽:

#+ATTR_LATEX: :align lp{0.3\linewidth}p{0.4\linewidth}
#+CAPTION: 通过使用tabularx可以使表格中很长的内容自动换行
| column1 | column2                                                               | column3                                                                                                            |
|---------+-----------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------|
| test    | very long long long long long long long long long long long long long | very long long long long long long long long long long long long long long long long long long long long long long |

方法二:
修改表格的环境为 tabularx (注:tabularx 的 width 属性是必须指定的),对过长的那一列指定 align 属性为 X ,这可以使其自动换行。如:

#+ATTR_LATEX: :environment tabularx :width \linewidth :align lXX
#+CAPTION: 通过使用tabularx可以使表格中很长的内容自动换行
| column1 | column2                                                               | column3                                                                                                            |
|---------+-----------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------|
| test    | very long long long long long long long long long long long long long | very long long long long long long long long long long long long long long long long long long long long long long |

采用方法二时,需要把 tabularx 加入到 org-latex-packages-alist 中。

参考:
https://en.wikibooks.org/wiki/LaTeX/Tables
http://orgmode.org/manual/LaTeX-specific-attributes.html

8.12. 导出 pdf 表格时设置字体大小

通过指定 :font 属性,可以设置 pdf 表格的字体大小。如:

#+ATTR_LATEX: :font \scriptsize
| Name  | Phone | Age |
|-------+-------+-----|
| Peter |  1234 |  17 |
| Anna  |  4321 |  25 |

下面是一些字体大小的可选设置(从上到下字体变小):

\Huge
\huge
\LARGE
\Large
\large
\normalsize
\small
\footnotesize
\scriptsize
\tiny

9. 插入公式

要插入行内公式,把公式放入到 $$ 之间,或者 \(\) 之间即可。如: $a^{2} + b^{2} = c^{2}$ ,会显示: \(a^{2} + b^{2} = c^{2}\)
要插入行间公式,把公式放入到 $$$$ 之间,或者 \[\] 之间即可。如: $$a^{2} + b^{2} = c^{2}$$ ,会把公式显示在单独的一行中。

若用下面的形式,则需要 org-exp-blocks.el 支持:

#+begin_equation
...
#+end_equation

10. 插入 LaTex 片断

\begin 开始的 LaTex 片断,可以直接写在 org 文件中。如:

Following is math equation.

\begin{equation}
 x=\sqrt{b}
\end{equation}

参考:http://orgmode.org/manual/LaTeX-fragments.html

11. 插入程序源码

org 中可插入代码块,其格式为:

#+NAME: <name>
#+BEGIN_SRC <language> <switches> <header arguments>
  <body>
#+END_SRC

参考:http://orgmode.org/org.html#Structure-of-code-blocks

11.1. switches 实例:显示源码行号和引用行号

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.
You can also add a -r switch which removes the labels from the source code.

指定 -n 可显示源码行号,指定 -r 可对源码行号进行引用。

orgmode_switch_example.png

Figure 1: switches 实例:显示源码行号和引用行号

如图 1 所示 org 源码会导出为:

1: (save-excursion
2:    (goto-char (point-min)))

In line 1 we remember the current position. Line 2 jumps to point-min.

参考:http://orgmode.org/manual/Literal-examples.html

11.2. Header arguments

11.3. Evaluating code blocks

Code blocks can be evaluated and the results of evaluation optionally placed in the Org mode buffer. The results of evaluation are placed following a line that begins by default with #+RESULTS and optionally a cache identifier and/or the name of the evaluated code block.

By default, the evaluation facility is only enabled for Lisp code blocks specified as emacs-lisp.

There are a number of ways to evaluate code blocks. The simplest is to press C-c C-c or C-c C-v e with the point on a code block. This will call the org-babel-execute-src-block function to evaluate the block and insert its results into the Org mode buffer.

Named code blocks can be evaluated with a separate #+CALL: line or inline within a block of text.
The syntax of the #+CALL: line is

#+CALL: <name>(<arguments>)
#+CALL: <name>[<inside header arguments>](<arguments>) <end header arguments>

The syntax for inline evaluation of named code blocks is

... call_<name>(<arguments>) ...
... call_<name>[<inside header arguments>](<arguments>)[<end header arguments>] ...

11.4. 快速输入#+BEGIN_SRC … #+END_SRC

输入 <s 再按 TAB 键,就会自动展开为 #+BEGIN_SRC ... #+END_SRC。类似地,还有其它的一些展开序列。如表 7 所示。

Table 7: org-mode 中的 Easy Templates
Sequence Expands to
<s #+BEGIN_SRC ... #+END_SRC
<e #+BEGIN_EXAMPLE ... #+END_EXAMPLE
<q #+BEGIN_QUOTE ... #+END_QUOTE
<v #+BEGIN_VERSE ... #+END_VERSE
<c #+BEGIN_CENTER ... #+END_CENTER
<l #+BEGIN_LaTeX ... #+END_LaTeX
<L #+LaTeX:
<h #+BEGIN_HTML ... #+END_HTML
<H #+HTML:
<a #+BEGIN_ASCII ... #+END_ASCII
<A #+ASCII:
<i #+INDEX: line
<I #+INCLUDE: line

11.5. Code Blocks 中支持的程序语言

8 列出了 Code Blocks 中支持的程序语言。

Table 8: Code Blocks 中支持的程序语言
Language Identifier Language Identifier
Asymptote asymptote Awk awk
Emacs Calc calc C C
C++ C++ Clojure clojure
CSS css ditaa ditaa
Graphviz dot Emacs Lisp emacs-lisp
gnuplot gnuplot Haskell haskell
Java java LaTeX latex
Javascript js Lisp lisp
Ledger ledger MATLAB matlab
Lilypond lilypond Objective Caml ocaml
Mscgen mscgen Org mode org
Octave octave Perl perl
Oz oz Python python
Plantuml plantuml Ruby ruby
R R Scheme scheme
Sass sass shell sh
GNU Screen screen SQLite sqlite
SQL sql    

参考:http://orgmode.org/manual/Languages.html#Languages

12. Babel

Babel is Org-mode's ability to execute source code within Org-mode documents.

Babel 支持的语言及对应的文档:
http://orgmode.org/worg/org-contrib/babel/languages.html

12.1. 对某种语言启用 Babel

The option org-babel-load-languages controls which languages are enabled for evaluation (by default only emacs-lisp is enabled). This variable can be set using the customization interface or by adding code like the following to your emacs configuration.

The following disables emacs-lisp evaluation and enables evaluation of R and ditaa code blocks.

(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . nil)
   (ditaa . t)
   (R . t)))

13. Org-mode 小技巧

13.1. 导出 html

13.1.1. 原封不动地导出 html

“#+BEGIN_EXPORT html”和“#+END_EXPORT”之间的内容会原封不动地导出为 html。

#+BEGIN_EXPORT html
All lines between these markers are exported literally
#+END_EXPORT

参考:http://orgmode.org/manual/Quoting-HTML-tags.html

13.1.2. 导出带颜色的文本

下面是导出带颜色的文本的实例(仅对导出 html 有效,导出 pdf 无效):

@@html:<font color = "red">@@这是红色文本。不过仅导出html时有效,导出pdf时无效(导出pdf时会自动删除周围的html标记)。@@html:</font>@@

参考:http://orgmode.org/manual/Quoting-HTML-tags.html

13.1.3. 导出 html 时代码块用等宽字体

按 org mode 的文档说明,在导出代码块时会采用等宽字体。但测试(org mode 8.2.10)时发现导出 html 时#+BEGIN_SRC ... #+END_SRC 之间和#+BEGIN_EXAMPLE ... #+END_EXAMPLE 之间的内容并不是等宽字体,可以在 org 文件开头处加入下面设置来显示地输出等宽字体:

#+HTML_HEAD: <style>.src {font-family:courier;}</style>
#+HTML_HEAD: <style>.example {font-family:courier;}</style>

参考:http://superuser.com/questions/726201/how-can-i-apply-easy-formatting-to-org-mode-blocks

13.2. 导出 pdf

org 文件可以导出为 pdf。步骤是先生成 tex 文件,再由 LaTex 生成 pdf 文件。

The default LaTeX output is designed for processing with pdftex or latex. The LaTeX exporter can be configured to support alternative TeX engines, see org-latex-pdf-process, and alternative packages, see org-latex-default-packages-alist and org-latex-packages-alist.

参考:http://orgmode.org/manual/LaTeX-and-PDF-export.html#LaTeX-and-PDF-export

13.2.1. 定义自己的 LaTex 导出模板

13.4. 万能的 C-c C-c

The key C-c C-c has many purposes in Org. Here is a summary of what this means in different contexts.

  • If there are highlights in the buffer from the creation of a sparse tree, or from clock display, remove these highlights.
  • If the cursor is in one of the special #+KEYWORD lines, this triggers scanning the buffer for these lines and updating the information.
  • If the cursor is inside a table, realign the table. This command works even if the automatic table editor has been turned off.
  • If the cursor is on a #+TBLFM line, re-apply the formulas to the entire table.
  • If the current buffer is a capture buffer, close the note and file it. With a prefix argument, file it, without further interaction, to the default location.
  • If the cursor is on a target, update radio targets and corresponding links in this buffer.
  • If the cursor is in a property line or at the start or end of a property drawer, offer property commands.
  • If the cursor is at a footnote reference, go to the corresponding definition, and vice versa.
  • If the cursor is on a statistics cookie, update it.
  • If the cursor is in a plain list item with a checkbox, toggle the status of the checkbox.
  • If the cursor is on a numbered item in a plain list, renumber the ordered list.
  • If the cursor is on the #+BEGIN line of a dynamic block, the block is updated.
  • If the cursor is at a timestamp, fix the day name in the timestamp.

参考:http://orgmode.org/manual/The-very-busy-C_002dc-C_002dc-key.html

13.5. 引用前文中的节

如果后文要引用前文中的节,可以这样:

 * Section One
  <<sec:one>>

 * Section Two
  <<sec:two>>

I can reference Section One with  [[sec:one]] and [[sec:one][Section One]].

参考:http://stackoverflow.com/questions/16346622/how-can-i-reference-a-section-by-number-in-org-mode-export

13.6. 插入时间戳

使用快捷键 C-c . 或直接执行函数 M-x org-time-stamp

Author: cig01

Created: <2012-11-03 Sat>

Last updated: <2018-11-10 Sat>

Creator: Emacs 27.1 (Org mode 9.4)