Skip to content

Translate math notes written with the amsthm package to Anki notecards!

License

Notifications You must be signed in to change notification settings

langston-barrett/amsthm-to-anki

Repository files navigation

amsthm-to-anki

Build Status

This is a simple program to transform LaTeX math notes (written with the amsthm package) into Anki notecards. It converts things like definitions, theorems, lemmas, corrolaries, and equations into the input format for the LaTeX Note Importer.

Table of Contents

What Does it Do?

  • Definitions, theorems, and lemmas with names are treated basically the same:
  • Definitions of the following (amsthm) form:
\begin{definition}[Name of Thing Being Defined]
  Definition of term.
\end{definition}
  • Theorems with names:
\begin{theorem}[Name of Theorem]
  An important theorem.
\end{theorem}
  • Lemmas with names:
\begin{lemma*}[Name of Lemma]
  An important lemma.
\end{lemma*}

where "What is the term/theorem/lemma 'Name of Term/Theorem/Lemma'?" will be on the front and the definition/theorem/lemma will be on the back.

  • Theorems, lemmas, and corrolaries of an if-then form with premises and a conclusion:
\begin{theorem}[Name of Theorem]
  If
  \begin{premises}
    \item $x\in\C$
  \end{premises}
  then
  \begin{conclusion}
    there exists $y\in\C$ such that $y^2=x$.
  \end{conclusion}
\end{theorem}

where the name/premises will be on the front side and the conclusion on the back.

  • Equations of a specific form:
\begin{eqenv}[Name of Equation]
  Setup ("Let x be a real number", etc.)
  \begin{equation*}
    x = y
  \end{equation*}
\begin{eqenv}

where the setup and first half of the equation will be on the front side and the second half will be on the back side. Note that the "halves" are split on the string " = ".

  • And things that are already notes will be kept:
\begin{note}
  \begin{field}
    Side 1
  \end{field}
  \begin{field}
    Side 2
  \end{field}
\end{note}

Usage

You can download binaries from the tags page. Then if input.tex is a LaTeX2e file with the above sorts of environments in it, you can just run

./amsthm-to-anki input.tex output.tex

and import output.tex with the LaTeX Note Importer.

Setting up Anki

  1. Make sure pdflatex is on your PATH (hint: it likely already is).
  2. Install the LaTeX Note Importer. See the Anki documentation for details on installing plugins.
  3. Ensure your LaTeX preamble is set how you like it: go to the "Add" screen. Click on the "Basic" button. Click "Manage", then select the first "Basic" card type from the list, and click "Options". Adjust your LaTeX preamble so that your notes will compile properly.

Setting up amsthm in your input document

This is the setup I have in my document preamble:

\usepackage{amsthm}
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}
\newtheorem*{theorem*}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem*{lemma*}{Lemma}
\newtheorem{corrolary}{Corrolary}
\newtheorem*{corrolary*}{Corrolary}
\theoremstyle{definition} % these are the non-italicized
\newtheorem{eqenv}{Equation}
\newtheorem*{eqenv*}{Equation}
\newtheorem{remark}{Remark}
\newtheorem*{remark*}{Remark}
\newtheorem{example}{Example}
\newtheorem*{example*}{Example}
\newtheorem{definition}{Definition}
\newtheorem*{definition*}{Definition}

Note that the evironment names must match exactly with what I've defined here (although the displayed text -- the second argument -- can be arbitrary). I also define the following environments:

\newenvironment{note}{\paragraph{Note:}}{}
\newenvironment{field}{}{}
\newenvironment{premises}{\begin{enumerate}[label=\alph*.]}{\end{enumerate}}
\newenvironment{conclusion}{}{}

Code Outline

This section is mainly for my own benefit, and was used to outline the code before I wrote it. Still, might be useful for those who come after!

As per the Stack default, app/ contains the executable code, src/ contains the reusable code, and test/ contains the tests for the code in src/, made with the Tasty test framework, HUnit, QuickCheck, and Golden Test.

For each of the outlined transformations above, there is one file implementing that extraction and one testing it:

  • src/Extract.hs and test/Extract.hs
  • src/Extract/DefinitionsTheoremsLemmas.hs and test/DefinitionsTheoremsLemmas.hs
  • src/Extract/Premises.hs and test/Premises.hs
  • src/Extract/Equations.hs and test/Equations.hs
  • src/Extract/Notes.hs and test/Notes.hs

The src/Extract/Util.hs and src/Types.hs modules are imported by the submodules of Extract. The Main module only imports the toplevel Extract module, which provides a single entrypoint function. This maximizes the amount of testable program logic.

Libraries

I've used this project to experiment with the extensible-effects library, which provides an alternative to monad transformers. Its full power wasn't necessary, I currently only use it to provide a Writer monad in case I need debugging functionality.

Other newer or unusual libraries include:

  • Classy Prelude
  • Tasty test framework: A composable way to integrate HUnit and QuickCheck.
  • Data.Typeable and Data.Data: I use these for testing. I wanted a concise way to write an HUnit assertion that the return value of a function uses a certain constructor, e.g. is a Right. Additionally, I didn't want to collapse the information to a Bool and only see True /= False when my tests failed. Check out Test.Util for more details. The ideas were adapted from this StackOverflow answer

Internal API

Each module provides one method with the type

moduleName  LaTeX  ([Error], [Notecard])

TODO

About

Translate math notes written with the amsthm package to Anki notecards!

Resources

License

Stars

Watchers

Forks

Packages

No packages published