Math and Symbols

MathJax

MathJax在新窗口打开 是一个基于浏览器的现代排版系统,适用于数学和化学方程式。它不需要安装任何额外的软件,因此易于使用,建议大多数用户使用。

MathJax is a modern, browser-based typesetting system, useful for mathematical and chemical equations. It does not require the installation of any extra software, so it is easy to use, and it is recommended for most users.

MathJax 在 Anki 2.1+、AnkiMobile 和 AnkiDroid 2.9+上得到开箱即用的支持。

MathJax is supported out of the box on Anki 2.1+, AnkiMobile, and AnkiDroid 2.9+.

尝试一下:

To try it out:

  1. 在字段中键入以下内容:

    Type the following in a field:

\sqrt{x}
  1. 选择你刚刚键入的文本。

    Select the text you just typed.

  2. 单击编辑器中最右侧的按钮,然后从菜单中选择“MathJax 行内公示”。Anki 将更改文本,使其显示为:

    Click the rightmost button in the editor, and choose "MathJax inline" from the menu. Anki will change the text so it reads:

\(\sqrt{x}\)
  1. 如果单击“卡片…”按钮,你将看到查看卡片时公式的预览。

    If you click the Cards… button, you’ll see a preview of how the equation will appear when the card is reviewed.

Anki 的 MathJax 支持需要 TeX 格式的内容。如果你不熟悉 TeX 格式,请参阅此备忘单在新窗口打开。请注意,第 2 点不适用于 Anki-Anki 对内联方程式使用\(\),对显示方程式使用\[\]

Anki’s MathJax support expects content in TeX format. If you’re not familiar with TeX formatting, please see this cheatsheet. Please note that point 2 does not apply in Anki - Anki uses \( and \) for inline equations, and \[ and \] for display equations.

如果你想在 MathJax 表达式中使用换行符,请使用 Shift+Enter 而不是仅使用 Enter,因为普通换行符会阻止 MathJax 正常工作。

If you want to use newlines in a MathJax expression, please use Shift+Enter instead of just Enter, as a normal newline will prevent MathJax from working correctly.

Anki 包括对 mhchem 渲染化学方程式的内置支持。有关更多信息,请参阅“化学方程式”部分和以下部分:https://mhchem.github.io/MathJax-mhchem/在新窗口打开

Anki includes built in support for mhchem for rendering chemical equations. Please see the 'chemical equations' section and the following sections for more information: https://mhchem.github.io/MathJax-mhchem/

https://faqs.ankiweb.net/customizing-mathjax.html在新窗口打开

LaTeX

LaTeX 是一个强大的排版系统,可用于输入数学公式、化学公式、乐谱等。Anki 为 LaTeX 提供了一些支持,允许你在笔记中输入 LaTeX 代码。当你查看卡片时,Anki 会调用 LaTeX 并显示生成的图像。

LaTeX is a powerful typesetting system, useful for entering mathematical formulas, chemical formulas, musical notation and so on. Anki provides some support for LaTeX, allowing you to enter LaTeX code in your notes. When you review a card, Anki will call LaTeX and display the generated image instead.

LaTeX 还有更多的工作要做,图像只能用 Anki 的计算机版本生成——尽管一旦生成,图像就可以由移动客户端显示。由于 LaTeX 带来的额外复杂性,它只推荐给需要比 MathJax 提供的更多功能的用户。

LaTeX is more work to set up, and images can only be generated with the computer version of Anki - though once generated, the images can be displayed by mobile clients. Because of the extra complexity LaTeX brings, it is only recommended for users that need more features than MathJax provides.

假定已经了解

Assumed Knowledge

Anki 的 LaTeX 支持并不是关键:假设你已经知道如何使用 LaTeX,并且已经安装了它。如果你没有使用 LaTeX 的经验,请参阅互联网上的众多指南之一。如果你在标记方面遇到问题,请在 LaTeX 论坛上询问。

Anki’s LaTeX support is not turn-key: it is assumed that you know how to use LaTeX already, and that you have it installed. If you have no experience with LaTeX, please consult one of the many guides available on the internet. If you are having trouble with markup, please ask on a LaTeX forum.

要安装 LaTeX,请在 Windows 上使用 MiKTeX;在 macOS 上使用 MacTeX,在 Linux 上使用发行版的软件包管理器。Dvipng 也必须安装。

To install LaTeX, on Windows use MiKTeX; on macOS use MacTeX, and on Linux use your distro’s package manager. Dvipng must also be installed.

在 Windows 上,转到 MikTeX 维护窗口中的“设置”,并确保“即时安装丢失的软件包”设置为“始终”,而不是“先问我”。如果你仍然有困难,一位用户报告说,以管理员身份运行 Anki 直到所有包都被提取出来是有帮助的。

On Windows, go to Settings in MikTeX’s maintenance window, and make sure "Install missing packages on the fly" is set to "Always", not to "Ask me first". If you continue to have difficulties, one user reported that running Anki as an administrator until all the packages were fetched helped.

在 macOS 上,LaTeX 只在 MacTeX 和 BasicTeX 上进行了测试。如果使用 BasicTeX,则需要使用以下命令单独安装 dvipng:

