$\LaTeX$ Syntax

以下のセクションでは、$\LaTeX$を使用して書かれた数式をドキュメントに追加する方法について説明します。

Escaping Characters in Docstrings

Since some characters used in $\LaTeX$ syntax, such as $ and \, are treated differently in docstrings. They need to be escaped using a \ character as in the following example:

"""
Here's some inline maths: ``\\sqrt[n]{1 + x + x^2 + \\ldots}``.

Here's an equation:

``\\frac{n!}{k!(n - k)!} = \\binom{n}{k}``

This is the binomial coefficient.
"""
func(x) = # ...

マニュアルページ(.mdファイル)の方程式については、エスケープは必要ありません。したがって、マニュアルとドキュメント文字列の間で方程式を移動する際には、エスケープ文字 \ を適切に追加または削除する必要があります。

ドキュメント文字列内の特殊文字をエスケープする必要がないように、raw""文字列マクロを使用し、@docと組み合わせることができます:

@doc raw"""
Here's some inline maths: ``\sqrt[n]{1 + x + x^2 + \ldots}``.

Here's an equation:

``\frac{n!}{k!(n - k)!} = \binom{n}{k}``

This is the binomial coefficient.
"""
func(x) = # ...

関連する問題は、ドキュメント文字列にドル記号を追加する方法です。ドル記号は次のように二重エスケープする必要があります:

"""
The cost was \\\$1.
"""

Inline Equations

Here's some inline maths: ``\sqrt[n]{1 + x + x^2 + \ldots}``.

どのように表示されるか


こちらはいくつかのインライン数学です:$\sqrt[n]{1 + x + x^2 + \ldots}$


Warning

LaTeXと同様に、インラインおよびディスプレイ数式をエスケープするために$および$$を使用することも可能です。ただし、これは非推奨であり、この機能は将来のリリースで削除される可能性があります。

Display Equations

Here's an equation:

```math
\frac{n!}{k!(n - k)!} = \binom{n}{k}
```

This is the binomial coefficient.

---

To write a system of equations, use the `aligned` environment:

```math
\begin{aligned}
\nabla\cdot\mathbf{E}  &= 4 \pi \rho \\
\nabla\cdot\mathbf{B}  &= 0 \\
\nabla\times\mathbf{E} &= - \frac{1}{c} \frac{\partial\mathbf{B}}{\partial t} \\
\nabla\times\mathbf{B} &= - \frac{1}{c} \left(4 \pi \mathbf{J} + \frac{\partial\mathbf{E}}{\partial t} \right)
\end{aligned}
```

These are Maxwell's equations.

どのように表示されるかは


ここに方程式があります:

\[\frac{n!}{k!(n - k)!} = \binom{n}{k}\]

これは二項係数です。


方程式の系を書くには、aligned 環境を使用します:

\[\begin{aligned} \nabla\cdot\mathbf{E} &= 4 \pi \rho \\ \nabla\cdot\mathbf{B} &= 0 \\ \nabla\times\mathbf{E} &= - \frac{1}{c} \frac{\partial\mathbf{B}}{\partial t} \\ \nabla\times\mathbf{B} &= - \frac{1}{c} \left(4 \pi \mathbf{J} + \frac{\partial\mathbf{E}}{\partial t} \right) \end{aligned}\]

これらはマクスウェルの方程式です。

Printing LaTeX from Julia

LaTeXをJuliaからきれいに表示するには、MIME"text/latex"タイプのためにBase.showをオーバーロードします。例えば:

struct LaTeXEquation
    content::String
end

function Base.show(io::IO, ::MIME"text/latex", x::LaTeXEquation)
    # Wrap in $$ for display math printing
    return print(io, "\$\$ " * x.content * " \$\$")
end

LaTeXEquation(raw"""
    \left[\begin{array}{c}
        x \\
        y
    \end{array}\right]
""")

\[ \left[\begin{array}{c} x \\ y \end{array}\right] \]

Set math engine and define macros for LaTeX

mathengine引数は、数学レンダリングエンジンを指定することを可能にし、MathJaxとKaTeXの両方をサポートしています(後者がデフォルトです)。

さらに、レンダリングエンジンにカスタム設定を渡すこともできます。例えば、グローバルなLaTeXコマンド定義を追加するには、mathengineを次のように設定できます:

mathengine = Documenter.MathJax(Dict(:TeX => Dict(
    :equationNumbers => Dict(:autoNumber => "AMS"),
    :Macros => Dict(
        :ket => ["|#1\\rangle", 1],
        :bra => ["\\langle#1|", 1],
    ),
)))

また、MathJax v3を使用すると、physics packageをロードできます:

mathengine = MathJax3(Dict(
    :loader => Dict("load" => ["[tex]/physics"]),
    :tex => Dict(
        "inlineMath" => [["\$","\$"], ["\\(","\\)"]],
        "tags" => "ams",
        "packages" => ["base", "ams", "autoload", "physics"],
    ),
))

構文はKaTeXを使用する場合、わずかに異なります。以下の例は、makedocs関数に含めることができるものです:

makedocs(
    format = Documenter.HTML(; mathengine=
        Documenter.KaTeX(
            Dict(:delimiters => [
                Dict(:left => raw"$",   :right => raw"$",   display => false),
                Dict(:left => raw"$$",  :right => raw"$$",  display => true),
                Dict(:left => raw"\[",  :right => raw"\]",  display => true),
                ],
                :macros => Dict("\\RR" => "\\mathbb{R}",
                    raw"\Xi" => raw"X_{i}",
                    raw"\Ru" => raw"R_{\mathrm{univ.}}",
                    raw"\Pstd" => raw"P_{\mathrm{std}}",
                    raw"\Tstd" => raw"T_{\mathrm{std}}",
                ),
            )
        )
    )
)

MathJax2MathJax3 および KaTeXmathengine の利用可能なタイプです。