Convert LaTeX (Tikz) Anki Note to .png Error
The original problem was caused by a buggy converter from pdf to png. It was first solved by downgrading from imagemagick
7.0.x to imagemagick
6.9.x and then by switching to sips
. Below I describe how to setup Anki for use with pdflatex and imagemagick
/sips
.
How to set up Anki for pdflatex (as required by tikz and other packages)
The setup consists of adding
- a suitable preamble and
- a corresponding add-on (python script) for converting TeX code to png pictures.
We first present two variants with the corresponding code fragments, and then explain where to put these fragments in Anki.
Variant 1 (recommended): standalone
class, png conversion with imagemagick
or sips
The standalone
class is able to crop/trim the image itself and therefore can be used with converters that can't do it (like sips
).
Preamble:
\documentclass[12pt,border=1mm,varwidth=3in]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\begin{document}
Modify the value 3in
for the maximal width to be smaller or greater.
Anki add-on for pdf/png conversion using convert
from the imagemagick
tools; call it e.g. pdflatex-convert.py
:
newLaTeX = \
[
["pdflatex", "-interaction=nonstopmode", "tmp.tex"],
["convert", "-density", "200", "-trim", "tmp.pdf", "tmp.png"]
]
import anki.latex
anki.latex.latexCmds = newLaTeX
Alternatively, under MacOS you can use the pre-installed tool sips
(scriptable image processing system) with the following script pdflatex-sips.py
:
newLaTeX = \
[
["pdflatex", "-interaction=nonstopmode", "tmp.tex"],
["sips", "-s", "format", "png", "tmp.pdf", "--out", "tmp.png"]
]
import anki.latex
anki.latex.latexCmds = newLaTeX
Variant 2: article
class, png conversion with imagemagick
This preamble is close to Anki's default, hence it probably works with all existing notes. It is less robust than the preamble using standalone
. E.g., if a box (like an included picture) is to wide for the page, it may be moved to a second page; but then the conversion to png
will fail.
\documentclass[12pt]{article}
\usepackage[text={3in,5in}]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\pagestyle{empty}
\setlength{\parindent}{0in}
\begin{document}
As Anki add-on for pdf/png conversion use pdflatex-convert.py
from above. pdflatex-sips
will not work since neither the article
class nor sips
will trim the picture.
Remarks. If you want to use macros from specific packages, you have to add the corresponding \usepackage
commands to the preambles above. E.g., you may additionally need \usepackage{tikz}
for pictures. You don't have to collect all packages that you have ever used or will ever use in a single preamble; define instead several types of notes with different preambles, e.g. one for pictures and one for texts.
The Anki add-ons presented above are variants of Edit_LaTeX_build_process.py
as offered at the Anki website, and replace the latter.
How to install the Anki add-on
Start Anki and select Tools
| Add-ons
| Open Add-ons Folder...
. Copy the Python script (the py
-file from above) into this folder. Move any other script that modifies Anki's LaTeX handling, like Edit_LaTeX_build_process.py
and its compiled version Edit_LaTeX_build_process.pyc
, out of the way, e.g. by moving them into an sub-folder old
.
After that you have to restart Anki.
How to install the preamble
Start Anki, select Tools
| Manage Note Types
| your note type using latex | Options
, and copy the preamble into this window.
Example
Paste the code [latex]{{c1::\ce{H3O+} }}\begin{tikzpicture}\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);\end{tikzpicture}[/latex]
into a note of type cloze
that uses one of the preambles above with the additional packages tikz
and mhchem
.
Anki generates the following two cards with a chemical formula and a simple tikz picture.