On macOS, LaTeX has only been tested with MacTeX and BasicTeX. If you use BasicTeX, you need to install dvipng separately, with the following command:

sudo tlmgr update --self; sudo tlmgr install dvipng

该命令可能不在路径上,因此你可能需要提供完整的路径,例如 /usr/local/texlive/2014basic/bin/x86_64-darwin/tlmgr。

The command may not be on the path, so you may need to provide the full path, e.g /usr/local/texlive/2014basic/bin/x86_64-darwin/tlmgr.

如果你没有使用上述 LaTeX 软件包,则需要使用 edit LaTeX在新窗口打开 插件来指定 LaTeX 和 dvipng 的完整路径。

If you are not using the above LaTeX packages, you will need to use the edit LaTeX add-on to specify the full path to latex and dvipng.

Web/Mobile

当你查看一张带有 LaTeX 的卡片时,Anki 会为该 LaTeX 生成一张图像,并将该图像放在你收藏的媒体文件夹中以备将来使用。网络和移动客户端将显示这些图像(如果它们已经存在),但无法自行生成图像。

When you review a card with LaTeX on it, Anki will generate an image for that LaTeX and place the image in your collection’s media folder for future use. The web & mobile clients will display these images if they already exist, but can not generate the images on their own.

为了避免在其他客户端学习之前必须至少学习一次所有的卡片,Anki 可以为你批量生成图像。要生成所有图像,请转到“工具”>“检查媒体”。之后,同步应该将生成的媒体上传到 AnkiWeb 和其他客户端。

To avoid having to review all your cards at least once before you can study on the other clients, Anki can generate the images in bulk for you. To generate all the images, please go to Tools>Check Media. After that, syncing should upload the generated media to AnkiWeb and the other clients.

例子

Example

输入 LaTeX 内容的最常见方法是用[LaTeX][/LaTeX]标签将其包围。编辑器部分提供了一个快捷按钮。

The most general way to input LaTeX content is to surround it with [latex][/latex] tags. There’s a shortcut button for this documented in the editor section.

[latex]标签必须在字段内使用-将它们放在卡片模板中会导致问题。

[latex] tags must be used inside a field - placing them in the card template will cause problems.

例如,在 Anki 卡片的正面输入以下内容:

For example, entering the following on the front of an Anki flashcard:

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

将在查看卡片时产生以下内容:

will produce this when the flashcard is viewed:

latex-0

上例中的公式被称为“文本公式”,因为它显示在非数学文本中。相比之下,以下示例显示了“显示的公式”:

The formula in the example above is called a 'text formula', because it is displayed right within the non-mathematical text. In contrast, the following example shows a 'displayed formula':

Does the sum below converge?

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

latex-1

“文本公式”和“显示公式”是最常见的 LaTeX 表达式类型,因此 Anki 提供了它们的缩写版本。形式表达:

'Text formulas' and 'display formulas' are the most common type of LaTeX expressions, so Anki provides abbreviated versions of them. Expressions of the form:

[latex]\begin{math}...\end{math}[/latex]

可以缩短为

can be shortened to

[$]...[/$]
[latex]\begin{displaymath}...\end{displaymath}[/latex]

可以缩短为

and expressions of the form

[$$]...[/$$]

例如,前面显示的两个 LaTeX 片段等效于

For example, the two LaTeX snippets shown before are equivalent to

Does [$]\sum_{k = 1}^{\infty}\frac{1}{k}[/$] converge?

Does the sum below converge?

[$$]\sum_{k = 1}^{\infty}\frac{1}{k}[/$$]

Packages

Anki 允许你自定义 LaTeX 前导码,这样你就可以导入化学、音乐等的自定义包。例如,假设你在互联网上找到 chemtex 的示例文件:

Anki allows you to customize the LaTeX preamble so you can import custom packages for chemistry, music and so on. For example, imagine you find an example file for chemtex on the internet:

\documentclass[a4paper,12pt]{report}
\usepackage{chemtex}
\begin{document}

\initial
\begin{figure}[h]\centering
\parbox{.3\textwidth}{\ethene{H}{H$_3$C}{CH$_3$}{Br}}
\hfil
\parbox{.3\textwidth}{\cbranch{H}{S}{H}{S}{C}{S}{}{S}{H}
  \xi=-200 \cright{}{Q}{C}{D}{O}{S}{OH}}
\hfil
\parbox{.3\textwidth}{\hetisix{Q}{Q}{Q}{Q}{Q}{Q}{O}{Q}{O}
  \xi=-171 \fuseup{Q}{Q}{Q}{Q}{D}{Q}{D}{Q}{D}}
\caption{Chemie mit {\tt CHEMTEX}\label{a1}}
\end{figure}

\end{document}

首先,按照软件包和 MiKTeX/MacTeX 的文档来安装软件包。要检查包是否正常工作,你需要将上面的代码放入一个.latex 文件中,并测试是否可以从命令行编译它。一旦你确认该软件包可用且有效,我们就可以将其与 Anki 集成。

Firstly, follow the documentation of the package and MiKTeX/MacTeX in order to install the package. To check the package is working, you’ll want to put code like the above into a .latex file and test if you can compile it from the command line. Once you’ve confirmed that the package is available and working, we can integrate it with Anki.

要将包与 Anki 一起使用,请单击主窗口中的“添加”,然后单击模板选择按钮。单击“管理”按钮,然后选择你计划使用的笔记模板,然后单击“选项”。显示了 LaTeX 页眉和页脚。标题将类似于:

To use the package with Anki, click "Add" in the main window, then click the note type selection button. Click the "Manage" button, then select the note type you plan to use and click "Options". The LaTeX header and footer are shown. The header will look something like:

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

要使用 chemtex,你需要在前面的示例中添加 usepackage 行,因此它看起来像:

To use chemtex, you’d add the usepackage line in the earlier example, so it looks like:

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

在那之后,你应该能够在你的 Anki 卡中包括如下行:

After that, you should be able to include lines like the following in your Anki cards:

[latex]\ethene{H}{H$_3$C}{CH$_3$}{Br}[/latex]

模板冲突

Template Conflicts

从 Anki 2.1.20/AnkiMobile 2.0.56/AnkiDroid 2.13 起,不再需要此解决方法,因为字段中的{{field}}文本不再会导致问题。如果你需要支持旧版本并希望继续使用此语法,请确保将{{=<% %>=}}字符串放在正面和背面模板的最顶部,因为最新的 Anki 版本除了开头之外不会识别它。

As of Anki 2.1.20 / AnkiMobile 2.0.56 / AnkiDroid 2.13, this workaround is no longer required, as {{field}} text inside fields no longer causes problems. If you need to support older versions and want to keep using this syntax, please make sure you place the {{=<% %>=}} string at the very top of your front and back template, as recent Anki versions will not recognize it anywhere but the start.

对于旧版本:

For older versions:

在编写数学方程时,{{}}在 LaTeX 代码出现并不罕见。为了确保你的 LaTeX 方程不会与 Anki 的字段替换冲突,可以将分隔符更改为其他内容。

It’s not uncommon for {{ and }} to pop up in LaTeX code when writing mathematical equations. To ensure that your LaTeX equations don’t conflict with Anki’s field replacements, it’s possible to change the separator to something else.

例如,如果你有一个模板:

For example, if you have a template:

{{latex field}}

将其更改为以下内容将使 LaTeX 不太可能发生冲突:

Changing it to the following will make it unlikely that the LaTeX will conflict:

{{=<% %>=}}
<%latex field%>

挖空冲突

Cloze Conflicts

挖空以}}终止,这可能与 LaTeX 中出现的}}相冲突。为了防止 LaTeX 被解释为挖空标记,你可以在任何不表示挖空结束的双大括号之间放一个空格,这样

Cloze deletions are terminated with }}, which can conflict with a }} appearing in your LaTeX. To prevent LaTeX from being interpreted as a closing cloze marker, you can put a space between any double closing braces that do not indicate the end of the cloze, so

{{c1::[$]\frac{foo}{\frac{bar}{baz}}[/$] blah blah blah.}}

不会工作,但是:

will not work, but

{{c1::[$]\frac{foo}{\frac{bar}{baz} }[/$] blah blah blah.}}

(并且 LaTeX 在数学模式中忽略空格,因此你的方程式将呈现相同的效果)。如果你想避免在渲染的文本中添加额外的空间(例如,当你制作用于学习编程语言的 Cloze 卡时),另一种选择是在 HTML 模式下编辑卡时使用 HTML 注释:

will (and LaTeX ignores spaces in math mode, so your equation will render the same). If you want to avoid adding the extra space into the rendered text (for example, when you are making Cloze cards for learning programming languages), another option is to use a HTML comment when editing the card in HTML mode:

{{c1::[$]\frac{foo}{\frac{bar}{baz}<!-- -->}[/$] blah blah blah.}}

如果需要在 Cloze 卡的文本中使用::字符序列,则可以使用其中一种解决方法。为以下笔记生成的第一张卡片将显示 [type] in C++ is a type-safe union

You may use either workaround if you need to use the :: character sequence within the Cloze-deleted text. The first card generated for the following note text will read [type] in C++ is a type-safe union:

{{c1::std:<!-- -->:variant::~type~}} in C++ is a {{c2::type-safe union}}

不安全的命令

Unsafe Commands

Anki 禁止在卡或模板中使用某些命令,如 \input\def,因为允许它们可能会导致恶意共享牌组损坏你的系统。(为了安全起见,这些命令甚至在笔记中都是被禁止的,所以如果你收到这个错误,但认为你没有使用过,请仔细检查你在页眉、模板和卡片中的任何笔记。)如果你需要使用这些命令,请将它们添加到系统包中,并按照上一节中的描述导入该包。

Anki prohibits certain commands like \input or \def from being used on cards or in templates, because allowing them could allow malicious shared decks to damage your system. (To be on the safe side, these commands are prohibited even in comments, so if you’re getting this error but don’t think you’ve used one, please double-check any comments you have in your headers, templates, and cards.) If you need to use these commands, please add them to a system package and import that package as described in the previous section.