[Tex/LaTex] How to use LaTeX in Anki on Windows with MiKTeX

ankimiktexwindows

I installed MikTeX, updated everything, downloaded the passivetex package, ran everything as admin, checked the depositories and everything, and yet I cannot get LaTeX to work with Anki. Here is the error message I get:

Error executing latex.
Generated file: C:\Users\Leon\AppData\Local\Temp\anki_temp\tmp.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.19 (MiKTeX 2.9.6930 64-bit)
entering extended mode
(tmp.tex
LaTeX2e 
("C:\Users\Leon\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\article.cls"
Document Class: article 2018/09/03 v1.4i Standard LaTeX document class
("C:\Users\Leon\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\size12.clo"))
("C:\Users\Leon\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\fontenc.sty"
("C:\Users\Leon\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\t1enc.def"))
("C:\Users\Leon\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\inputenc.sty"

! LaTeX Error: File `utf8x.def' not found.

Type X to quit or  to proceed,
or enter new name. (Default extension: def)

Enter file name: 
! Emergency stop.
 

l.165 \endinput


No pages of output.
Transcript written on tmp.log.

Any help with this? I am on Windows 10.
EDIT: I have the admin premissions and have turned off autoupdates in MikTeX.

Best Answer

For a fresh user to Anki using LaTeX and Miktex there is a sharp initial learning curve with several pitfalls. When you enter LaTeX code into Anki it is best to start with the simplest of examples so to get a Question and Answer such as

enter image description here

You need to start with this simple entry enter image description here

Does the divergent p-series 
[latex]\begin{math}\sum_{k = 1}^{\infty}\frac{1}{k}\end{math}[/latex]
 converge?

The series converges for p>1 and diverges for 0<p≤1. With p=1 it is called the Harmonic Series.

The first error message may be

Error executing latex.
Generated file: C:\Users\yourname\AppData\Local\Temp\anki_temp\tmp.tex
Have you installed latex and dvipng/dvisvgm?  

This could mean you have not installed MiKTeX correctly (including setting the paths). To check this open command line and try and run latex, dvipng, and dvisvgm and make sure the system can find these. If the system can find all these programs, the error message can also be generated by Anki not having sufficient privileges to fetch all necessary packages. Try running Anki one time in Administrator mode.

IF you get other error messages then either your latex code is not right or MiKTeX has not been configured correctly.

One test to see what is happening is to check Anki's TeX output which is a file
%temp%\anki_temp\tmp.tex (you can enter %temp%\anki_temp in windows explorer address bar to get there quickly) If you open tmp.tex in notepad then check/verify its contents are identical to the MWE shown below .

It is best to run MiKTeX-console and ensure it is fully stable before Anki calls it in the background.

The best way to check MiKTeX can run the same code as above is to use the included TeXworks editor and run the same minimal example until it works.

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

\begin{math}\sum_{k = 1}^{\infty}\frac{1}{k}\end{math}
\end{document}

so it should show enter image description here

Once you are confident that the above simple sample works with TeXworks IT should then work in Anki and in the temp directory (see above) you will find half a dozen files including two trace logs the short one is latex_log.txt (which Anki feeds back to the user) also is the more useful tmp.log with more useful information from the tex compilation.


In summary
Although the Anki manual and other older guides suggest settings are changed to "Install missing packages on the fly" is set to "No", not to "Ask me first" It is essential that MiKTeX is allowed to update itself

Thus I recommend the following (until someone corrects me as to why not?)
DO use the setting "Always install missing packages on the fly" until you have a stable setup, then (if you must) switch it to no.

While you are there in the console ensure amsfonts and miktex-dvipng-bin-2.9 have been added to the active packages (there should be a date under "installed on", it has also been recommended to include passivetex and ucs packages to clear the above utf8x error, however the simple example above runs correctly without it. It is easy to add or remove it using + and -.

Most importantly, periodically enter MiKTeX-console to manually force Tasks Refresh file name database as well as Refresh font map files this ensures your errors messages are minimal.

Whatever the problem is with Anki this TeX forum will require to see the contents of tmp.tex (what we may call the MWE) within your question so as to see what caused any error messages reported in the logs. It may also be useful to add the [latex] blah bla blah [/latex] line you used within Anki (although that should by default be included in the tmp.tex that you post).

If you customise LaTeX calling from Anki using the "Edit LaTeX build process" with the add-on from https://ankiweb.net/shared/info/937148547 or one of its variants then we will also need to know it was added and certainly what was changed especially if you extend to tikz or or other packages.