> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# {Minted}

本文介绍如何使用 [`minted` 宏包](https://ctan.org/pkg/minted?lang=en)在 LaTeX 文档中格式化并高亮显示编程语言源代码。我们先来看一个示例：

```latex theme={null}
\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}{python}
import numpy as np
    
def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #to become the incidence matrix
    VT = np.zeros((n*m,1), int)  #dummy variable
    
    #compute the bitwise xor matrix
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1) 

    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;
                
                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)
                
                VT = np.zeros((n*m,1), int)
    
    return M
\end{minted}
\end{document}
```

该示例生成的输出如下：

<p style={{ textAlign: "center" }}>
  <img src="https://sharelatex-wiki-cdn-671420.c.cdn77.org/learn-scripts/images/f/f0/OLV2minted.png" alt="展示 minted 宏包输出效果的示例" />
</p>

这里有两个重要的命令。在导言区中，通过以下语句导入该宏包

```latex theme={null}
\usepackage{minted}
```

然后，标签 `\begin{minted}{python}` 和 `\end{minted}` 界定了一个环境，该环境以等宽字体逐字输出文本，并为注释、关键字和函数着色。参数 `python` 指定源代码所使用的编程语言。`minted` 支持 150 多种编程语言和标记语言以及配置文件，支持的语言列表请参见[参考指南](https://www.overleaf.com/learn/latex/Code_Highlighting_with_minted#Reference_guide)。

**注意**：要让 `minted` 在你的\_本地\_ LaTeX 发行版中正常工作，必须额外安装一个名为 [Pygments](https://pygments.org/) 的程序。[Overleaf](https://www.overleaf.com/) 可以省去你安装它以及运行特殊命令来编译文档的麻烦——在 Overleaf 上，使用 `minted` 的文档可以"开箱即用"。

### 基本用法

如下例所示，可以对 `minted` 环境进行配置，以修改排版代码的视觉呈现。这里，`minted` 环境使用了若干以逗号分隔、形如 `key=value` 的参数：

```latex theme={null}
\documentclass{article}
\usepackage{minted}
\usepackage{xcolor} % to access the named colour LightGray
\definecolor{LightGray}{gray}{0.9}
\begin{document}
\begin{minted}
[
frame=lines,
framesep=2mm,
baselinestretch=1.2,
bgcolor=LightGray,
fontsize=\footnotesize,
linenos
]
{python}
import numpy as np
    
def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #to become the incidence matrix
    VT = np.zeros((n*m,1), int)  #dummy variable
    
    #compute the bitwise xor matrix
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1) 

    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;
                
                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)
                
                VT = np.zeros((n*m,1), int)
    
    return M
\end{minted}
\end{document}
```

该示例生成的输出如下：

<p style={{ textAlign: "center" }}>
  <img src="https://sharelatex-wiki-cdn-671420.c.cdn77.org/learn-scripts/images/f/ff/OLV2minted2.png" alt="对 minted 宏包排版的代码应用格式设置的示例" />
</p>

本例中使用的参数如下：

* `frame=lines`：在代码的上方和下方各绘制一条线作为边框。其他可选值有 `leftline`、`topline`、`bottomline` 和 `single`。
* `framesep=2mm`：将边框间距设置为 2mm。也可以使用其他[长度单位](https://www.overleaf.com/learn/latex/Lengths_in_LaTeX)。
* `baselinestretch=1.2`：将代码的行距设置为 1.2。
* `bgcolor=LightGray`：将背景颜色设置为 `LightGray`。需要导入 `xcolor` 宏包才能生效。要了解更多颜色操作，请参阅[在 LaTeX 中使用颜色](https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX)。
* `fontsize=\footnotesize`：将字号设置为 `footnotesize`。也可以设置任何其他[字号](https://www.overleaf.com/learn/latex/Font_sizes_and_kinds%23Reference_guide)。
* `linenos`：启用行号。

其他可能有用的选项包括：

* `mathescape`：在代码注释中启用数学模式。
* `rulecolor`：更改边框的颜色。
* `showspaces`：使用特殊字符使空格可见。

### 从文件中引入代码

代码通常存储在源文件中，因此能够自动从文件导入代码的命令非常方便，如下例所示：

```latex theme={null}
\documentclass{article}
\usepackage{minted}
\title{Importing files using minted}
\begin{document}
The next code will be directly imported from a file:

\inputminted{octave}{BitXorMatrix.m}
\end{document}
```

该示例生成的输出如下：

<img src="https://sharelatex-wiki-cdn-671420.c.cdn77.org/learn-scripts/images/c/c8/OLV2mintedinput.png" alt="使用 minted 导入代码文件" height="392" width="600" />

命令 `\inputminted{octave}{BitXorMatrix.m}` 从文件 `BitXorMatrix.m` 导入代码，参数 `octave` 告诉 LaTeX 代码所使用的编程语言。该命令还可以接受两个额外参数，以便只导入文件的一部分；例如，要导入第 2 行到第 12 行的代码，命令如下：

```latex theme={null}
\inputminted[firstline=2, lastline=12]{octave}{BitXorMatrix.m}
```

### 单行代码

如果只需要输入一行代码，可以使用 `\mint` 命令，其语法如下例所示。

```latex wrap theme={null}
One-line code formatting also works with \texttt{minted}. For example, a small fragment of HTML like this:
\mint{html}|<h2>Something <b>here</b></h2>|
\noindent can be formatted correctly.
```

该示例生成的输出如下：

<img src="https://sharelatex-wiki-cdn-671420.c.cdn77.org/learn-scripts/images/d/d9/OLV2mintedHTML.png" alt="使用 minted 的单行代码示例" height="112" width="600" />

花括号中的参数设置编程语言（本例中为 `html` 标记语言），而实际要格式化的文本则由 '|' 字符界定。

### 自托管 Overleaf 中的问题

如果你使用沙盒编译并通过 Overleaf 服务器部署 Overleaf，minted 可能无法正常工作。详细信息请参见：[issues/131](https://github.com/yu-i-i/overleaf-cep/issues/131)。你需要启用 arm 指令配置文件以及部分系统调用，minted 才能正常工作。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.