using org-mode export feature to publish to wordpress with math equations

If you are using the latest official version of Emacs and the latest version of org-mode, which is not the same thing as the version of org-mode that is shipped with Emacs, then here is some way to publish a blog post to wordpress with math equations in it, using org-mode.

1. some preliminary

In an org-mode buffer, one uses dollar signs (the same way as in LaTeX documents) to surround mathematical expressions, but without extra spaces. That means:

$a^2 + b^2$ is good
but $ a^2 + b^2 $ is not good.

When you run the command org-export-dispatch on an org-mode buffer, you can choose to export the contents to HTML. When you do that, something like

$a^2 + b^2$

is exported to something like

\(a^2 + b^2\)

When you paste the exported HTML text into the textbox of the WordPress post editor and press Preview, you will see that the generated preview does not have math equation images.

2. search and replace

The WordPress manual mentions that writing something like

$.latex a^2 + b^2$

(without the dot) is the way to have WordPress generate equations images.

So you just have to search for all occurrences of something like

\(a^2 + b^2\)

in the exported HTML text, and then replace them appropriately before pasting the contents to the WordPress post editor.

Emacs certainly has “search and replace” feature and you can even save such a task as a keyboard macro, but let me give you a command for the task:

(defun my-parens-to-wordpress-math ()
  "Replace \\(...\\) with ... in current buffer."
  (interactive)
  (save-excursion
    (save-match-data
      (goto-char (point-min))
      (let ((case-fold-search nil)
            (re (rx "\\("
                    (group (+? not-newline))
                    "\\)")))
        (while (re-search-forward re nil t)
          (replace-match (concat "$" "latex \\1$") t))))))

Wrote that command to publish my first math-heavy article on this blog: article on information entropy

3. some idea on how to write a math blog post using org-mode, AUCTeX, latex-preview

My workflow for now is like this:

  1. Write an outline (looking like a TOC) for a blog post in an org-mode buffer
  2. Instead of continuing in an org-mode buffer, I switch to an AUCTeX mode buffer, and write one section there (with the visual help of preview-latex), and then paste it back into a section in the original org-mode buffer.
  3. Go on with the next section.
  4. and so on

AUCTeX does have an outline feature and org-mode does have LaTeX preview feature, but I am more at home with the outline feature of org-mode and the preview feature of AUCTeX for now.

This entry was posted in Emacs, Mathematics and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s