Copy formatted org-mode text from Emacs to other applications

| categories: rtf, emacs | tags:

I do a lot of writing in org-mode and I thought it would be great if I could copy text from an org-file and paste it with formatting into other applications, e.g. Word, Gmail, etc…. Curiosity got the better of me and I wondered how this is done in other applications. It works by creating a Rich Text Format version of what you want to copy and then putting that on the clipboard. It isn't quite enough to just copy it, it needs to go in the clipboard as an RTF datatype. On Mac OSX I used pbcopy to make that happen.

Check out this video of this post in action: https://www.youtube.com/watch?v=irkmQnggVpE

One simple strategy to do this from org-mode is to generate HTML by export, and then convert it to RTF with a utility, e.g. textutil. For example like this.

(defun formatted-copy ()
  "Export region to HTML, and copy it to the clipboard."
  (interactive)
  (save-window-excursion
    (let* ((buf (org-export-to-buffer 'html "*Formatted Copy*" nil nil t t))
           (html (with-current-buffer buf (buffer-string))))
      (with-current-buffer buf
        (shell-command-on-region
         (point-min)
         (point-max)
         "textutil -stdin -format html -convert rtf -stdout | pbcopy"))
      (kill-buffer buf))))

(global-set-key (kbd "H-w") 'formatted-copy)

This works well for everything but equations and images. Citations leave a bit to be desired, but improving this is still a challenge.

Let us try this on some text. Some bold, italic, underline, struck and verbatim text to copy. Here are some example Formulas: H2O ionizes to form H+. We simply must have an equation: \(e^{i\pi} + 1 = 0\) 1. We should also have a citation kitchin-2015-examp and multiple citations kitchin-2016-autom-data,kitchin-2015-data-surfac-scien 2.

A code block:

import pycse.orgmode as org
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 60, 500)
plt.figure(figsize=(4, 2))
plt.plot(np.exp(-0.1 * x) * np.cos(x),
         np.exp(-0.1 * x) * np.sin(x))
org.figure(plt.savefig('spiral.png'),
           caption='A spiral.',
           attributes=[['org', ':width 100']])
print('')
org.table([['H1', 'H2'], None, [1, 2], [2, 4]],
          caption='A simple table')
print('')
org.result(6 * 7)

Figure 1: A spiral.

Table 1: A simple table
H1 H2
1 2
2 4
42

In summary, this simple approach to generating RTF from exported HTML works really well for the simplest markups. To improve on getting figures in, getting cross-references, captions, proper references, etc… will require a more sophisticated export approach, and probably one that exports RTF directly. That is a big challenge for another day!

Bibliography

Footnotes:

1

There are probably some ways to get better images for equations. To get equation numbers and references to them will probably require a two pass build process.

2

This is another place where configuration will be required for bibliography style. Also, some checks to join neighboring footnotes.

Copyright (C) 2016 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.3.4

Discuss on Twitter