在 Anki 中使用 minted 和 Pygments 生成代码片段时出现 Python 错误

在 Anki 中使用 minted 和 Pygments 生成代码片段时出现 Python 错误

在命令行上一切正常,但在 Anki 中我收到一个 python 错误。

我的电脑:

MacBook Air (13-inch, Mid 2012)
Processor 2 GHz Intel Core i7
Memory 8 GB 1600 MHz DDR3

我的操作系统:

OS X El Capitan
Version 10.11 (15A284)

我的 Python 库

~ % which python
/usr/bin/python

我的python版本:

~ % python -V
Python 2.7.10

这是我的 Edit_LaTeX_build_process.py 文件(笔记: -shell-escape):

newLaTeX = \
[
    ["/Library/TeX/texbin/latex", "-shell-escape", "-interaction=nonstopmode", "tmp.tex"],
    ["/Library/TeX/texbin/dvipng", "-D", "200", "-T", "tight", "tmp.dvi", "-o", "tmp.png"]
]

# make the changes
import anki.latex
anki.latex.latexCmds = newLaTeX

这些是我的卡选项(笔记: \usepackage{minted}):

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

命令行测试成功:

测试.tex

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

    #include
    using namespace std;
    int main()
    {
        return 0;
    }

\end{minted}
\end{document}

Shell 命令:

~ % latex -shell-escape test.tex

Anki 中的测试失败:

[latex]
\begin{minted}{cpp}

#include
using namespace std;
int main()
{
    return 0;
}

\end{minted}
[/latex]

错误输出:

Error executing /Library/TeX/texbin/latex.
Generated file:   /var/folders/_q/53_csl094rqfn9rr18bn3bpw0000gn/T/anki_temp/tmp.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015)  (preloaded format=latex)
\write18 enabled.
entering extended mode
(./tmp.tex
LaTeX2e <2015/01/01>
Babel <3.9l> and hyphenation patterns for 21 languages loaded.
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/base/size12.clo))
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/base/inputenc.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/base/utf8.def
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/base/t1enc.dfu)
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/base/ot1enc.dfu)
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/base/omsenc.dfu)))
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/amsfonts/amssymb.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/amsfonts/amsfonts.sty))
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/amsmath/amsopn.sty))
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/minted/minted.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/oberdiek/kvoptions.sty
(/usr/local/texlive/2015basic/texmf-   dist/tex/generic/oberdiek/ltxcmds.sty)
(/usr/local/texlive/2015basic/texmf-  dist/tex/generic/oberdiek/kvsetkeys.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/generic/oberdiek/infwarerr.sty)
(/usr/local/texlive/2015basic/texmf-dist/tex/generic/oberdiek/etexcmds.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/generic/oberdiek/ifluatex.sty))))
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty
Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix 
<2008/02/07> (tvz))
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/float/float.sty)
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/base/ifthen.sty)
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/tools/calc.sty)
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/ifplatform/ifplatform.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/generic/oberdiek/pdftexcmds.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/generic/oberdiek/ifpdf.sty))
(/usr/local/texlive/2015basic/texmf-dist/tex/generic/oberdiek/catchfile.sty)
(./tmp.w18))
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/etoolbox/etoolbox.sty)
(/usr/local/texlive/2015basic/texmf-dist/tex/generic/xstring/xstring.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/generic/xstring/xstring.tex))
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/lineno/lineno.sty))
(./_minted-tmp/default.pygstyle)/usr/local/bin/pygmentize
(./tmp.aux)
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/xcolor/xcolor.sty
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/latexconfig/color.cfg)
(/usr/local/texlive/2015basic/texmf-dist/tex/latex/graphics/dvips.def))
(./_minted-tmp/default-pyg-prefix.pygstyle)Traceback (most recent call   last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages/py2app/apptemplate/lib/site.py", line 22, in <module>
zipimport.ZipImportError: can't decompress data; zlib not available

! Package minted Error: Missing Pygments output; \inputminted was
probably given a file that does not exist--otherwise, you may need 
the outputdir package option, or may be using an incompatible build   tool.

See the minted package documentation for explanation.
Type  H <return>  for immediate help.
...                                              

l.22 \end{minted}

(./tmp.aux) )
(see the transcript file for additional information)
No pages of output.
Transcript written on tmp.log.

任何帮助使其正常工作的帮助都非常感谢,因为在尝试让它工作了一天多之后,我已经没有主意了。

答案1

我认为问题在于包pygmentize调用的minted是 python 脚本。从 Anki.app 内部调用时,pygmentize使用 Anki 内部的 python 环境,这不是完整的 python 发行版。

无论如何,我通过使用 PyInstaller 将 pygmentize 制作成独立的可执行文件来解决这个问题:

$ sudo pip install pyinstaller

不幸的是,当前版本的 pyinstaller 在 El Capitan 上存在问题(问题追踪器) 但修复起来相对容易。使用文本编辑器替换以下文件中shutil.copy2的所有实例:shutil.copy

/Library/Python/2.7/site-packages/PyInstaller/building/api.py
/Library/Python/2.7/site-packages/PyInstaller/building/osx.py
/Library/Python/2.7/site-packages/PyInstaller/building/utils.py
/Library/Python/2.7/site-packages/PyInstaller/lib/macholib/util.py

现在您可以构建独立的pygmentize可执行文件并将其移动到位:

$ cd ~
$ pyinstaller --onefile /usr/local/bin/pygmentize
$ sudo mv /usr/local/bin/pygmentize /usr/local/bin/pygmentize~
$ sudo cp dist/pygmentize /usr/local/bin/pygmentize

这对我有用。希望这能有所帮助!

斯蒂芬

相关内容