Background
I am starting to implement a few anki cards that include math equations. I used the built in mathjax for that but it sometimes takes 5 – 10 seconds to render the mathjax correctly; time, that could be invested into studying more material. It seems to be intermittent though, I cannot reproduce it reliably.
I noticed that I do not have any issues with loading pictures in Anki, even if I use multiple big pictures on the same card. So, instead, I wanted to add .svg pictures of my math equations. Since they’ll load much faster than mathjax renders for me, that would be a great step forward.
I also do not mind having a little bit of disk space used by those equations and do not sync with ankiweb either, so disk space isn’t an issue.
Things I tried
- I first thought that Ankis latex support could help me out here. It generates .png files though, which aren’t ideal. They have a very low resolution and don’t scale well (aside from the issue with switching themes between light and dark).
- Then I thought that #1 should be easily solveable. All I had to do is install
livetex
anddvisvgm
on my system and I’m good to go. This is true, but has the unintended side effect, that the rendered equations now greatly vary in size. I do not want to set custom css logic for every .svg though – I expected this to work without much hassle from the card templates.
My tex file for #2
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\pagestyle{empty}
\setlength{\parindent}{0in}
\begin{document}
% use \mathsf{} to use a nicer font for all math equations
\begin{math}
\mathsf{ \infty }
\end{math}
\end{document}
This has been compiled with
latex Maths.tex
dvisvgm --no-fonts --exact-bbox Maths.dvi
Generated Latex_Unendlich.svg
<?xml version='1.0' encoding='UTF-8'?>
<!-- This file was generated by dvisvgm 2.11.1 -->
<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='11.285679pt' height='5.415691pt' viewBox='38.854296 60.46924 11.285679 5.415691'>
<defs>
<path id='g0-49' d='M6.073225-3.239851C5.427646-4.052802 5.284184-4.23213 4.913574-4.531009C4.244085-5.068991 3.574595-5.284184 2.964882-5.284184C1.566127-5.284184 .657534-3.969116 .657534-2.570361C.657534-1.195517 1.542217 .131507 2.917061 .131507S5.284184-.956413 5.869988-1.912827C6.515567-1.099875 6.659029-.920548 7.029639-.621669C7.699128-.083686 8.368618 .131507 8.978331 .131507C10.377086 .131507 11.285679-1.183562 11.285679-2.582316C11.285679-3.957161 10.400996-5.284184 9.026152-5.284184S6.659029-4.196264 6.073225-3.239851ZM6.38406-2.833375C6.874222-3.694147 7.758904-4.901619 9.109838-4.901619C10.377086-4.901619 11.022665-3.658281 11.022665-2.582316C11.022665-1.41071 10.221669-.442341 9.169614-.442341C8.476214-.442341 7.938232-.944458 7.687173-1.195517C7.388294-1.518306 7.113325-1.888917 6.38406-2.833375ZM5.559153-2.319303C5.068991-1.458531 4.184309-.251059 2.833375-.251059C1.566127-.251059 .920548-1.494396 .920548-2.570361C.920548-3.741968 1.721544-4.710336 2.773599-4.710336C3.466999-4.710336 4.004981-4.208219 4.25604-3.957161C4.554919-3.634371 4.829888-3.263761 5.559153-2.319303Z'/>
</defs>
<g id='page1'>
<use x='38.854296' y='65.753425' xlink:href='#g0-49'/>
</g>
</svg>
The results:
Fields and templates
This is my front field (from the standard Basic card):
<b>Svg generated by dvisvgm 2.11.1 looks like this:</b><br>This is infinity <img class="img_math" src="Latex_Unendlich.svg">.<br><br><br><b>Latex (gen by anki) </b><b>looks like this</b><b>:</b><br>This is infinity [latex]<br> \begin{math}<br> \mathsf{ \infty }<br> \end{math}<br>[/latex].<br><br><br><b>Mathjax looks like this:</b><br>This is infinity <anki-mathjax>\infty</anki-mathjax>.
Template is default, except for the css:
.card {
font-family: arial;
font-size: 20px;
text-align: center;
color: black;
background-color: white;
}
img.img_math {
height: 0.55em;
}
Desired Behavior
I want to have scaleable, themable math equations that load without noticeable delay and have a height similar to my font-size (something like setting height: 1em;
for the <img>
tag.)
Actual Behavior
- With mathjax it renders very slowly.
- With Ankis latex it renders poorly and isn’t scale- or themable.
- With dvisvgm some equations are too big, some others too small.
Example from actual card
Something like this would be reasonable and desireable:
Instead, I get this:
The infinity and the thing with N = {…} are two separate .svg files, both generated by dvisvgm 2.11.1 and both .svg files get scaled by the same css, like shown above.
Question
- How can I get the mathjax results to safe on my disk as .svg and reuse them (not just cached but actually in the media folder)?
- If #1 isn’t possible, how can I make latex work so that the equations are scaled correctly (without having to add custom css to all of them)?
Anki Debug Info
Anki 24.11 (87ccd24e) (ao)
Python 3.9.18 Qt 6.6.2 PyQt 6.6.1
Platform: Linux-6.1.0-0.deb11.21-amd64-x86_64-with-glibc2.31
===Add-ons (active)===
(add-on provided name [Add-on folder, installed at, version, is config changed])
AnkiWebView Inspector ['31746032', 2023-06-27T21:26, 'None', '']
Image Occlusion Enhanced ['1374772155', 2022-04-09T09:15, 'None', '']
Review Heatmap ['1771074083', 2022-06-30T03:43, 'None', '']
Study Time Stats ['1247171202', 2024-02-24T17:59, 'None', '']
===IDs of active AnkiWeb add-ons===
1247171202 1374772155 1771074083 31746032
===Add-ons (inactive)===
(add-on provided name [Add-on folder, installed at, version, is config changed])