# LaTeX vertical alignment and font size

It would be nice if the LaTeX equations that are generated were vertically aligned.
For example, the input Hello [$]3_\textrm{World}[/$], renders as

whereas something like

would be better. Fractions are especially ugly in the current way.

For the implementation details, dvipng generates a depth file which indicates the offset of the image: from the dvipng manpage

--depth*
Report the depth of the image. This only works reliably when the LaTeX style preview.sty from preview-latex is used with the
active option. It reports the number of pixels from the bottom of the image to the baseline of the image. This can be used
for vertical positioning of the image in, e.g., web documents, where one would use (Cascading StyleSheets 1)

<img src="<filename.png>" style="vertical-align: -<depth>px">

The depth is a negative offset in this case, so the minus sign is necessary, and the unit is pixels (px).


Also note that in the latter version, the image has been rescaled so the font size matches the one of the surrounding context. This can also be achieved by dvipng, with the -D option: from the dvipng manpage

-D num
Set the output resolution, both horizontal and vertical, to num dpi (dots per inch).

One may want to adjust this to fit a certain text font size (e.g., on a web page), and for a text font height of font_px
pixels (in Mozilla) the correct formula is

<dpi> = <font_px> * 72.27 / 10 [px * TeXpt/in / TeXpt]

The last division by ten is due to the standard font height 10pt in your document, if you use 12pt, divide by 12.
Unfortunately, some proprietary browsers have font height in pt (points), not pixels. You have to rescale that to pixels,
using the screen resolution (default is usually 96 dpi) which means the formula is

<font_px> = <font_pt> * 96 / 72 [pt * px/in / (pt/in)]

On some high-res screens, the value is instead 120 dpi. Good luck!


By the way, the latter output was generated by a firefox extension called LaTeX It!, which does exactly that, converting pieces of mail surrounded by \$ into images and inserting them back inlined. Here is the link to their implementation.

Have you considered using MathJax instead?

No, for two reasons:

1. I work a lot on AnkiDroid too, where MathJax is absent;
2. It is (very) slow to compile on-the-fly all the LaTeX, sometimes slower than actually providing an answer.

MathJax 3 in the latest Anki versions is reasonably fast - have you tried it and found its performance wanting? I believe it’s already integrated into AnkiDroid as well. Update AnkiDroid to Mathjax 3 by hgiesel · Pull Request #7782 · ankidroid/Anki-Android · GitHub

If you’re stuck on LaTeX, you can probably adjust the LaTeX arguments in an add-on.

I’ll try out MathJax, despite the fact that it’s not really the solution to the problem, but rather a way to hide it until MathJax won’t be able to cover up for everything that one might want from pre-compiled LaTeX.

Of course, this moment might never occur, if MathJax is actually good enough to provide everything the LaTeX compiler may offer. However, if this is the case, then there is no point in even maintaining LaTeX compilation in the first place. If Anki will, in the future, drop the LaTeX compilation for an other solution, I would completely understand that, and maybe that would be the right thing to do.

But, if it is not the case, I think it would make sense to definitively to correct this odd behavior, especially because tools like dvipng already though of this problem, and already provide the solution. I can’t, however, just change the command arguments because not only dvipng has to provide the depth of the image, but you also have to use it inside the HTML renderer that (I assume) Anki uses, simply by adding a parameter to the img tag.

I would do it by myself, but I unfortunately don’t have, currently, enough time to dig in the Anki source code and documentation to find out exactly how is the HTML that will be rendered generated, and to add other small details such as storing somewhere in the media database the depth information (this is why I posted it as a feature suggestion).

I hope you’ll fix this someday. Anyways, thanks for your time, all the work you provide with Anki is very useful.

I hope that never happens. All my cards are all LaTeX. I do that since 1) my notes are in LaTeX and don’t want to translate them to something else to be out in Anki 2) I have tables that are difficult to make in anything but LaTeX 3) my equations have constructs that are not easily supported in Mathjax 4) some of the materials for my exams only come with anki cards that are created only with LaTeX.

I personally think the cards look great when rendered with the SVG option.

Just my 2cts.

Yes, but do you render the whole card as LaTeX? My problem arises when I want to insert LaTeX content within html content (which I can’t do otherwise with cloze deletions, for example), and that the generated image isn’t aligned. I personally am not a great fan of MathJax if I could be compiling once and for all my equations, but the absence of vertical alignment and font resizing is annoying. Btw tweaking the vertical alignment and font resizing wouldn’t break anything in latex compilation, just shift and rescale the images so that they fit the rest of the text.

Yes, my whole card is in LaTeX which doesn’t have the problem you face. I was just voicing a concern if the author of anki was considering removing the latex feature since they have mathjax.

I’m pretty sure I’ve seen either an add on in the past maybe for the 2.0 or some post on Reddit that talks or addresses how to fix the alignment issue.

I don’t have any plans to drop LaTeX support. But it’s a fairly niche feature, and I think MathJax is a better solution for the typical user - so updates to the LaTeX code fall fairly low on the priority list.

Does that mean that it’s not likely to have any time soon the aforementioned update, or just that the delay will longer than usual feature requests?

It’s a “maybe someday”, and will likely not happen in the near future.

I have tried using MathJax and I have found an other issue I didn’t have with pre-compiling LaTeX: AnkiDroid requires you to actually go through all your cards to cache the result, and doesn’t have access to MathJax offline, meaning I have to review with an internet connection every card in my collection at least once after each modification. This is quite annoying since I use AnkiDroid only when I don’t have access to a laptop, which 95% of the time also means don’t have any internet connection.

Please excuse my harsh language, but I think this is complete and utter bullsh*t. I’m using MathJax on AnkiDroid on a regular basis and it always instantly renders the result after a modification, even without an internet connection.

My personal opinion is that MathJax is very convenient for short formulas because you won’t have to worry about compiling. I’m using it very frequently for mathematics and the only time MathJax has failed me in the past was when I needed to draw a commutative diagram with diagonal edges.