Copy formatted org-mode text from Emacs to other applications
Posted June 16, 2016 at 11:46 AM | 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.
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
- [kitchin-2015-examp] Kitchin, Examples of Effective Data Sharing in Scientific Publishing, ACS Catalysis, 5(6), 3894-3899 (2015). https://doi.org/10.1021/acscatal.5b00538
- [kitchin-2016-autom-data] "Kitchin, Van Gulick & Zilinski, Automating Data Sharing Through Authoring Tools, "International Journal on Digital Libraries", , 1-6 (2016). https://doi.org/10.1007/s00799-016-0173-7
- [kitchin-2015-data-surfac-scien] "John Kitchin", Data Sharing in Surface Science, "Surface Science ", N/A, in press (2015). https://doi.org/10.1016/j.susc.2015.05.007
Footnotes:
Copyright (C) 2016 by John Kitchin. See the License for information about copying.
Org-mode version = 8.3.4