monospace font in tables and source code blocks in org-mode, proportional font in other parts

Emacs term popular term use
variable pitch font proportional font usual text
fixed pitch font monospace font source code & ascii art

1 Enabling proportional font for text-mode buffers

(add-hook 'text-mode-hook 'variable-pitch-mode)

With that, every major mode, including org-mode, inheriting from text-mode will display buffers in proportional font.

2 Using monospace font for tables and code blocks, while still using variable-pitch-mode in org mode buffers

(defun my-adjoin-to-list-or-symbol (element list-or-symbol)
  (let ((list (if (not (listp list-or-symbol))
                  (list list-or-symbol)
    (require 'cl-lib)
    (cl-adjoin element list)))

(eval-after-load "org"
    (lambda (face)
       face nil
        (face-attribute face :inherit))))
    (list 'org-code 'org-block 'org-table 'org-block-background)))

Now org buffers text should be displayed in proportional font, except for tables and source blocks which are displayed in monospace font.

If you see glitches in tables and source blocks, or if you find the text not anti-aliased, apply this bug fix for fixed-pitch

3 How does the code work?

It adds the face fixed-pitch to the inherit attribute of the faces: org-code, org-block, org-table, and org-block-background.

How does one find out that those four faces are the faces to target? This command

M-x describe-face

lists all faces active at point.

I did not check the other three, but when I checked org-code face, its inherit attribute was the symbol shadow and shadow is yet another face name. The above code replaces that attribute with the list:

(fixed-pitch shadow)

so that the combined effect of fixed-pitch and shadow applies to org-code face.

Does this break anything? Yes, I found that when I export as html an org file which contains a source block whose contents is itself an org buffer which contains a source block, Emacs reports error and says that (fixed-pitch shadow) is not a symbol. Probably related to htmlize.

4 Info buffers

Why not enable variable-pitch-mode on Info buffers as well, now that we know how to make parts of text to be in monospace font.

;;; display Info mode buffers in proportional font
(add-hook 'Info-mode-hook 'variable-pitch-mode)

;;; but code examples in monospace font
(defvar my-rx-info-code (rx bol "     " (* not-newline) eol))
(add-hook 'Info-mode-hook 'my-Info-font-lock)
(defun my-Info-font-lock ()
  (require 'org)
      ;; let's just use org-block
      (quote org-block)

;;; following doesn't work. don't know why
;; (font-lock-add-keywords
;;  'Info-mode
;;  `((,my-rx-info-code
;;     .
;;     (quote org-block)
;;     )))

The regexp my-rx-info-code matches lines starting with five spaces. It intends to match emacs lisp code examples, but there are lots of false positives as well.

5 Markdown mode buffers

To display source code blocks or pre blocks in monospace:

(eval-after-load "markdown-mode"
    (lambda (face)
       face nil
        (face-attribute face :inherit))))
    (list 'markdown-pre-face 'markdown-inline-code-face)))
This entry was posted in Emacs and tagged , . Bookmark the permalink.

6 Responses to monospace font in tables and source code blocks in org-mode, proportional font in other parts

  1. Heinrich says:

    Wow, that is a great addition! Maybe ‘org-date could also be added in eval-after-load “org”, as it might also occur in tables and ruin the formatting.

  2. Pingback: how to make rainbow-delimiters-mode work with org-mode export or htmlize | Yoo Box

  3. Xiang says:

    For some reason, although running `describe-face` tells me that the font should be inheriting from ‘fixed-pitch (or just ‘default in my case), the displayed font is still ‘variable-pitch…

  4. Unfortunately, this doesn’t work for code blocks anymore, as `org-block-background` has been removed from org 😦

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s