Org-mode 显示任意颜色的字体

先上效果图:

目前只能使用 latex 或 html 导出, 背景不支持 latex 导出

在 org 文件开头加上两个 macros:

#+macro: color @@html:<span style="color:$1">$2</span>@@@@latex:\textcolor{$1}{$2}@@
#+macro: bgcolor @@html:<span style="background-color:$1;">$2</span>@@@@latex:$2@@

以下的代码为新的 syntax 增加高亮

(font-lock-add-keywords
 'org-mode
 '(("{{{[ \n]*\\(bg\\)?color[ \n]*(\\([#0-9a-zA-Z]+\\)[ \n]*,\\([^,]+?\\))}}}"
    (2 font-lock-comment-face t t)
    (3 (let ((bg (match-string 1))
             (color (match-string 2)))
         (list (if (equal bg "bg") :background :foreground) color))
       prepend t)))
 ;; 这里必须是 non-nil 否则被 org-macros 的 font-lock 覆盖
 'append)

这个链接里有更多的 org-macros

5 个赞

font-lock 代码改为

(font-lock-add-keywords
   'org-mode
   '(("{{{[ \n]*\\(bg\\)?color[ \n]*(\\([#0-9a-zA-Z]+\\)[ \n]*,\\([^,]+?\\))}}}"
      (2 (progn
           (put-text-property (match-beginning 2) (match-end 2)
                              'display
                              (car org-script-display))
           font-lock-comment-face)
         t t)
      (3 (let ((bg (match-string 1))
               (color (match-string 2)))
           (add-text-properties (match-beginning 0) (match-beginning 2)
                                '(invisible org-link))
           (add-text-properties (match-end 3) (match-end 0)
                                '(invisible org-link))
           (add-text-properties (- (match-beginning 3) 1) (match-beginning 3)
                                '(invisible org-link))
           (list (if (equal bg "bg") :background :foreground) color))
         prepend t)))
   'append)

可以显示为

2 个赞

这个帅呆了 :joy: