Anki setup for code and math cards

After stumbling on a very detailed Twitter thread (now expanded into an even more extensive blog post) by Michael Nielsen some months ago, I got inspired to try Anki as a tool to make learning new things more efficient.

This post gathers in the same place ressources (SO questions, blog posts...) that I used to get to my current setup. I will probably need it at some future time, and hopefully it can help others too.

Apps and account

I use the Anki desktop apps on Ubuntu and Mac mainly for creating cards, and the Android app for reviewing on the go. I use a free AnkiWeb account to synchronize decks between the three apps; I don’t use the web interface for reviewing.

The interplay between the different apps is quite smooth as long as you make sure that the synchronization is well run after each review session, and more importantly after each modification of your decks. Sometimes the sync will not work, for instance because of a network outage on your smartphone. In that case you will be prompted in to choose which version to keep (the server one or the local one); just stop and think about which one is the most recent and you should be good.

Plugins and LaTeX

I installed the Power Format Pack plugin, which allows for more advanced formatting options, especially for source code and LaTeX.

Setting up LaTeX is not a very straightforward experience, so you can always use a website to generate images of your expressions (I used this one for a few days before mustering the courage to dive into a session of googling and Stack Overflow poring...).

Anyway, here’s a list of instructions (assuming Ubuntu).

LaTeX installation

In a terminal, run sudo apt-get install texlive-base dvipng.

Those are rather heavy packages, and they are needed because Anki compiles your LaTeX code using these libraries, and insert the generated images into the cards; reference for this step here.

Improving Anki formatting

With the default options, the generated images are a bit too big and are not vertically aligned with the text, which annoyed me.

To solve this, you first have to change the LaTeX header in the template of the cards: go to "Tools > Manage Note Types", and for each type of decks, click on it, then click on "Options" and in the top text field ("Header"), add a line after the last one (\begin{document}) to set the font size.

I found that \footnotesize worked best for me. You might want to experiment though, so here’s a list of LaTeX font sizes.

In the first line (\documentclass[12pt]{article}), I also changed the size from 12pt to 8pt. I think I could have arrived at the same visual result without modifying the 12pt by choosing an even smaller font size category like \tiny, but it’s working that way.

You can then close this window, and click on "Cards…"; then in the "Styling" textbox, append these lines to correct the vertical alignment problem:

img[src*="latex"] {
  vertical-align: middle;
}

You now need to repeat this of every type of deck that needs it (references for these instructions: here and there).

Regenerating formulas images

If you didn’t have any cards created with LaTeX, you’re good to go. You’ll notice that the sync is now uploading medias along your cards: those are the generated images for your LaTeX formulas.

If instead you already had some cards created, Anki has to regenerate the images. You also need to do this if you want to experiment with the formatting options, to examine the result after each change.

The most simple way is to remove all the images named with the pattern latex-*.png in the collection.media directory, which for me is in ~/.local/share/Anki2/User 1/ on Ubuntu (you can always search the directory name on a different system to locate it).

At last in Anki, execute "Tools > Check Media"; Anki will then recompile all your LaTeX formulas and regenerate the images. You can now sync. Reference for this part here.

Conclusion

I have barely scratched the surface of what is possible with Anki (more complicated cards with cloze, fiddling with the reviewing settings...), but I really like the flexibility it provides for creating cards.

I have a feeling that I will need to come back to this page in some time when I change my phone or laptop, and that future-me will thank me...

By @Clément Chastagnol in
Tags : #Anki,