3

I've been trying to create mathematics flashcards in Anki. Naturally, I wanted LaTeX to be the default typesetting system for those cards. In order to do so, I created a new basic note type named basic math. Here's my options header:

\documentclass[10pt]{article}
\usepackage[paperwidth=5in, paperheight=100in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath,amsfonts,mathrsfs,color}
\pagestyle{empty}
\setlength{\parindent}{0in}
\newcommand{\C}{\mathbb{C}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Z}{\mathbb{Z}}
\renewcommand{\Re}{\operatorname{Re}}
\renewcommand{\Im}{\operatorname{Im}}
\begin{document}

My card type front (in French) is:

[latex]{{Recto}}[/latex]

My card type back (in French) is:

{{FrontSide}}

<hr id=answer>
<br />

[latex]{{Verso}}[/latex]

Finally, here's my card type style:

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

Here's a card front (in French):

Vrai ou faux ?
\[
z-\overline{z}=2\Im z
\]

and its back:

Faux. En général,
\[
z-\overline{z}=2{\color{red}i}\Im z
\]

Alas, here's the rendering:

enter image description here

Clearly, something's weird with the alignment of the display math. If I change the back of the card for this:

Faux. En général, bla bla bla bla bla bla bla bla bla bla bla
\[
z-\overline{z}=2{\color{red}i}\Im z
\] 

then the equation is properly centered (as it should normally be by default with LaTeX):

enter image description here

So, my question is: How to get the proper center alignment, as the Anki math environment [$$][/$$] would give?

Guest
  • 67

1 Answers1

4

First off, you Can't Do™ what you're trying to do with [latex]{{Recto}}[/latex]. I refer you to the manual:

Media references to fields are not allowed. They may or may not display during review, and will not work when checking for unused media, importing/exporting, and so on. Examples that won’t work:

[latex]{{Field 1}}[/latex]

Besides that, though, what you're seeing is a result of the \documentclass{article}, combined with how Anki invokes dvipng to crop the resulting image. (Also, why the heck do you have it set to 100in? )

Let's take a look what's happening behind the scenes, when Anki generates an image from LaTeX:

  1. latex (not pdflatex! the old-school one, unless you're manually changing anki.latex.latexCmds with an add-on.) is invoked, to generate a DVI file from the concatenated LaTeX input:

     latex -interaction=nonstopmode tmp.tex
    
  2. dvipng is invoked on the result, to convert it to a PNG that can be included into your Anki deck, and in the process, crops out most of the whitespace:

     dvipng -D 400 -T tight tmp.dvi -o tmp.png
    

Okay, let's see this in action on your “working” example, minus the ultra-tall 100in instruction (click the image to see it full-size):

enter image description here

Bottom-left is your concatenated LaTeX code; top-right is the document generated by LaTeX; and bottom-right is the final, cropped image.

So far, so good. Let's try your original code:

enter image description here

Ahhah! Do you see what's happening, there?

Your equation is centered, from LaTeX's perspective — that is, the perspective of a 5in-wide {article}, which is what you have herein told it to generate; it's the cropping that's making it look like it's ofw-center. (Or, from another perspective, we could say that the equation is centered, and it's the text that's being payed-out poorly — it's offset far to the left, in your document.)


Okay, enough of the “why.” Unfortunately, as far as I know, this is not an easy problem to fix, and still get out what Anki expects.

One approach, though, that will work, with your specific example, is to switch from a standard \documentclass{article}, to \documentclass{standalone} — a package with a feature specifically designed to do what we want to do, here (adjust the layout — stuff like your centering, for instance — within LaTeX, to typeset things to their smallest size.)

Specifically, let's try the crop and varwidth options. Change your LaTeX prelude in Anki to the following, adjusted as necessary¹ to your needs: (notice that I've gotten rid of Anki's default \special{papersize=3in,5in} line, as it's no longer relevant)

\documentclass[crop=true,varwidth=true]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\pagestyle{empty}
\setlength{\parindent}{0in}
\begin{document}

enter image description here

Tada!

Now, let's talk about caveats, before I ride off into the SRS sunset:

  1. This is going to cause some compilation errors. Don't ask me why — I think the standalone package doesn't like what we're doing with it (it's built to generate its own images? Although I'm not sure of the specifics.) This shouldn't screw up Anki's image generation, tho they might show up in the preview window for you, I'm not sure? YMMV.

  2. varwidth environments have … many caveats. A bunch of things don't work in them. I don't know all of the details — you'll have to go trawling through TeX.se to find answers from people more knowledgable than I, most likely, when using it.

Because of those, I want to examine one last alternative:

Don't typeset your entire card in LaTeX in the first place!

It's lucky we're on SuperUser, and not TeX.se — I'm pretty sure it's Highest Blasphemy over there, but quite honestly, Computer Modern is Effing Ugly on screens anyway (it's designed for print, and it's great there … but not so much on screens, which is where we use Anki); and more relevantly, Anki is likely to move to MathJax in the future (i.e. more consistent in-card rendering, as opposed to shipping around hacky pre-compiled PNGs like this.) It's more future-proof to rely on Anki's built-in web engine (HTML, CSS, JavaScript) for styling and typesetting, than to ship that all out to LaTeX! Rendering equations to PNGs using LaTeX is effectively, at this point, a hold-over measure.

Basically, this can all be avoided, if you just typeset your card in HTML. Instead of:

[latex]
Faux. En général,
\[
z-\overline{z}=2{\color{red}i}\Im z
\]
[/latex]

… try:

Faux. En général,
[$$]z-\overline{z}=2{\color{red}i}\Im z[/$$]

enter image description here

Boom; the text is centered over the equation. What a masterpiece! (My settings are slightly different than yours; yours won't be so outsized-looking, etc.)


  1. I have no idea how to make the {geometry} package you use work with {standalone}. I'm not actually an expert, I just happened to grapple with this specific problem a while back — getting it to work with your specific needs, will probably need some wrangling with LaTeX itself, as opposed to Anki. Good luck.
ELLIOTTCABLE
  • 2,708