如何使用 LaTeX 将图像导入 Anki?

如何使用 LaTeX 将图像导入 Anki?

几天来,我试图寻找我的问题的答案,但没有成功。

在另一个问题,它帮助我在 Anki 中使用“\ tikz”。在其他地方感谢@gernot。

我想使用 LaTeX 语言在 Anki 卡片中包含一张图片。

我知道有方法可以将图像包含在我用 Google 找到的 Anki 卡片中,但它不使用 LaTeX 来插入。

在我之前的问题,有人给了我一些建议,但不知为何,这些建议并没有起到作用。谢谢你的帮助。

我把文件放在 td.png 中

/usr/local/texlive/2016/texmf-dist/tex/latex/mwe/

Error executing pdflatex.
Generated file: /var/folders/v1/psh6j3c139x1wkj2k38_77v40000gn/T/anki_temp/tmp.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./tmp.tex
LaTeX2e <2016/03/31>
Babel <3.9r> and hyphenation patterns for 83 language(s) loaded.
(/usr/local/texlive/2016/texmf-dist/tex/latex/standalone/standalone.cls
Document Class: standalone 2015/07/15 v1.2 Class to compile TeX sub-files stand
alone
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/ifluatex.sty)
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/local/texlive/2016/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/xkeyval/xkeyval.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/xkeyval/xkeyval.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/xkeyval/xkvutils.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/xkeyval/keyval.tex))))
(/usr/local/texlive/2016/texmf-dist/tex/latex/standalone/standalone.cfg)
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/size12.clo))
(/usr/local/texlive/2016/texmf-dist/tex/latex/varwidth/varwidth.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/inputenc.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/utf8.def
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/t1enc.dfu)
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/ot1enc.dfu)
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/omsenc.dfu)))
(/usr/local/texlive/2016/texmf-dist/tex/latex/amsfonts/amssymb.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/amsfonts/amsfonts.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/local/texlive/2016/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/amsmath/amsopn.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/xcolor/xcolor.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics-cfg/color.cfg)
(/usr/local/texlive/2016/texmf-dist/tex/latex/pdftex-def/pdftex.def
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/infwarerr.sty)
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/ltxcmds.sty)))
(/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/basiclayer/pgf.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/utilities/pgfrcs.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/utilities/pgfutil-common.te
x
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/utilities/pgfutil-common-li
sts.tex))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/utilities/pgfutil-latex.def
(/usr/local/texlive/2016/texmf-dist/tex/latex/ms/everyshi.sty))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/utilities/pgfrcs.code.tex))
(/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/basiclayer/pgfcore.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/graphicx.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/graphics.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/trig.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)))
(/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/systemlayer/pgfsys.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/systemlayer/pgfsys.code.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/utilities/pgfkeys.code.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/utilities/pgfkeysfiltered.c
ode.tex))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/systemlayer/pgf.cfg)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/systemlayer/pgfsys-pdftex.d
ef
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/systemlayer/pgfsys-common-p
df.def)))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/systemlayer/pgfsyssoftpath.
code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/systemlayer/pgfsysprotocol.
code.tex))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcore.code.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmath.code.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathcalc.code.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathutil.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathparser.code.tex
)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.code.
tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.basic
.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.trigo
nometric.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.rando
m.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.compa
rison.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.base.
code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.round
.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.misc.
code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.integ
erarithmetics.code.tex)))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmathfloat.code.tex)
)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepoints.co
de.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepathconst
ruct.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepathusage
.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcorescopes.co
de.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcoregraphicst
ate.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcoretransform
ations.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcorequick.cod
e.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcoreobjects.c
ode.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepathproce
ssing.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcorearrows.co
de.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcoreshade.cod
e.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcoreimage.cod
e.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcoreexternal.
code.tex))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcorelayers.co
de.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcoretranspare
ncy.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/basiclayer/pgfcorepatterns.
code.tex)))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/modules/pgfmoduleshapes.cod
e.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/modules/pgfmoduleplot.code.
tex)
(/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/compatibility/pgfcomp-version
-0-65.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/compatibility/pgfcomp-version
-1-18.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/frontendlayer/tikz.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/utilities/pgffor.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/utilities/pgfkeys.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/utilities/pgfkeys.code.tex)
) (/usr/local/texlive/2016/texmf-dist/tex/latex/pgf/math/pgfmath.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmath.code.tex))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/utilities/pgffor.code.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/math/pgfmath.code.tex)))
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/frontendlayer/tikz/tikz.cod
e.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/libraries/pgflibraryplothan
dlers.code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/modules/pgfmodulematrix.cod
e.tex)
(/usr/local/texlive/2016/texmf-dist/tex/generic/pgf/frontendlayer/tikz/librarie
s/tikzlibrarytopaths.code.tex)))
(/usr/local/texlive/2016/texmf-dist/tex/latex/mhchem/mhchem.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3kernel/l3pdfmode.def))
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3packages/l3keys2e/l3keys2e.sty)
 (/usr/local/texlive/2016/texmf-dist/tex/latex/l3experimental/l3str/l3regex.sty

(/usr/local/texlive/2016/texmf-dist/tex/latex/l3experimental/l3str/l3tl-build.s
ty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3experimental/l3str/l3tl-analysi
s.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3experimental/l3str/l3flag.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3experimental/l3str/l3str-conver
t.sty)) (/usr/local/texlive/2016/texmf-dist/tex/latex/tools/calc.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/chemgreek/chemgreek.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3packages/xparse/xparse.sty)))
(/usr/local/texlive/2016/texmf-dist/tex/latex/easylist/easylist.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/fontenc.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/SIstyle/sistyle.sty) (./tmp.aux)
(/usr/local/texlive/2016/texmf-dist/tex/context/base/mkii/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) ABD: EveryShipout initializing macros
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/pdftexcmds.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/oberdiek/epstopdf-base.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/oberdiek/grfext.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/kvdefinekeys.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/oberdiek/kvoptions.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/kvsetkeys.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/etexcmds.sty)))
(/usr/local/texlive/2016/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
(/usr/local/texlive/2016/texmf-dist/tex/latex/amsfonts/umsa.fd)
(/usr/local/texlive/2016/texmf-dist/tex/latex/amsfonts/umsb.fd)

! LaTeX Error: File `td' not found.

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

l.18 \includegraphics{td}

[1{/usr/local/texlive/2016/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./tmp.aux) )
(see the transcript file for additional information)</usr/local/texlive/2016/te
xmf-dist/fonts/type1/public/amsfonts/cm/cmr12.pfb>
Output written on tmp.pdf (1 page, 8634 bytes).
Transcript written on tmp.log.

编辑 LaTeX 构建过程:

newLaTeX = \
[
    ["pdflatex", "-interaction=nonstopmode", "tmp.tex"],
    ["sips", "-s", "format", "png", "tmp.pdf", "--out", "tmp.png"]
]
import anki.latex
anki.latex.latexCmds = newLaTeX

选项 LaTeX Anki 卡。

标头:

\documentclass[12pt,border=1mm,varwidth]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amssymb, amsmath, amsfonts}
\usepackage{xcolor}
\usepackage{pgf, tikz}
\usepackage[version=4]{mhchem}
\usepackage[ampersand]{easylist}
\usepackage{fontenc}
\usepackage{graphicx}
\usepackage{sistyle}

\newcommand\tab[1][1cm]{\hspace*{#1}}

\begin{document}
\begin{varwidth}{100em}

页脚 :

\end{varwidth}
\end{document}

卡牌设定。

前台模板:

[latex]{{cloze:Texte}}[/latex]

<div style='font-family: Arial; font-size: 20px;'>{{Enoncer}}</div>

返回模板:

[latex]{{cloze:Texte}}[/latex]
{{Extra}}

造型:

.card {
 font-family: arial;
 font-size: 20px;
 text-align: center;
 color: black;
 background-color: white;
}

.cloze {
 font-weight: bold;
 color: blue;
}

@tym007 是的,这是可能的。如果其他包(如 pgfplots)尚未加载 \usepackage{graphicx},请将其添加到前言中。然后 [latex]\includegraphics{myfile.png}[/latex] 应该会加载图像。不过,有些事情可能会出错。如果失败,请检查 tmp.tex 和 tmp.log。例如,如果图像太宽,请使用 \includegraphics[width=3cm]{...}。如果文件名或路径包含特殊字符,您可能需要 \string 或 \detokenize,请参阅如何在 includepdf 文件名中使用波浪号或空格?。– gernot 10 月 10 日 20:34

@tym007 作为第一个测试,使用 \includegraphics{example-image-a}。如果您有当前的 TeXLive,则此图像位于搜索路径上。如果此方法有效,您就会知道您自己的图像出现任何故障都是由于“未找到图像”、“图像太宽”或“文件名中有奇怪的字符”造成的。– gernot 10 月 10 日 20:38

@tym007 如果图片太大,那么带有 article 类的序言将生成一个包含两页的 pdf,并且转换为 png 会失败(使用 [width=...] 解决)。带有 standalone 的序言只会剪切图片,但不会失败。– gernot 10 月 10 日 20:44

事实上。对于“\ usepackage {graphicx}”的运行,我们必须将图像放在与 .tex 相同的文件中。但在 Anki 上,我不知道将它放在哪里才能与此功能一起使用。有什么想法吗? – tym007 10 月 11 日 15:27

@tym007 要么给出完整路径,如 \includegraphics{/usr/local/texlive/2016/texmf-dist/tex/late‌​x/mwe/example-image-‌​a.jpg},要么设置 \graphicspath,要么设置 TEXINPUTS 变量以包含包含图像的目录。如果文件名或路径包含空格或特殊字符,请参阅例如如何包含路径中带有空格的图形?的答案。有关 \graphicspath 的信息,请参阅 WikiBook 上的 LaTeX/Importing Graphics。– gernot 10 月 11 日 16:06

我尝试使用此代码,但没有成功。'\includegraphics{/usr/local/texlive/2016/texmf-dist/tex/lat‌​‌​e‌​x/mwe/td.jpg}' – tym007 2 天前

@tym007 tmp.tex 是什么样子的?tmp.log 说了什么?当你自己编译 tmp.tex 时会发生什么?图像 /usr/local/texlive/2016/texmf-dist/tex/lat‌​‌​ex/mwe/td.jpg 真的存在于你的系统上吗?它不在我的系统上......如果你的图像是 td.jpg,你必须使用此文件的路径而不是 /usr/local/texlive/2016/texmf-dist... – gernot 2 天前

答案1

Anki 使用一些临时目录进行编译,因此您必须明确提供图像的路径。假设目录为/home/gernot/anki,其中包含图像。通过以下方式duck.jpg在 Anki 中设置序言:Tools | Manage Note Types | your cloze note type | Options

\documentclass[12pt,border=1mm,varwidth]{standalone}
\usepackage{graphicx}
\graphicspath{{/home/gernot/anki/}}
\begin{document}

在字段中添加一张卡片,your cloze note type内容如下Text

[latex]\begin{center}\includegraphics{duck}\\\relax {{c1::duck}}\end{center}[/latex] 

然后 Anki 将生成两张卡片

在此处输入图片描述在此处输入图片描述

需要注意以下几点:

  • 中的路径\graphicspath必须以斜线结尾/

  • 请记住,像 这样的字符串在一张卡片上将{{c1::XXX}}被替换为,而在另一张卡片上将被替换为。您必须确保您的代码在两种情况下都是正确的。在上面的例子中,写入会出错,因为被解释为带有可选参数。写入仍然会出错,因为字符串在第二张卡片上变成未定义的命令。因此必须在两者之间放置一个空格、行尾或中间部分。[...]XXX\\{{c1::duck}}\\[...]\\\\\relax{{c1::duck}}\\\relaxduck\relaxduck{}

  • 如果 LaTeX 代码恰好包含双括号{{,则可能会被误认为是 Anki 符号。请将其替换为{%+new line+ {,或者如果从 LaTeX 的角度来看可以接受,则替换为{ {

  • 如果图像的路径或文件名包含特殊字符(空格、波浪号、点),则可能需要采取额外的预防措施。请参阅如何使用 \graphicspath? \includegraphics:文件名中的点;但请记住最后一点,避免使用双括号。

(鸭子图片来自维基百科

相关内容