1

I am trying to embed images into Anki flashcards using \includegraphics.

A similar question was asked (and answered) some years ago - unfortunately the solution given there does not seem to work anymore. A related more recent (unresolved) question is this one, which however relates to .eps files only and runs up against a different type of error .

The following minimal example describes my problem.

Header:

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\graphicspath{{/home/user/Downloads/}}

\begin{document}

Footer: \end{document}

Card Front: Most magnificent animal

Card Back:

[latex]
\begin{center}
\includegraphics{duck.jpg} \\\relax 
\end{center}
[/latex]

(image source)

Error:

This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(./tmp.tex
LaTeX2e <2020-02-02> patch level 2
L3 programming layer <2020-02-14>
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty)
(/usr/share/texlive/texmf-dist/tex/latex/graphics/graphicx.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/latex/graphics/graphics.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/trig.sty)
(/usr/share/texlive/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)
(/usr/share/texlive/texmf-dist/tex/latex/graphics-def/dvips.def)))
(/usr/share/texlive/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)
(./tmp.aux)

! LaTeX Error: Cannot determine size of graphic in /home/user/Downloads/duc
k.jpg (no BoundingBox).

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.9 \begin{center}\includegraphics{duck.jpg}
                                            \\\relax \end{center}
</home/user/Downloads/duck.jpg> [1] (./tmp.aux) )
(see the transcript file for additional information)
Output written on tmp.dvi (1 page, 364 bytes).
Transcript written on tmp.log.

So the relevant bit here seems to be Cannot determine size of graphic in /home/user/Downloads/duck.jpg (no BoundingBox) which does not seem to be too uncommon of an error. Unfortunately though, the solutions provided here, such as specifying natwidth and natheight do not seem to help with the matter. Notably, replacing duck.jpg with example-image-a yields a correctly compiling card.

Am I overlooking something obvious? Is it currently possible to use graphicx together with Anki?

jro
  • 111
  • Related: https://tex.stackexchange.com/questions/320139/how-to-get-anki-working-with-tikz-based-packages – Marijn Jun 02 '20 at 10:11
  • 1
    Short summary: Anki uses latex to compile to the dvi file format and then convert the dvi to png and then include the png on the flashcard. When compiling to dvi you cannot (easily) use jpg images. Therefore an alternative is to compile to pdf instead and convert the pdf to png and then include the png in the flashcard (all automatically). – Marijn Jun 02 '20 at 10:13
  • Oh this is very helpful, thank you! I'll expand this into an answer if you don't mind. – jro Jun 02 '20 at 11:24

1 Answers1

0

Thanks to @Marijn for pointing me in the right direction. The solution is essentially given here together with some background. For completeness:

Header:

\providecommand{\pgfsyspdfmark}[3]{}
\documentclass[convert={convertexe={convert}}]{standalone}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\graphicspath{{/home/user/Downloads/}}

\begin{document}

Footer: \end{document}

Card Back:

[latex]
\includegraphics[width=0.5\textwidth]{duck.png}\\\relax 
[/latex]

It's crucial to modify the LaTeX build process (using the Edit LaTeX build process addon): Under Tools -> Add-ons -> Edit LaTeX build process -> config, replace the default "pngCommands" section by

    "pngCommands": [
        [
            "pdflatex",
            "-interaction=nonstopmode",
            "--shell-escape",
            "tmp.tex"
        ]
    ]

Now compilation should work. (I am using a Linux system, ymmv on Windows or macOS)

jro
  • 111