My Technical Notes

Friday, 23 December 2016

Syntax highlighting in Anki using LaTeX, minted and Pygments

Follow Syntax Highting in Anki the Easy Way instead.


This blog post will explain how to get syntax highlighting work in Anki. Note that this is a laborious process. You may need to restart your computer after some of the steps for the changes to take effect.

Anki uses LaTeX to render `[latex]` tag elements within card fields. In this "tutorial", I am using the LaTeX `minted` package for code snippets within card fields. `minted` uses a Python package called `Pygments` to render its code snippets. Below is a component diagram of how everything fits together:


Installing MikTeX

Install MikTeX from their official website. When installing, accept the option which will allow MikTeX to install missing packages on-the-fly.

Installing Minted LaTeX package

Install "minted" LaTeX package using `mpm` (MikTeX Package Manager). You will find a "MikTeX Package Manager (Admin)" shortcut in MikTeX's start menu folder.

If you get error messages when trying to install "minted", then reinstall MikTeX.

Installing Python

Install Python from Python's website. Allow it to change the `PATH` variable. This will allow you to call Python from the command line. Also, tick the option for it to install for all users so that the Python folders end up somewhere within `C:\Program Files (x86)\` as opposed to `C:\Users\<me>`. At the end of the install, allow it to disable the Windows Path Limit.

Installing Pygments

Pygments is a Python package that does the syntax highlighting. You can install it from the command line (elevated):

easy_install Pygments

If the installation hangs, then stop it and try it again. It should succeed second time around.

To clean up, delete any folder with a name starting with "easy_install" in `C:\Users\<me>\AppData\Local\Temp` (the `TEMP` environment variable).

Testing LaTeX and minted package

Note that you may need to restart your computer if your computer fails this test and try it again.

Save the following document under a ".tex" file extension:



\title{Test \LaTeX{} and minted}
\author{Tahir Hassan}


Sample abstract for testing purposes

Here is some text.

import numpy as np
def adder(a,b):
    return a + b

Minted works as expected.


Run the following statement on the command line (change "myfile.tex" to the real file name and path).

pdflatex myfile.tex --shell-escape

Ensure that the PDF contains syntax highlighting.

Installing "Edit LaTeX Build Process" Add-on

To install an extension, within Anki, click on "Tools" → Add-ons → "Browse & Install". Notice that there is a textbox for entering a "Code". Click on the "Browse" button to be taken to their website.

In this webpage, click on the extension you want (We want "Edit LaTeX Build Process"), and you will see a box showing the code. Paste this into the "Code" textbox and press "Ok".

Restart Anki.

Adding `--shell-escape` option

In Anki, go to "Tools" → "Add-ons" → "Edit_LaTeX_build_process" → "Edit...". Add "--shell-escape" argument before "-interaction=nonstopmode". The first few code lines should look like this:

newLaTeX = \
   ["latex", "--shell-escape", "-interaction=nonstopmode", "tmp.tex"],
   ["dvipng", "-D", "200", "-T", "tight", "tmp.dvi", "-o", "tmp.png"]

Restart Anki.

Changing Note Type LaTeX options

Within Anki, go into "Tools" → "Manage Note Types". It is possible that you may have to do this for all the major Note Types. For instance, while "Basic" is highlighted, click on "Options". In the LaTeX header, under the last `usepackage` statement, add another one:


Restart Anki.

Test Anki LaTeX syntax highlighting

Create a Basic card in any deck. With the following data:

Question Text:

Does this C++ code work?

using namespace std;
int main()
  return 0;

Answer Text:

This works?!

Run the question and see if the C++ code firstly shows, and if so, check if it is nicely syntax highlighted.

The first time it runs, it will take a long time to process the LaTeX.


No comments: