# Convert LaTeX (Tikz) Anki Note to .png Error

Good morning.

I try to apply advice given on this page to be able to include drawings in Latex with Anki. But, for a reason of which I am unaware, Anki does not want to convert files.tex into.png.

It is possible to have a little of assistant  ? Thank you

Hardware : Mac book pro os : El capitain

Anki error :

An error occurred. It may have been caused by a harmless bug,
or your deck may have a problem.
To confirm it's not a problem with your deck, please run Tools > Check Database.
If that doesn't fix the problem, please copy the following
into a bug report:
Traceback (most recent call last):
File "aqt/editor.pyc", line 434, in onCardLayout
File "aqt/clayout.pyc", line 48, in __init__
File "aqt/clayout.pyc", line 60, in redraw
File "aqt/clayout.pyc", line 186, in selectCard
File "aqt/clayout.pyc", line 199, in onCardSelected
File "aqt/clayout.pyc", line 234, in renderPreview
File "anki/cards.pyc", line 120, in q
File "anki/cards.pyc", line 137, in _getQA
File "anki/collection.pyc", line 529, in _renderQA
File "anki/hooks.pyc", line 32, in runFilter
File "anki/latex.pyc", line 41, in mungeQA
File "anki/latex.pyc", line 61, in _imgLink
File "anki/latex.pyc", line 107, in _buildImg
File "shutil.pyc", line 82, in copyfile
IOError: [Errno 2] No such file or directory: u'/var/folders/v1/psh6j3c139x1wkj2k38_77v40000gn/T/anki_temp/tmp.png'

# -*- coding: utf-8 -*-
# Edit LaTeX generation procedure
# add-on Version 2, for Anki 2.0.27+

# I do not think this add-on is really eligible for copyright protection, since
# it simply overwrites a variable in Anki's code, but in case it is, you are
# free to do anything you want with it without needing to ask for permission.

# ========== INSTRUCTIONS ==========
# This add-on does not change anything until you edit it.
#
# Between the square brackets at the far left, you may insert any number of
# lines, each of which specifies a command to be run on your system. When Anki
# goes to generate LaTeX images, it will run each command in sequence. At the
# end of each line, after the closing square bracket, there should be a comma,
# except on the final command.
#
# Each command must be enclosed in square brackets and may contain any number
# of quoted, comma-separated arguments, the first being the command to run and
# the remainder being command-line arguments.
#
# Please see the default command set for an example. If you are having trouble
#
# NOTE: If you are using this add-on on Windows, you must use double
# backslashes (\\) in any paths you include: "C:\\Windows", not "C:\Windows".
# ==================================

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

# TikZ compliant setting
newLaTeX = \
[
["pdflatex", "-interaction=nonstopmode", "--shell-escape", "tmp.tex"]
]

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

Latex code :

\providecommand{\pgfsyspdfmark}[3]{}
\documentclass[convert={convertexe={convert}},border=2]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath, amsfonts, mathrsfs}
\usepackage[paperwidth=5in, paperheight=100in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[version=4]{mhchem}
\usepackage{pgf, tikz}
\usepackage{xcolor}
\pagestyle{empty}
\setlength{\parindent}{0in}
\begin{document}

\ce{{{c1::H3O}}+}
\begin{tikzpicture}
\draw (0, 0) -- (4, 0) -- (4, 4) -- (0, 4) -- (0, 0);
\end{tikzpicture}

\end{document}
• Welcome to TeX.SX! Remove at least the line loading the geometry package. It doesn't make sense with the standalone class, but influences the output. With this line, the page is empty, without it I get the string c1::H30^+ followed by a square, which one would expect from the code. Btw, what is the expected output? – gernot Oct 8 '16 at 8:08
• What is the purpose of \ce{{{c1::H3O}}+}? It doesn't look like the mhchem package is able to handle it, in contrast to \ce{H3O+}. – gernot Oct 8 '16 at 8:22
• {{c1:: }} Allow to hide a note with Anik, but he has no consequence to the latex in that case. Advice which I followed was for an user of Windows, but me I work with Mac. I had to make a mistake in some orders which are not compatible with Mac. I delete geometry but it still does not work. Thank for your help. – tym007 Oct 8 '16 at 8:45
• I have added an answer which describes how I get it running under Linux/Ubuntu 16.04. My hope is that it will also work under MacOs. If you still have probems, we have to check the files in Anki's temporary directory (and I have to grab my son's Mac). – gernot Oct 8 '16 at 18:24

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)

• 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 ${{c1::\ce{H3O+} }}\begin{tikzpicture}\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);\end{tikzpicture}$ 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.

• Thanks for your help. I changed my file as described but I unfortunately still a mistake. I try another method but I have a problem with the command "convert". Yet she walks into the terminal. 2ème méthod : newLaTeX = \ [ ["pdflatex", "-interaction=nonstopmode", "tmp.tex"], ["convert", "-trim", "-density", "300", "tmp.pdf", "tmp.png"] ] (With this method all files are created except tmp.png) – tym007 Oct 8 '16 at 18:38
• bug report: IOError: [Errno 2] No such file or directory: u'/var/folders/v1/psh6j3c139x1wkj2k38_77v40000gn/T/anki_temp/tmp.png' – tym007 Oct 8 '16 at 18:39
• @tym007 Do you have imagemagick installed on your computer? On the command line, enter which convert. If it says something like /usr/bin/convert, go to Anki's tmp directory and execute convert -density 300 tmp.pdf tmp.png manually. View the pdf and the png files; do they look ok? – gernot Oct 8 '16 at 18:45
• To be more precise in the error that gave me in the first method. ! Extra }, or forgotten Error executing pdflatex.......... ................... ! ==> Fatal error occurred, no output PDF file produced! Transcript written on tmp.log. system returned with code 256 (./tmp.aux)sh: convert: command not found system returned with code 32512 Class standalone Warning: Conversion unsuccessful! (standalone) There might be something wrong with your (standalone) conversation software or the file permissions! – tym007 Oct 8 '16 at 19:25
• oops, when I execute convert -density 300 tmp.pdf tmp.png I have a error "Abort trap: 6" – tym007 Oct 8 '16 at 19:32

Good morning.

Already, thanks to Gernot to have helped me.

To conclude, the version of convert of ImageMagick 7.0.1 did not walk.

I therefore abolished the version of ImageMagick that I had installed with Mac port.

sudo port uninstall imagemagick

brew instal imagemagick

I did not supplement installation procedure as to point out on this site, not to change the version of ImageMagick.

Create (or choose) a directory to install the package into and change to that directory, for example:

cd $HOME Next, extract the contents of the package. For example: tar xvzf ImageMagick-x86_64-apple-darwin15.2.0.tar.gz Set the MAGICK_HOME environment variable to the path where you extracted the ImageMagick files. For example: export MAGICK_HOME="$HOME/ImageMagick-7.0.3"
If the bin subdirectory of the extracted package is not already in your executable search path, add it to your PATH environment variable. For example:

export PATH="$MAGICK_HOME/bin:$PATH"
Set the DYLD_LIBRARY_PATH environment variable:

export DYLD_LIBRARY_PATH="\$MAGICK_HOME/lib/"
Finally, to verify ImageMagick is working properly, type the following on the command line:

magick logo: logo.gif
identify logo.gif
display logo.gif
Note, the display program requires the X11 server available on your Mac OS X installation DVD. Once that is installed, you will also need to export DISPLAY=:0.

I then tested by letting the same regulatings give by Gernot and all these pasts correctly.

No error all my files are generated.

Thank you still to have helped me.