Anki: Displayed size does not match specified size

In Anki, the sizes I specify in LaTeX encoding do not match those that are displayed.

MWE: Leaving the header and footer at the Anki default of

\documentclass[12pt]{article}
\special{papersize=3in,5in}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\pagestyle{empty}
\setlength{\parindent}{0in}
\begin{document}


and

\end{document}


respectively, typing $\rule{1cm}{1cm}$ (and adjusting the note's CSS so that the produced image is not resized with img{max-height:none; max-width:none;}) will display an approximately 1.75cm by 1.75cm square, as opposed to the expected 1cm by 1cm square.

How can I adjust this so that the size I specify in LaTeX is accurately displayed?

The obvious solution would be some way to scale an entire LaTeX document, but my searches for such have been unfruitful.

I would prefer not to resort to re-scaling the actual images produced by Anki (hence the CSS), as this renders them blurry.

What I have tried:

• Adjusting the font size will not fix the MWE above, as LaTeX still thinks that 1cm is ~1.75cm.

• Adjusting the paper size does not seem to do anything.

What others have suggested, and I have tried:

• \scalebox and \resizebox (after putting \usepackage{graphicx}/\usepackage{graphics} in header) do not resize anything visually, but can cause other LaTeX objects to register them as smaller. For example, with the modified header and default footer,

$\scalebox{0.5}{\rule{1cm}{1cm}}foo bar$

will produce a >1cm box blocking the word foo, followed by the word bar. (Suggested by @John Kormylo)

Edit: Info on anki LaTeX compile process. Removed, see my answer if you really want to know.

Edit: Semi-failed attempt, Removed, see my answer if you really want to know.

The previous solution I found (previous solution) uses the standalone class, and does not handle the graphicx package well, nor images, which are extremely useful in anki. To fix this, I have instead figured out the following.

In Anki, putting the following as the LaTeX header:

\documentclass[12pt]{article}
\RequirePackage{graphicx}
\RequirePackage{environ}
\pagestyle{empty}
\setlength{\parindent}{0in}
\NewEnviron{ankicard}{\fontsize{16}{19}\selectfont\scalebox{0.6}{\BODY}}{}
\begin{document}
\begin{ankicard}


and the following as the footer:

\end{ankicard}\end{document}


and, in the Edit LaTeX build process addon to anki, putting the following:

#original setting
#newLaTeX = \
#[
#    ["latex", "-interaction=nonstopmode", "tmp.tex"],
#    ["dvipng", "-D", "200", "-T", "tight", "tmp.dvi", "-o", "tmp.png"]
#]

# ======

#Setting for pdflatex compilation
newLaTeX = \
[
["pdflatex", "-interaction=nonstopmode", "tmp.tex"],
["pdfcrop", "tmp.pdf"],
["pdftoppm", "-png", "-r", "200", "tmp-crop.pdf", "tmp"],
["ankirename.bat"]
]

# make the changes
import anki.latex
anki.latex.latexCmds = newLaTeX


(only the second list is new, rest comes w/ addon), and putting ankirename.bat somewhere in your path, with the contents

rename tmp-1.png tmp.png


Should scale all (newly generated) LaTeX in anki to the appropriate size. If it does not, you can fiddle with the 0.6 parameter in scalebox in the ankicard environment of the LaTeX header. This should also allow placing and scaling graphics in anki, and should allow LaTeX to automatically wrap any text that you type that is too long without the varwidth package that the standalone class requires to do so, which seems to conflict with graphicx.

The explanation:

As the previous solution has said, some packages require the pdflatex compiler, which does not always output all of the necessary data in any produced .dvi files, so not only will anki's default use of latex ... not work, the second command of dvipng ... won't work either, so we have to change both of these.

Switching to pdflatex ... compilation is straightforward, ripped right from the linked answer. This will produce a file called tmp.pdf

However, if we want to use the article document type (as anki uses by default) to allow inserting and scaling images and such, we can't use the standalone class like the other answer mentions, so not only do we not get the automatic compilation to .png that standalone provides, we don't get its automatic cropping either. Fortunately, these are both easy to fix, with pdfcrop, which produces a file tmp-crop.pdf, and pdftoppm, which produces a file called tmp-1.png. (Note: if pdfcrop --debug is saying that it both can and can't find ghostscript, double check that C:\Windows\System32 is in your path.)

Lastly, anki does not actually use a shell to execute these commands, it simply runs the executables. But because rename and mv are not (as I infer from the fact that they produce errors here) actual programs, but rather functions in a shell, we cannot put rename tmp-1.png tmp.png directly into the LaTeX build process file. Instead, we just add a layer of indirection and have it call a .bat file that does that for us.