LaTeX 插入代码

LaTeX 插入代码可以使用的宏包有 verbatim、fancyvrb、listings 以及 minted。个人最推荐使用 minted。

verbatim

verbatim 没有语法高亮功能,只是显示一个等宽字体的输出。查看 Overleaf 示例

% Preamble
\usepackage{verbatim}
% Body
\begin{verbatim}
Text enclosed inside \texttt{verbatim} environment 
is printed directly
and all \LaTeX{} commands are ignored.
\end{verbatim}

fancyvrb

fancyvrb 是 verbatim 的增强版,增加了显示行号和代码块边框的功能。

% Preamble
\usepackage{fancyvrb}
\usepackage{xcolor}  % 用到了 \color 命令
% Body
% 设置在代码块左部显示行号,用方框包围代码块,代码块显示为红色
\begin{Verbatim}[numbers=left, frame=single, formatcom=\color{black}]
#include <iostream>

int main() {
  std::cout << "Hello, world!" << std::endl;
  return 0;
}
\end{Verbatim}

listings

listings 则更为强大,带有语法高亮、自定义风格等功能。查看 Overleaf 示例

% Preamble
\usepackage{xcolor}  % 为了渲染颜色,需要使用 xcolor 包
\usepackage{listings}  % 渲染代码块
% Body

% 定义颜色
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

% 定义 listings 风格,可以定义多个
\lstdefinestyle{mystyle}{
    % backgroundcolor=\color{backcolour},  % 背景色
    commentstyle= \color{red!50!green!50!blue!50},  % 注释的颜色
    keywordstyle= \color{blue!70},  % 关键字/程序语言中的保留字颜色
    numberstyle=\tiny\color{codegray},  % 左侧行号显示的颜色
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,  % 对过长的代码自动换行
    captionpos=b,
    keepspaces=true,
    numbers=left,  % 在左侧显示行号
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,  % 不显示字符串中的空格
    showtabs=false,
    tabsize=2,
    frame=single  % [none | single | shadowbox] 显示边框
}

\lstset{style=mystyle}  % 使用 listings 风格

\begin{lstlisting}[language=Python]
def main():
    print("Hello, world!")
    return 0
\end{lstlisting}

参考:Code listing | Overleaf

minted(推荐)

minted 是最方便好用的一款代码格式化宏包,开箱即用,只需配置目标语言即可。不过它需要 Python Pygments 包的依赖,以及需要启用 shell-escape 选项。查看 Overleaf 示例

安装 Pygments 包。以下命令选其一。

sudo apt install install python3-pygments
pip install pygments

启用 shell-escape 选项:

echo "shell_escape = t" >> /usr/local/texlive/2024/texmf.cnf

# 或者,在每次编译的时候指定 -shell-escape 选项
latexmk -shell-escape main.tex

注意将 2024 改为你当前在使用的 TeX Live 版本

如果你的编译套件不是 TeX Live,那么开启 shell-escape 选项的方法可以参考 How can I enable shell-escape? | StackExchange

% Preamble
\usepackage[outputdir=build]{minted}
% Body
\usemintedstyle{vs}  % 切换风格

\begin{minted}{python}
def main():
    print("Hello, world!")
    return 0
\end{minted}

如果你在编译时指定了特定的输出目录,才需要在引入 minted 包时提供 outputdir 选项。这里指定的输出目录是 build

参考:Code Highlighting with minted | Overleaf

posted @ 2024-04-24 14:27  Undefined443  阅读(413)  评论(0编辑  收藏  举报