Tags: web meta vim
The Board of Directors are pleased to announce that the blog's LaTeX rendering is now done at build time.
Previously, readers had to download MathJax from a CDN somewhere. Well, their browsers did. Then the MathJax script converted all LaTeX on the page to HTML, and downloaded CSS files and fonts to make the HTML look pretty.
I came across a Python Markdown plugin the other day called markdown-katex that resolved this gag-worthy state of affairs.
Here's the build process and how markdown-katex now fits into it:
Now for some vim trivia. I had to go back and change the LaTeX delimiters in all the blog posts. MathJax uses $ as a delimiter for inline math mode, and $$ for display math mode. markdown-katex uses $` and `$ for inline math mode, and...
...for display math mode. I used a vim macro to replace the inline delimiters, with the following key presses:
qa/\$RETa`ESCni`ESCwq. It finds the next pair of LaTeX delimiters (e.g.
$x^2+1$) and converts them to the new format (
Here's a breakdown of how that works.
qastarts recording a macro in register
/\$RETsearches for the next dollar sign and moves the cursor there. RET is the enter/return key.
a`ESCenters insert mode after the cursor, writes a backtick, and exits insert mode. ESC is the escape key.
ncontinues the search, moving the cursor to the next dollar sign.
i`ESCenters insert mode before the cursor, writes a backtick, and exits insert mode.
wmoves the cursor forward by 1 word so that the macro doesn't get stuck on the $ we just processed.
qstops recording the macro.
I could type
@a to run the macro once, then
. to repeat it after that. It's much faster and less error-prone than fixing such things manually, obviously, but I also feel that vim is more suited to this task than, say, a Python script. It's so easy to record a macro, and you can catch errors as they happen. I would go with Python if I had to change more than a handful of files, though.
I'd be happy to hear from you at email@example.com.