* MS Word comments from org-mode
:PROPERTIES:
:categories: orgmode, docx
:date: 2023/02/25 11:49:07
:updated: 2023/02/25 15:48:27
:org-url: https://kitchingroup.cheme.cmu.edu/org/2023/02/25/MS-Word-comments-from-org-mode.org
:permalink: https://kitchingroup.cheme.cmu.edu/blog/2023/02/25/MS-Word-comments-from-org-mode/index.html
:END:
TL;DR:
#+BEGIN_EXPORT html
#+END_EXPORT
Today I [[https://www.reddit.com/r/orgmode/comments/11bkt2h/is_it_possible_to_pass_html_in_org_to_pandoc_word/][learned]] you can make a Word document from org-mode with Word comments in them. This could be useful when working with collaborators maybe. The gist is you use html for the comment, then export to markdown or html, then let pandoc convert those to docx. A comment in HTML looks like this:
#+BEGIN_EXAMPLE
Comment textThe text being commented on
#+END_EXAMPLE
Let's wrap that in a link for convenience. I use a full display so it is easy to see the comment. I only export the comment for markdown and html export, for everything else we just use the path. We somewhat abuse the link syntax here by using the path for the text to comment on, and the description for the comment.
#+BEGIN_SRC emacs-lisp :results silent
(org-link-set-parameters
"comment"
:export (lambda (path desc backend)
(if (member backend '(md html))
(format "%s%s"
(user-full-name)
desc
path)
;; ignore for other backends and just use path
path))
:display 'full
:face '(:foreground "orange"))
#+END_SRC
Now, we use it like this [[comment:This is the text commented on][This is the comment]].
In org-mode it looks like:
#+attr_org: :width 800
[[./screenshots/date-25-02-2023-time-11-44-11.png]]
To get the Word doc, we need some code that first exports to Markdown, and then calls pandoc to convert that to docx. Here is my solution to that. Usually you would put this in a subsection tagged with :noexport: but I show it here to see it. Running this block generates the docx file and opens it. Here I also leverage org-ref to get some citations and cross-references.
#+BEGIN_SRC emacs-lisp
(require 'org-ref-refproc)
(let* ((org-export-before-parsing-hook '(org-ref-cite-natmove ;; do this first
org-ref-csl-preprocess-buffer
org-ref-refproc))
(md (org-md-export-to-markdown))
(docx (concat (file-name-sans-extension md) ".docx")))
(shell-command (format "pandoc -s %s -o %s" md docx))
(org-open-file docx '(16)))
#+END_SRC
#+RESULTS:
The result looks like this in MS Word:
#+attr_org: :width 800
#+name: fig-2
#+caption: How a comment looks in Word.
[[./screenshots/date-25-02-2023-time-11-41-37.png]]
That is pretty remarkable. There are some limitations in Markdown, e.g. I find the tables don't look good, not all equations are converted, some cross-references are off. Next we add some more org-features and try the export with HTML.
** export features for test
Test cross-references, references, equations, etc...
Aliquam erat volutpat (Fig. ref:fig-2). Nunc eleifend leo vitae magna. In id erat non orci commodo lobortis. Proin neque massa, cursus ut, gravida ut, lobortis eget, lacus. Sed diam. Praesent fermentum tempor tellus. Nullam tempus [[cite:&yang-2022-evaluat-degree]]. Mauris ac felis vel velit tristique imperdiet. Donec at pede. Etiam vel neque nec dui dignissim bibendum. Vivamus id enim. Phasellus neque orci, porta a, aliquet quis in Table ref:tab-1, semper a, massa. Phasellus purus (eqref:eq-1). Pellentesque tristique imperdiet tortor. Nam euismod tellus id erat [[cite:&kolluru-2022-open-chall]].
#+name: tab-1
#+caption: A table.
| x | y |
| 1 | 3 |
| 3 | 6 |
We have equations:
#+name: eq-1
\begin{equation}
y = mx + b
\end{equation}
- bullet1
- nested bullet
- bullet2
some defintions:
- emacs :: greatest editor
1. item 1
2. item 2
One equation: \(e^{i\pi} - 1 = 0\)
A second equation:
\begin{equation}
e^{i\pi} - 1 = 0
\end{equation}
** References
bibliography:~/Dropbox/emacs/bibliography/references.bib
** Alternate build with HTML.
Here we consider [[comment:alternate build approaches][For example, html]].
#+csl-style: apa-5th-edition.csl
#+title: MS Word comments with org-mode
Run this to get the docx file. I find this superior; it has references, cross-references, equations, tables, figures, etc. Even a title.
#+BEGIN_SRC emacs-lisp
(let* ((org-export-before-parsing-hook '(org-ref-csl-preprocess-buffer
org-ref-refproc))
(org-html-with-latex 'dvipng)
(f (org-html-export-to-html))
(docx (concat (file-name-sans-extension f) ".docx")))
(shell-command (format "pandoc -s %s -o %s" f docx))
(org-open-file docx '(16)))
#+END_SRC
#+RESULTS: