Streamlining Anki with LaTeX

I recently started using Anki (open source spaced repetition flashcard tool) for reviewing, learning, and remembering technical material that I’ve learned over the years. Given the nature of optimization, machine learning, control theory, etc., it’s usually the case that I want to easily include expressions/equations in my flashcards. By default, any LaTeX code in an Anki flashcard needs to be wrapped in [latex] [/latex] tags, which is cumbersome and leads to stylistic inconsistencies. Additionally, in order to use your own custom commands and common packages, by default, they need to be added manually to an Anki note type header; that is, you cannot include them with an input{} command. This makes maintaining these commands cumbersome.

So, the solution is to
1. configure Anki to use LaTeX as its default typesetting system (the default Anki card editor is a light WYSIWYG editor), and
2. create and install a custom package that allows you to use and, importantly, maintain your custom commands with Anki.
Let’s go through how to setup both.

Requirements

This tutorial assumes that you have installed
Anki
– LaTeX distribution (e.g. for OSX, Windows).

LaTeX as Anki’s default typesetting system [1]

Create a LaTeX note type

1. Tools > Manage Note Types > Add
2. Select “Add: Basic”
3. Name: LaTeX

Modify the card format

Find “LaTeX” in the Note Types dialog list. Select “Cards…” button. Now surround the {{Front}} and {{Back}} tags in [latex] [/latex] tags, and add your preferred styling to the cards. (Sorry, you’ll have to remove the space in [ latex] in the instructions below because the same tag convert text to LaTeX in these posts too.)

Front Template:

[ latex]{{Front}}[/latex]


Styling:

.card {
font-family: arial;
font-size: 10px;
text-align: center;
color: black;
background-color: white;
}
img {
width: auto;
height: auto;
max-height:1000px;
}


Back Template:

{{FrontSide}}

<hr id=answer>

[ latex]{{Back}}[/latex]


Custom commands in LaTeX note type

The next thing you’ll want to do is modify your LaTeX note type’s Header and Footer with the commands you commonly use. To edit the Header/Footer, return to the Note Types dialog and select “Options…”. The default looks like this:

Header:

\documentclass[10pt]{article}
\usepackage{amssymb,amsmath,amsfonts,mathrsfs}
\usepackage[paperwidth=5in, paperheight=100in]{geometry}
\pagestyle{empty}
\setlength{\parindent}{0in}
\begin{document}


Footer:

\end{document}


Creating your own LaTeX package [2]

While adding your commonly used packages and commands is useful, I like to maintain a common set of commands/packages that I can simply include in my new documents (without needing to update the Header/Footer in Anki every time I add something new). However, by default, Anki does not allow for input or def in its preamble, so including your (self-updating) custom and usual commands is not so straightforward. Fortunately, you can include your commands in a custom LaTeX package and invoke that package instead. For example, a LaTeX package called giraffemath.sty is defined by including the line \ProvidesPackage{giraffemath}. Here is a barebones example:

\ProvidesPackage{giraffemath}
\newcommand{\R}{\ensuremath{\mathbb{R}}}
\newcommand{\C}{\ensuremath{\mathbb{C}}}
\newcommand{\Z}{\ensuremath{\mathbb{Z}}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\F}{\mathbb{F}}
\newcommand{\W}{\mathbb{W}}


Installing and including the package [3]

Now, let’s add your custom package(s) to your local latex packages directory. We’ll first create a directory to house all your custom packages, and then we’ll symlink it to your local distribution installation directory’s texmf-dist subdirectory. On my machine (OSX), my path is /usr/local/texlive/2013/texmf-dist.

mkdir ~/Dropbox/custom-tex-packages
cp giraffemath.sty ~/Dropbox/custom-tex-packages
ln -s ~/Dropbox/custom-tex-packages/ kpsewhich -var-value TEXMFHOME/tex/latex/custom-tex-packages


Refreshing the LaTeX tree [4]

Next, we will need to refresh the tex packages to pick up the changes you have made to the packages. NOTE: You will need to run this command each time you make a change to your custom packages.

sudo texhash
sudo mktexlsr


Adding your package to the Anki note type header

Last but not least, add your package to the LaTex preamble. Your new header may look like this:

Header:

\documentclass[10pt]{article}
\usepackage{amssymb,amsmath,amsfonts,mathrsfs}
\usepackage[paperwidth=5in, paperheight=100in]{geometry}
\pagestyle{empty}
\setlength{\parindent}{0in}

\usepackage{giraffemath}

\begin{document}


Adding a new note

That’s it! Now when you create a new Anki card, you can choose LaTeX as your note type. LaTeX away! (Wrap inline math in .) Here’s an example:

And the card looks like this:

5 thoughts on “Streamlining Anki with LaTeX”

1. I’m trying out Anki for studying math and had just decided all-LaTeX cards were going to be the way to go. This post has saved me a ton of time figuring out enough of how Anki works to make that happen. Thank you!

2. Dang says:

I have a question regarding this approach: were you able to get Cloze deletions to work?

3. Ed says:

Just a heads up. Anki does not officially support Latex coding in the note’s templates. The manual says as much.

This practice messes up the cards especially on AnkiWeb. I found out the hard way. I tried that a few months back and eventually every card spat out use Tools->Check while reviewing. It’s a nice way to wrap your content and save a few steps but…alas.

4. Juw says:

This was great help. Thank you so much

5. Anas says:

Very helpful!