# 2015-10-10 A trouble with Org-mode HTML export

I’m not sure whether this is going to be useful to anybody but me, but here’s a problem that cost me some time, and in case I run into a similar issue in two years’ time, let me write it down so that it’s not forgotten.

The problem turned up while working on my Org-edu-HTML Emacs library. One thing the library should do is exporting an Org document to a bunch of HTML pages. To that end, I employed my earlier org-one-to-many library, which splits an Org file into pieces.

One problem I ran into was that Org-edu-HTML should export each org file separately. Contrary to what one might think, the find-file-no-select function should not be used in Elisp code. Instead, I was advised to use with-temp-buffer and insert-file-contents.

So, what i did was (more or less) this:

(with-temp-buffer
(insert-file-contents infile)
(org-mode)
(org-export-to-file 'html outfile))


Unfortunately, it didn’t work. Well, it was even worse: it sort-of-kind-of worked, but not quite. When the fle contained mathematical formulae, and I asked Org to use dvipng and not MathJax, the png files did not appear.

Strange, isn’t it?

The solution was not really obvious for me. It turns out that the function org-html-format-latex, which is responsible for converting LaTeX fragments and environments into something acceptable in HTML (which is MathJax under normal circumstances), contains this code:

(or (buffer-file-name)
(make-temp-name
(expand-file-name "latex" temporary-file-directory)))


Now, when I used the with-temp-buffer construct, the function buffer-file-name returned nil (which is perfectly reasonable). In such a case, make-temp-name took over, and since the default value of temporary-file-directory is /tmp/, all my png files went straight there. Not what I wanted.

The remedy turned out to be quite simple. I needed to put (set-visited-file-name <something> t) into my code, for a suitable value of something.

This was not enough, as it turned out. Since with-temp-buffer kills the buffer it creates, and set-visited-file-name sets the “modified” flag for the buffer, I needed also to say (restore-buffer-modified-p nil) afterwards. (Consult the Elisp reference for the difference between restore-buffer-modified-p and set-buffer-modified-p.) Without that, the user is asked every time with-temp-buffer tries to clean up after itself.

So, that’s it for today. Not something fascinating, but probably useful (though perhaps for 0.001% of Emacs users…)