box.matto.nl
Enjoying Open Source Software

Create docx documents from org mode

Plain text is the best format

In my spare time I write articles and other content. Depending on the publisher, this has to be either in odt or in docx format.

Of course, I like to write those texts in a decent editor and write in a plain text format.

For several years I have used an awk script that converted text from a markdownish format to fodt, see Advantages of FODT format in LibreOffice.

This created sometimes some issues with special characters like '<' and '>'. For this reason I abandoned this method and started to use Pandoc for the conversion. This way I could still write in plain text (in Markdown format) with all the advantages of using plain text, like the option to use a decent editor, and the use of version control tools.

Another very important element of writing in plain text is that the writer can fully focus on the content and doesn't have to worry about bullet-size, paragraph formatting, indention et cetera. The formatting is done afterward, by tools like Groff, LaTeX, Markdown.pl and so on.

Emacs org mode

A couple of months ago I decided to learn to use Emacs after using Vi and Vim for more than two decades. Org-mode is one of the lures of Emacs, and it comes with some powerful export functionalities.

Out of the box, org mode can export to the odt format and creates a very rich document.

Unfortunately, the default formatting does not fit the requirements of the publishers I work with. It has to be trimmed down a bit.

Changing some export options

At the first lines of any org file we can enter some meta-data options. It turns out we can use this to make the document looks more like what the publisher expects.

Disable title and author lines

With the following line we disable the creation of the title line and the author line at the top of the odt- or docx document:

#+OPTIONS: title:nil author:nil

Of course, if you want to have the title and the name of the author at the top of your document, you can add something like:

#+OPTIONS: title: To be and not to be
#+OPTIONS: author: Cat Schroedinger

Disable table of contents (toc)

With the following line we disable the creation of the TOC (table of contents) in the odt- or docx document:

#+OPTIONS: toc:nil

Prevent superscript and subscript

In my texts I sometimes have to use underscores ( _ ), which in a normal org-mode export results in a mix of superscript and subscript. To prevent this and have the underscores appear just like in the org file, at the following line:

#+OPTIONS: ^:nil

Prevent numbering of headings

Normally, the headings of your org file will result in numbered headings in your export.

This can be turned of with the following option:

#+OPTIONS: num:nil

Wrap up

Combined, this gives the following lines of meta-data at the top of the org file:

#+OPTIONS: title:nil author:nil
#+OPTIONS: toc:nil
#+OPTIONS: ^:nil
#+OPTIONS: num:nil

It not necessary to have this split out over several lines, but it helps the readability and adaptability.

Block of mono-spaced font

It is easy to create a block of text that will be shown as mono-spaced in the resulting export.

  • Mark a region
  • Hit Ctrl-c Ctrl-, (that is: control comma)

Emacs will show a list of options, choose s for src.

This will put markers at the edges of the region:

#+begin_src
...
#+end_src

Org mode export to odt

Out of the box, org mode exports to the odt file format.

For this, use the key binding "Ctrl-c Ctrl-e o o", or, in Emacs notation: "C-c C-e o o".

Org mode export to docx

Without additional configuration, org mode exports to the odt file format.

This is great, because odt is a real open format, unlike docx (Microsoft had to bribe several groups of people to get the docx format faulty recognized as "open").

Unfortunately, for many people the odt format is confusing and for those, we have to convert the odt format into docx.

Emacs will gladly take care of this conversion, when we add the following line to the Emacs configuration (like e.g., in ~/.emacs, or ~/.emacs.d/init.el):

(setq org-odt-preferred-output-format "docx")

For this to work, you have to have LibreOffice installed (or perhaps OpenOffice, I haven't tried that).

Advantages of using org mode to create an odt or docx document

There are several advantages to use org mode to convert a plain text document into a odt file or a docx file.

  • No need to install Pandoc, just Emacs is sufficient.
  • Export right from within Emacs (with: "Ctrl-c Ctrl-e o o").
  • Good integration of the RCS workflow in Emacs (just loop through the RCS cycle with: "Ctrl-x v v").
  • Org mode is a excellent tool for outlining
  • Fits nicely in a general "everything in org mode" workflow.

Give it a try!

NB: If you don't use RCS, you can of course use any other version control system.

Tags:

⇽ How I make my Shakshuka One week with FreeBSD 13 on an Acer Aspire One ZG5 ⇾