Anki was crashed when I use LaTeX packages

I’m sure I have carefully read the “LaTeX” chapter in “Anki-Manual”. :no_mouth:But Anki was crashed without any error message when I use the “chemfig” package of LaTeX. :sob:
The LaTeX header is very simple which I only added “\usepackage{chemfig}” code like the handbook shown.
Here is my full header code:

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

Then I added a card for testing LaTeX:

Does the sum below converge?

[latex]\begin{displaymath}\sum_{k = 1}^{\infty}\frac{1}{k}\end{displaymath}[/latex]

It works well! :slightly_smiling_face:
1

It also works well in LaTeX editor (ps:my LaTeX editor is “TeX Live”).

Then I tryed to use chemfig package to render “2-Methyl pyrethane” in LaTeX editor:

\chemfig{[7]H_3C-CH(-[6]CH_3)-[1]CH_2-CH_2-[1]CH_3}

2
Good!
Then I put this code into Anki card:

[latex]\chemfig{[7]H_3C-CH(-[6]CH_3)-[1]CH_2-CH_2-[1]CH_3}[/latex]

But Anki was crashed, or more accurately it had no responding when I want to see the results by whatever clicking “preview” button or clicking “Study Now” button.
So I have to abort this progress by using “Windows Task Manager” :upside_down_face:
My Anki version is 2.1.44, and my “TeX Live” is the latest version.
Does anyone know what is going wrong? Thanks :slightly_smiling_face:

ps:Please ignore my grammar mistakes, I am not a English native speaker.

Did you adjust the packages setting?

https://docs.ankiweb.net/math.html#assumed-knowledge

If you continue to have trouble, I’d recommend you use MathJax instead.

1 Like

I’m grateful for your reply! :smiley:
I guess I may have found the key of the mistakes with your help.
I found that the problem isn’t in the package I had used, it is the renderer mistake.
I checked the “anki_temp” folder and found that “tmp.dvi” file couldn’t be rendered correctly, So I changed LaTeX header:

\documentclass[UTF-8]{ctexart}

The good news is Anki did not crash when I tryed to preview. :grinning:
But bad news is the chemical formula didn’t render correctly. :dizzy_face:
I realized that anki used “LaTeX” to render and “dvipng” to convert “.dvi” file into “.png” file.
That means I should use “XeLaTex” instead of “LaTex” and use “dvipdfmx” instead of “dvipng”, but I was confused by the commands in dvipdfmx’s Options.

Usage: dvipdfmx [OPTION]... [DVIFILE[.dvi|.xdv]]
       dvipdfmx --extractbb|--xbb|--ebb [OPTION]...     Be "extractbb"
       dvipdfmx --help|--showpaper|--version
Convert DVI or XDV input to PDF; defaults given below.

Options:
  -c            Ignore color specials (for B&W printing)
  --dvipdfm     Enable DVIPDFM emulation mode
  -d number     Set PDF decimal digits (0-5) [3]
  -f filename   Load additional font map filename[.map]
  -g dimension  Annotation "grow" amount [0.0in]
  -h | --help   Show this help message and exit
  -l            Landscape mode
  -m number     Set additional magnification [1.0]
  --mvorigin    Translate the origin for MP inclusion
  -o filename   Set output file name, "-" for stdout [DVIFILE.pdf]
  -p papersize  Set papersize [a4]
  -q            Be quiet
  -r resolution Set resolution (in DPI) for raster fonts [600]
  -s pages      Select page ranges [all pages]
  --showpaper   Show available paper formats and exit
  -t            Embed thumbnail images of PNG format (DVIFILE.pageno, pageno=int)
  --version     Output version information and exit
  -v            Be verbose
  -vv           Be more verbose
  --kpathsea-debug number       Set kpathsea debugging flags [0]
  -x dimension  Set horizontal offset [1.0in]
  -y dimension  Set vertical offset [1.0in]
  -z number     Set zlib compression level (0-9) [9]
  -C number     Specify miscellaneous option flags [0]:
                  0x0001 reserved
                  0x0002 Use semi-transparent filling for tpic shading command,
                         instead of opaque gray color; requires PDF 1.4.
                  0x0004 Treat all CIDFont as fixed-pitch font.
                  0x0008 Do not replace duplicate fontmap entries.
                  0x0010 Do not optimize PDF destinations.
                  0x0020 Do not use predictor filter for Flate compression.
                  0x0040 Do not use object stream.
                Positive values are always ORed with previously given flags.
                And negative values replace old values.
  -D template   PS->PDF conversion command line template [none]
  -E            Always try to embed fonts, regardless of licensing flags.
  -I number     Image cache life in hours [-2]
                 0: erase all old images and leave new images
                -1: erase all old images and also erase new images
                -2: ignore image cache
  -K number     Encryption key length [40]
  -M            Process MetaPost PostScript output
  -O number     Set maximum depth of open bookmark items [0]
  -P number     Set permission flags for PDF encryption [0x003C]
  -S            Enable PDF encryption
  -V number     Set PDF version [1.5]

All dimensions entered on the command line are "true" TeX dimensions.
Argument of "-s" lists physical page ranges separated by commas,
        e.g., "-s 1-3,5-6".
Papersize is specified by paper format (e.g., "a4")
        or by w<unit>,h<unit> (e.g., "20cm,30cm").

Email bug reports to dvipdfmx@tug.org.

What is the correct codes shuold I put or change in “Edit LaTeX build process” add-on, or am I doing this right? :thinking:
Thanks!

After hours of research, I have totally solved this confusing problem. :smiley:
The heart of this matter is that Anki’s LaTeX renderer used “pdfTeX” as a default while “chemfig” package can not be rendered correctly by it.
So I used “pdfLaTeX” replaced “pdfTeX” and used “ImageMagick” replced “dvipng” and made some changes:

"pngCommands": [
        [
            "pdflatex",
            "-interaction=nonstopmode",
            "tmp.tex"
        ],
        [
            "magick",
            "convert",
            "-quality",
            "100",
            "-transparent",
            "white",
            "-trim",
            "tmp.pdf",
            "tmp.png"
        ]
    ]

Now the “chemfig” package can run smoothly and fastly so that I can draw verious organic chemical formulas in Anki!:grin:

Here are some examples:

%2-methylpentane
\chemfig{[7]H_3C-CH(-[6]CH_3)-[1]CH_2-CH_2-[1]CH_3}

%Caffeine
\chemfig{*6((=O)-N(-CH_3)-*5(-N=-N(-CH_3)-=)--(=O)-N(-H_3C)-)}

%ATP
\setchemfig{cram width=3pt}
\definesubmol{a}{-P(=[::-90,0.75]O)(-[::90,0.75]HO)-}
\chemfig{[:-54]*5((--[::60]O([::-60]!aO([::-60]!aO([::60]!aHO))))<(-OH)-[,,,,line width=2pt](-OH)>(-N*5(-=N-*6(-(-NH_2)=N-=N-)=_-))-O-)}

2-methylpentane
Caffeine
ATP

Glad you were able to get things going! And apologies for the MathJax suggestion; I had been thinking of mhchem rather than chemfig.