5

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'

Add-ons Anki "Edit_Latex_build_process.py

# -*- coding: utf-8 -*-
# Edit LaTeX generation procedure
# Soren I. Bjornstad <soren.bjornstad@gmail.com>
# 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
# configuring this add-on, you may ask on the add-ons forum.
#
# 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}
tym007
  • 75
  • 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 08 '16 at 08: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 08 '16 at 08: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 08 '16 at 08: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 08 '16 at 18:24

2 Answers2

4

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.

enter image description here

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.

enter image description here

Anki generates the following two cards with a chemical formula and a simple tikz picture.

enter image description here enter image description here

gernot
  • 49,614
  • 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 08 '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 08 '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 08 '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 08 '16 at 19:25
  • oops, when I execute convert -density 300 tmp.pdf tmp.png I have a error "Abort trap: 6" – tym007 Oct 08 '16 at 19:32
  • In the first method, have a look at tmp.log to find out where this Extra } error comes from. If you run pdflatex manually on tmp.tex, do you get an error, or does the resulting tmp.pdf look as it should? – gernot Oct 08 '16 at 19:35
  • On the imagemagick website there is the advice to upgrade to the newest imagemagick version in case of trap: 6, as this error indicates images that corrupt memory, and the newer the version of imagemagick is, the more memory problems have been fixed. I have version 6.8.9 installed, but the newest one seems to be 7.0.3. What does convert -version say? – gernot Oct 08 '16 at 19:46
  • @tym007 Does tmp.tex compile without errors, manually? If not, add it temporarily to your posting. If yes, check whether the pdf looks ok, and send it to the imagemagick people; in a discussion they promised to fix trap 6 memory problems within a few days. – gernot Oct 08 '16 at 20:05
  • @tym007 So the problem is that convert from the imagemagick suite fails to convert a pdf- to a png-file with a trap 6 error, right? You can report it as a bug to the imagemagick project, and/or try to use sips. This is a conversion tool under MacOS that might work as well. See e.g. "Converting Image File Formats with the Command Line & sips". – gernot Oct 08 '16 at 20:53
  • @tym007 I have added a note about how to use sips, after checking that it really works. – gernot Oct 08 '16 at 23:01
  • Hi, I tested three solution. She walks all, but I can not add this code without its plant. It only works with the original Add-on \begin{easylist} & Main item~: && Sub item. && Another sub item. \end{easylist} One idea is to solve the problem? – tym007 Oct 09 '16 at 12:59
  • For the moment I manually change between the two add-on when I need to use tikz. Once the convertion is made in the image remains the media collection Anki. But there must be a Conditional stucture that could meet both. – tym007 Oct 09 '16 at 17:00
  • @tym007 I think I've found a solution, see both variants above (I've rewritten my answer once more). Please test the methods with your notes and let me know if you encounter problems. – gernot Oct 09 '16 at 17:35
  • Actually this function with both methods, whether with "itemize" or "minipage. It does not work with the troix method that has been removed. ["pdflatex", "-interaction=nonstopmode", "-shell-escape", "tmp.tex"], We must go "itemize" or "minipage" in the setting of the card because if you write next, anki puts side by side images. Thanks – tym007 Oct 09 '16 at 19:29
  • @tym007 I've just modified the standalone approach: When one adds the varwidth option, also your easylist example works. Do I understand you correctly that at the moment everything works? Or is there still a problem? (I didn't completely understand whether your remark on itemize, minipage, and side-by-side images describes a problem or is just an explanation.) – gernot Oct 09 '16 at 19:34
  • it works with "tikz", "mhchem" too. My problem is solved. – tym007 Oct 09 '16 at 19:37
  • Hello, do you know if it's possible to put an image in an Anki card with latex code. I made several test, but without success. thank you – tym007 Oct 10 '16 at 17:36
  • @tym007 Yes, it is possible. Add \usepackage{graphicx} to the preamble, if it is not yet loaded by some other package (like pgfplots). Then [latex]\includegraphics{myfile.png}[/latex] should load the image. Some things may go wrong, though. Inspect tmp.tex and tmp.log, if it fails. E.g., if the image is too wide, use \includegraphics[width=3cm]{...}. If the filename or path contains special characters, you may need \string or \detokenize, see How to use tilde or space in includepdf filename?. – gernot Oct 10 '16 at 20:34
  • @tym007 As a first test, use \includegraphics{example-image-a}. if you have a current TeXLive, then this image is on the search path. If this works, you know that any failures with your own images are due to either "image not found", "image too wide", or "strange characters in file name". – gernot Oct 10 '16 at 20:38
  • @tym007 If the image is too big, then the preamble with the article class will generate a pdf with two pages, and the conversion to png fails (to be solved with [width=...]). The preamble with standalone will just cut the image, but not fail. – gernot Oct 10 '16 at 20:44
  • In fact. For "\ usepackage {graphicx}" walking. We must put the image in the same file as the .tex. But on Anki, I do not know where to put it can be used with this function. An idea? – tym007 Oct 11 '16 at 15:27
  • @tym007 Either give the full path, like \includegraphics{/usr/local/texlive/2016/texmf-dist/tex/latex/mwe/example-image-a.jpg}, or set the \graphicspath, or set the TEXINPUTS variable to include the directory with the images. If the file name or path includes spaces or special characters, see e.g. the answers to How to include graphics with spaces in their path?. For infos on \graphicspath, see the WikiBook on LaTeX/Importing Graphics. – gernot Oct 11 '16 at 16:06
  • J'ai essayer avec ce code, mais cela ne marche pas. '\includegraphics{/usr/local/texlive/2016/texmf-dist/tex/late‌​x/mwe/td.jpg}' – tym007 Oct 12 '16 at 18:55
  • I try with this code, but it does not. '\includegraphics{/usr/local/texlive/2016/texmf-dist/tex/lat‌​e‌​x/mwe/td.jpg}' – tym007 Oct 12 '16 at 19:55
  • @tym007 How does tmp.tex look like? What does tmp.log say? What happens when you compile tmp.tex yourself? Does the image /usr/local/texlive/2016/texmf-dist/tex/lat‌​‌​ex/mwe/td.jpg really exist on your system? It doesn't on my system ... If td.jpg is your image, you have to use the path of this file instead of /usr/local/texlive/2016/texmf-dist... – gernot Oct 12 '16 at 20:34
  • Hello. I want my text does not exceed the edge of the screen automatically. Currently I use \ at the end of each line. advise on? thank you – tym007 Oct 18 '16 at 19:32
  • @tym007 I'm not completely sure that I understand your problem. If the problem is that the page is too wide: Try \documentclass[12pt,border=1mm,varwidth=5cm]{standalone} to set a maximum width of 5cm (change as needed). – gernot Oct 18 '16 at 21:06
  • when I use "paragraph" without configuration used for Anki ("varwidth ......"), I can write my text without exceeds the side of the pdf document. With the configuration Anki Latex, I have to use "\" to prevent it exceeds the pdf document. – tym007 Oct 20 '16 at 11:57
  • [link] [link] [link] [link] – tym007 Oct 20 '16 at 11:58
  • http://www.zupimages.net/viewer.php?id=16/42/54ac.png – tym007 Oct 20 '16 at 18:36
  • http://www.zupimages.net/viewer.php?id=16/42/2w15.png – tym007 Oct 20 '16 at 18:37
  • @tym007 Use the option varwidth=3in for the standalone class (see also the updated answer above). Without giving an explicit width, the standard textwidth will be used. But in both cases I can't really reproduce your problem. If you can't solve the problem, please ask a new question (there are already too many comments) and add the code (preamble as well as document body) to the question. – gernot Oct 21 '16 at 15:57
0

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

Then, I downloaded with brew another version of ImageMagick. (6.9.6-0)

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.

tym007
  • 75