Philip Kaludercic<p>Neat and related: When writing HTML with some math, I don't really want to write MathML by hand (let alone depend on MathJax or something like that). LaTeXML turns out to be helpful again, specifically via the <a href="https://metacpan.org/dist/LaTeXML/view/bin/latexmlmath" rel="nofollow noopener noreferrer" target="_blank">latexmlmath</a> (which btw. can be used above as well, and <em>is</em> faster). Either way, I have quickly hacked together a function that uses <code>latexmlmath</code> to convert TeX-math in a comment into MathML:</p><pre><code>(defun mathml-from-tex ()
"Convert TeX in a comment to MathML in a buffer.
The TeX has to be written in a single-line comment prefixed with a
\"MathML:\" string. For example:
<!-- MathML: \frac{-b \\pm \\sqrt{b^2 -4ac}}{2a} -->
Everything until the end of the comment (or line) is passed to
\"latexmlmath\" (part of the LaTeXML suite) and injected into the next
line of the buffer. A comment indicating the end of the math block is
also added, so that multiple invocations of the command can replace the
old output."
(interactive)
(save-excursion
(goto-char (line-beginning-position))
(unless (search-forward-regexp
(rx (literal comment-start) (* space)
"MathML: " (group (* nonl))
(literal comment-end))
(line-end-position) t)
(user-error "No instruction found"))
(if (eobp)
(insert "\n")
(forward-line 1))
(let ((end "EndOfMathML") ;TODO: use </math>
(math (match-string 1))
(start (point)))
(when (search-forward-regexp (rx (literal end)) nil t)
(delete-region start (line-end-position)))
(save-excursion
(insert end)
(save-excursion
(insert (if (eolp) "" "\n")))
(comment-line 1))
(call-process-region math nil "latexmlmath" nil t t "-"))))
</code></pre><p>I also attach a quick demo, see below.</p><p><a href="https://commenting.onthe.incoherenceofthe.net/tags/emacs" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>emacs</span></a> <a href="https://commenting.onthe.incoherenceofthe.net/tags/latex" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>latex</span></a> <a href="https://commenting.onthe.incoherenceofthe.net/tags/mathml" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>mathml</span></a></p>