Sending emails with math and source code

Org mode is great for authoring rich documents with syntax highlighted source code, LaTeX math and images. It even supports evaluating live snippets of code embedded in the text. It does all of this whilst remaining a plain text format.

Imagine how useful it would be to author programming-related or technical emails using this functionality?

Imagine no more! org-mime, part of the org mode contrib, does this for a number of emacs-based mail clients. However, our preference is for mu4e, which is not part of that list.

Fortunately, it seems mu4e has orgmode support integrated. To get this working, configure the following in your ~/.emacs.d/init.el:

;; configure orgmode support in mu4e
(require 'org-mu4e)
;; when mail is sent, automatically convert org body to HTML
(setq org-mu4e-convert-to-html t)

When composing a new email, switch on the special mu4e / orgmode mode with M-x org~mu4e-mime-switch-headers-or-body (it will automatically switch between compose mode and orgmode depending on whether your cursor is on the headers or in the body).

You should add the following options to your org mode body. The first configures LaTeX (math) to be converted using imagemagick (instead of MathJax or dvipng), and the second removes the table of contents.

#+OPTIONS: tex:imagemagick
#+OPTIONS: toc:0

When you want to send the email, move to the headers, and then do C-c C-c. Because org-mu4e-convert-to-html is set to true, the orgmode body will automatically be converted to HTML before being sent.

Until my pull request is merged in, you will have to make the following change to function org~mu4e-mime-convert-to-html() in org-mu4e.el:

(insert (org~mu4e-mime-multipart
         body html (mapconcat 'identity html-images "\n")))

should be:

(insert (org~mu4e-mime-multipart
         raw-body html (mapconcat 'identity html-images "\n")))

In other words, body becomes raw-body, else your outgoing mails will have HTML in their text parts, instead of plain text.

In that same function, you might also want to change:

(org-export-preserve-breaks t)

to

(org-export-preserve-breaks nil)

So that the HTML email does not get hard linebreaks.

Following are examples of some of the elements you might include in such a rich text email. They should appear in your rich text capable email client as follows:

org-mu4e-example.png

Here are the originals:

A section with some math

We can easily include math using LaTeX:

\[ C(p,t) = \lbrace x_0(p,t), x_1(p,t), \cdots, x_{N-1}(p,t) \rbrace\tag{1} \]

Some syntax highlighted C++

Syntax highlighted source code, for all of the languages supported by emacs (a very long list):

int main() {
    // Recently discovered trick, local functions in C++!
    struct Kludge {
        static int doSomething(int x) {
            return x * 2;
        }
    };

    for (int i=0; i < 10; i++) {
        cout << Kludge::doSomething(i) << endl;
    }

    return 0;
}

4 thoughts on “Sending emails with math and source code”

  1. This is great! Can you advise how I might advise the “org~mu4e-mime-switch-headers-or-body” function so that:

    #+OPTIONS: tex:imagemagick
    #+OPTIONS: toc:0

    Is automatically inserted at the beginning of the message?

    1. In org-mu4e.el, you’ll see “(org-export-with-LaTeX-fragments ‘dvipng)” in the function org~mu4e-mime-convert-to-html — I don’t know if it’s possible that there’s still a hook after that to change it non-invasively.

      As an invasive hack, you could simply edit that function, or replace it with your own in your emacs startup.

      I don’t (yet) know how to configure the TOC option programmatically.

      1. Seems you can disable orgmode tocs with “(setq org-export-with-toc nil)” — so you could add this to the let clause in the convert-to-html function!

        I would consider disabling orgmode tocs altogether!

  2. I’ve developed the following function which gives a keybinding shortcut (M-@) to switch to Org/HTML mode when composing emails. It also inserts the #+OPTION lines if they are not present, here at the bottom of the email.

    (defun kdm/mu4e-org-compose ()
    “Switch to/from mu4e-compose-mode and org-mode”
    (interactive)
    ;;(if (not (boundp ‘kdm/mu4e-org-html-opt-done))
    (let ((p (point)))
    (goto-char (point-min))
    (let ((case-fold-search t))
    (when (not (search-forward “#+OPTIONS: tex:imagemagick” nil t))
    (goto-char (point-max))
    (insert “\n#+OPTIONS: tex:imagemagick\n#+OPTIONS: toc:0\n”)))
    (goto-char p))
    (if (eq ‘mu4e-compose-mode (buffer-local-value ‘major-mode (current-buffer)))
    (org~mu4e-mime-switch-headers-or-body)
    (mu4e-compose-mode)))
    (global-set-key “\M-@” ‘kdm/mu4e-org-compose)

    Note that there are some minor issues with HTML composition. See https://github.com/djcb/mu/issues/571 and https://github.com/djcb/mu/issues/572

Leave a Reply

Your email address will not be published. Required fields are marked *