LaTeX considered as unused media

Now and then it happens to me that some LaTeX medias are not sent synced, so when I access the note on a mobile, it can’t show me the formatted text. I looked on the documentation, and on these cases it says that I have to use the Check Media tool. This one, however, tells me that every latex-*.png file is unused. It might be right that there are that are not useful anymore, however when I click on “Delete Unused”, and check my media collection, it has been purged from latex images, even the one that were useful. I check that by watching my directory populate when I preview my cards.


  1. I think this is a bug, and if it is not, can someone explain me what is the correct way of compiling/updating all latex related medias that were not sent my mobile;
  2. I would be cool if there was a big button to instantly recompile all LaTeX medias (I couldn’t find any add-on for this purpose).

As mentioned in the documentation, you should not put latex tags in your card template.

Is that an on purpose feature (like, is it a deliberate choice, with a reason), or is it simply because it is easier to develop Anki that way? In the former possibility, I can’t really understand it, but either way I think it is wrong (maybe there is a good reason for such restriction, but I can’t think of). In my case, I don’t really want to write hundreds – if not thousands – of times [latex]...[/latex].

I’d suggest to simply remove this limitation (to seek for latex medias, simply check each card and not each field).

Btw, toying with the media collection made me notice that latex images have names that look like hashes, so I was wondering: if to find out a certain formula’s image Anki hashes it, if there is a collision, one might end up with a wrong image shown, right?

I created a simple add on to insert defaults when I create a new card. All my cards utilize LaTeX for multiple fields.


How exactly does it work? Can I choose what it automatically inserts? Can it convert my existing database (like, insert [latex]…[/latex] in every field that is surrounded by these tags in a model)? Could you add a screenshot to your post of what actually changes? Thank you a lot :grinning:

You have to customize the python code I linked to to insert what you want and to what fields. I’m not a programmer and cobbled this together from reading what others have posted. It doesn’t convert anything. Upon typing the keyboard shortcut that is specified in the code, it inserts whatever you need to whatever fields you specify.

You may find a more polished add on that does what you want from the big list of addons that are published.

Unless you have a good reason for using Latex, use Mathjax. You can put the encoding \( ... \) in the note template usually without problems, and as it is generated on the fly, no problems with deleted files…

With that being said, mark your maths, press Ctrl + M, M (for Mathjax) or Ctrl + T, T (Latex)… You can see the shortcuts by pressing this thingy:

I would strongly recommend getting into the habit of using the shortcuts instead of wrapping in the note template, mathjax or not…

Also, the reason why latex encodings on the note templates are not properly supported is for technical reasons and quite apparent: Anki would have to parse all templates, not just the fields for checking for media. It is the same reason why you shouldn’t do <img src="{{Field}}.png"> on your templates…

1 Like

I have a good reasons for not using Mathjax:

  1. I want LaTeX on mobile. IDK if there is such a thing as mathjax on android, but it is probably longer to setup than juste compile the whole collection on computer;
  2. I may have equations that are computation-heavy. You may know that LaTeX compilation may take up to some seconds when you are compiling long equations, and I don’t want to wait that long on each note;
  3. I have already setup LaTeX configuration (the real binary, not the Anki’s LaTeX config) for custom packages, etc. so I don’t want to do that again with Mathjax.

I think that this technical reason is not that valid. I mean, yes, Anki would have to parse all templates. And then, how is that a problem? Just put in a loop. I don’t really see why is it complicated. The number of templates is very small comparing to the number of notes, and even the number of cards is of the order of fields in the note (usually – one might think of degenerate cases in which the number of cards is not θ(nb of fields), but rather Ο(2^(max nb of fields per card)*nb of cards), but I think one safely might assume the number of fields per card is (a small) constant).

Finally, I have an other issue with your solution, and it is that one cannot have LaTeX code that does not depend on the field content, without generating it on-the-fly, and this is ridiculous: if there is one thing that would be efficient, it would be render all the static parts for a given note type once and for all! You might argue that there is almost never the need of such static content, but I actually have :stuck_out_tongue:. Think about when you create a specific template that adds a bit of context to some notes, so that you don’t have to fill in every field with that context (for example, consider a deck with primitive questions: your note types might have two fields, one for the function to primitive, and the other with the answer. Your template might have one card with \int {{Derivative}} dx as a question, and {{Primitive}} as answer, and an other card with \frac{d}{dx}{{Primitive}} as a question, and {{Derivative}} as the answer. How could I have the same result, with the same amount of work – almost none– without allowing latex on templates?).

PS: the last example is a bit of a fraude in the sense that the content is not exactly static, and could not be pre-compiled, but it still IMO points out a flaw. I am quite sure you could find a similar example with real-static content.

Finally, I might say that if some users really don’t like the idea of a per-card media checking, you might just add a checkbox in the options to turn on this feature (maybe a per deck option, or a per note type option, IDK).

EDIT: just thought of an example of static content: a \qed at the end of proofs.

  1. MathJax is compatible with AnkiDroid
  2. valid
  3. valid, especially as I personally had bad experiences (performance issues) with setting up custom packages for MathJax (luckily, I do not rely on them)

Looping through a field in a SQL database vs parsing HTML (possibly even JS) getting content from a SQL database is a big performance difference. I’m not deep into Anki programming though, so I can not say how much of a difference it really is. I really doubt that dae is going to change this, nor accept pull requests that change this. You can ask though.

One hint, although not as practical as you’d like, towards statical content on Anki templates: media with an _underscore is not deleted.

Perhaps an add-on could even change the default behaviour towards using _images for media? Would be impractical for deleting unused media, but yet a workaround - if you are willing to pay an add-on developer.

I was just giving a hint, I have no say in Anki development :slight_smile:

The recent template changes have meant rendering the templates wouldn’t be the huge performance hit it used to be, but it’s still more expensive, and I’m afraid a change here is not on the short term horizon.

You can generate missing LaTeX images from the check media screen.

I’m with you on using LaTeX. I take all my notes with LaTeX. Mostly using the outlines package to easily create outlines. Then in my text editor, I turn those outlines into questions. Most are math heavy, tables, custom command shortcuts and the like. I don’t want to have to convert those to the html like notes in Anki so I use the latex tags in my anki cards but I never put them as part of the template. I have 700+ cards and I use the SVG option so that the cards scale really nice image wise without taking up a lot of space. Couldn’t be happier working with Anki this way.