LaTex插入 Python 程序代码块

  • 1. 需求描述和解决方案
  • 2. minted 包的安装与配置
    • 2.1 安装 Python 第三方库 Pygments
    • 2.2 下载和安装 minted 宏包
    • 2.3 配置 LaTeX 编译环境
  • 3. minted 包的使用
    • 3.1 基本使用
    • 3.2 扩展使用
      • 3.2.1 `\mint` 命令行
      • 3.2.2 `\mintinline` 行内使用
      • 3.2.3 `\inputminted` 文件输入
  • 4. Pygments 样式表
  • 5. 自定义显示风格

本文以插入 Python 程序代码块为例,详细介绍 minted 包的下载、安装、配置和使用。

minted 是一个用于突出显示源代码的 LaTeX 包,特别适用于 Python 程序代码的显示,也可以用于其它常用程序语言。

minted 包使用 Pygments 库简化了表达性语法高亮显示,还可以使用 fancyvrb 自定义选项输出。

网上关于使用 minted 包的资料很多,但有些讲的不清楚,有些跳过了一些步骤。我也踩了不少坑,干脆写篇笔记记录下来。

1. 需求描述和解决方案

在 LaTeX 将程序代码块按照程序设计语言的语法进行高亮显示。

常用的解决方案有:

  • 使用 listings 宏包。
    应用 listings 宏包可以对程序源代码的关键词、注释和字符串等使用不同的字体、颜色、加粗,也可以为代码添加边框、背景等风格,还可以自动显示程序行数。
    该方法需要自己设置语法高亮,在 LaTeX 中定义的参数比较复杂。
  • 使用minted 宏包。
    应用 minted 宏包可以对程序源代码的 关键词、注释和字符串等使用不同的字体、颜色、加粗,也可以为代码添加边框、背景等风格,还可以自动显示程序行数。
    该方法需要安装其他软件的 Pygments,安装配置比较复杂,但在 LaTeX 中的定义和使用比较简单。
    本文以插入 Python 程序代码块为例,详细介绍 minted 包的下载、安装、配置和使用。

2. minted 包的安装与配置

2.1 安装 Python 第三方库 Pygments

Pygments 可以完全定制任何程序语言所支持的突出显示标记,包括字符串、数字、类型标识符和外来结构(如HTML标记)中的特殊格式序列。

minted 包需要安装其他软件的 Pygments。

基于 minted 包插入 Python 程序代码块,首先要在 Python 中安装 Pygments 第三方库。步骤如下:

  • 打开命令行窗口 cmd

  • 使用包管理工具 pip 安装 Pygments 第三方库

pip install pygments

推荐从国内镜像源安装:

pip install pygments -i http://mirrors.aliyun.com/pypi/simple/

说明:也可以通过 Anaconda 或其它方法安装。

2.2 下载和安装 minted 宏包

  • TeXLive 和 MiKTeX 编辑器已经带有自带 minted 宏包,则可以跳过包的下载安装步骤。

  • 如果编辑器没有自带 minted 宏包,则需要自行下载 minted.sty 文件,可以从 CTAN 或 Github 获得:

    • CTAN 官方地址:CTAN: Package minted (https://ctan.org/pkg/minted)
    • Github 官方地址下载: github.com (https://github.com/gpoore/minted/tree/master/source)
  • 下载后要将 minted.sty 文件复制到 LaTeX 的安装路径,如:D:\Texlive\texmf-local\tex\latex\local

2.3 配置 LaTeX 编译环境

在 Latex 编译环境中要加入参数 –shell-escape

需要注意的是,如果在命令行窗口使用 latex 编译器,直接添加参数 –shell-escape。例如:

latex -shell-escape test.tex

但是在 TeXworks 等编译器中设置时,添加的参数是 -–shell-escape,注意是两个短横线。

  • 对于 TexStudio 软件:

    • 打开 TeXworks,选择菜单 编辑 >> 首选项;
    • 弹出 TeXworks首选项 窗口,选择 排版;
    • 在下方 处理工具 选中 XeLaTeX,点击 编辑,弹出 工具配置 窗口;
    • 选择添加按钮 +++ 添加新参数,输入-–shell-escape,注意是两个短横线;
    • 选择箭头按钮 ⇑\Uparrow⇑ ,将输入的参数-–shell-escape上移到第一行。

  • 对于 TexStudio 编辑器,通过 Options >> Configure TeXstudio >> Commands,将编译命令参数修改为:
xelatex.exe -synctex=1 -interaction=nonstopmode --shell-escape %.tex
  • 对于 WinEdit 编辑器,通过 Options >> Execution Modes, 在 Console Applications 的 Accessories 中选择 XeLaTeX,在下方的 Switches 中加入 --shell-escape 即可。

3. minted 包的使用

3.1 基本使用

minted 包的基本使用是:

  • 引用minted 包: \usepackage{minted}
  • 将程序代码插入 minted 环境中
\begin{minted}{<language>}
<code>
\end{minted}

测试案例:

新建一个 .tex 文件,复制以下代码进行编译,就可以测试 minted 的安装配置是否正确。注意要选择 xeLaTex 编译器。

\documentclass{article}\usepackage{minted}\begin{document}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{document}

运行结果:

注意事项:

minted 包与 LaTeX 中其它的一些定义可能存在冲突,同时存在将会报错。这也是本文推荐新建文件,复制上述简单代码进行测试的原因。

例如,将以下设置英文字体的语句加入刚才的 tex 文件中,就会发生错误,如下图所示。

\usepackage{newtxtext} % 设置英文字体

3.2 扩展使用

3.2.1 \mint 命令行

对于单行的源代码,可以使用 \mint 命令简写,语法为:

\mint[⟨options⟩]{⟨language⟩}⟨delim⟩⟨code⟩⟨delim⟩

使用 \mint 命令来排版一行程序代码,可以简化代码,效果与 minted 环境的输出是等效的。例如:

\mint{python}|import this|

3.2.2 \mintinline 行内使用

使用 \mintinline 命令可以排版一行文本内的程序代码,语法为:

\mintinline[⟨options⟩]{⟨language⟩}⟨delim⟩⟨code⟩⟨delim⟩

\mintinline 命令的分隔符可以是一对字符,如\ mint,也可以是一对匹配的花括号,如 {}。例如:

words \mintinline{python}{print(x**2)} words

3.2.3 \inputminted 文件输入

使用 \inputmented 命令可以读取和格式化整个文件,语法为:

\inputminted[<options>]{<language>}{<filename>}

4. Pygments 样式表

Pygments 提供了很多样式表,因此设置所需的样式表就可以切换高亮显示的效果。语法为:

\usemintedstyle[⟨language⟩]{⟨style⟩}

可以对整个文档(不指定语言)设置新的样式,也可以仅针对特定语言设置新的样式。

stylename可以通过 Pygments 在线演示 https://pygments.org/demo/ 或者 pygmentize−Lstylespygmentize -L stylespygmentize−Lstyles 命令查看。所有可用样式表的列表如下:

* default:The default style (inspired by Emacs 22).
* emacs:The default style (inspired by Emacs 22).
* friendly:A modern style based on the VIM pyte theme.
* colorful:A colorful style, inspired by CodeRay.
* autumn:A colorful style, inspired by the terminal highlighting style.
* murphy:Murphy's style from CodeRay.
* manni:A colorful style, inspired by the terminal highlighting style.
* material:This style mimics the Material Theme color scheme.
* monokai:This style mimics the Monokai color scheme.
* perldoc:Style similar to the style used in the perldoc code blocks.
* pastie:Style similar to the pastie default style.
* borland:Style similar to the style used in the borland IDEs.
* trac:Port of the default trac highlighter design.
* native:Pygments version of the "native" vim theme.
* fruity:Pygments version of the "native" vim theme.
* bw:* vim:Styles somewhat like vim 7.0
* vs:* tango:The Crunchy default Style inspired from the color palette from the Tango Icon Theme Guidelines.
* rrt:Minimalistic "rrt" theme, based on Zap and Emacs defaults.
* xcode:Style similar to the Xcode default colouring theme.
* igor:Pygments version of the official colors for Igor Pro procedures.
* paraiso-light:* paraiso-dark:* lovelace:The style used in Lovelace interactive learning environment. Tries to avoid the "angry fruit salad" effect with desaturated and dim colours.
* algol:* algol_nu:* arduino:The Arduino® language style. This style is designed to highlight the Arduino source code, so exepect the best results with it.
* rainbow_dash:A bright and colorful syntax highlighting theme.
* abap:* solarized-dark:The solarized style, dark.
* solarized-light:The solarized style, light.
* sas:Style inspired by SAS' enhanced program editor. Note This is not meant to be a complete style. It's merely meant to mimic SAS' program editor syntax highlighting.
* stata:Light mode style inspired by Stata's do-file editor. This is not meant to be a complete style, just for use with Stata.
* stata-light:Light mode style inspired by Stata's do-file editor. This is not meant to be a complete style, just for use with Stata.
* stata-dark:* inkpot:* zenburn:Low contrast Zenburn style.
* gruvbox-dark:Pygments version of the "gruvbox" dark vim theme.
* gruvbox-light:Pygments version of the "gruvbox" Light vim theme.

5. 自定义显示风格

minted 可以通过定义键值 key=valuekey=valuekey=value 使用选项,自定义显示风格。

行间和行内代码分别用命令\setminted\setmintedinline设置。

常用选项很多,具体参见 minted 说明文档。

本文只给出一个案例。

\documentclass{article}
\usepackage{minted} %  -shell-escape
\begin{document}
\begin{minted}[mathescape,linenos,numbersep=5pt,gobble=2,frame=lines,framesep=2mm]{csharp}string title = "This is a Unicode π in the sky"/*Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the perimeterof an $n$-sided regular polygon circumscribing acircle of diameter $d$.*/const double pi = 3.1415926535
\end{minted}
\end{document}

显示结果如下:

(本文完)


版权声明:
youcans@xupt 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/125137881)
Copyright 2022 youcans, XUPT
Crated:2022-6-5

LaTex实战笔记 4-插入 Python 程序代码块相关推荐

  1. 210811_152958-Gooey实战 | 几行代码转换Python程序为图形界面应用!

    Gooey实战 | 几行代码转换Python程序为图形界面应用! 1.概述 今天发现公众号的一个作者大大用Python写了个小工具, 发现还挺好玩, 而且代码已经分享给大家了.在文章末尾提到还没有为这 ...

  2. 20210112.使用字典来创建并分类汇总物品清单的python程序代码

    20210112.使用字典来创建并分类汇总物品清单的python程序代码 #这段代码使用字典来创建并分类汇总物品清单.为<python编程快速上手--让繁琐工作自动化>一书中的5.6.1实 ...

  3. python函数代码块以什么开头_Python初体验-开篇 代码全析

    第一次接触Python,现在就开始蟒蛇的威力. 一.首先贴上我们要解析的code: '''Convert file sizes to human-readable form. Available fu ...

  4. 计算Python的代码块或程序的运行时间

    1.运用场景 在很多的时候我们需要计算我们程序的性能,这个时候我们常常需要统计程序运行的时间.下面我们就来说说怎么统计程序的运行时间. 2. 实现方法 计算Python的某个程序,或者是代码块运行的时 ...

  5. python程序代码大全-调试Python程序代码的几种方法总结

    程序能一次写完并正常运行的概率很小,基本不超过1%.总会有各种各样的bug需要修正.有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时,哪些变量的值是正确的,哪些变量的值是错误 ...

  6. 如何进入python程序代码编辑环境_Python怎么打开代码编辑器 来学习吧

    Python作为近几年来最为优秀的编程语言之一,受到了很多程序员的追捧,现在我教大家如何打开Python的代码编辑器 工具/材料 电脑 Python 操作方法 01 首先,点击[开始]按钮,开始按钮几 ...

  7. python程序代码解析_Python源码分析3 – 词法分析器PyTokenizer

    Introduction 上次我们分析了Python中执行程序可分为5个步骤: Tokenizer进行词法分析,把源程序分解为Token Parser根据Token创建CST CST被转换为AST A ...

  8. python程序代码_python基础二

    Python基础-注释的引入 注释的分类: <1>单行注释:以#开头,#右边的所有文字当作说明,而不是真正要执行的程序,起辅助说明作用 多行注释用三个单引号 ''' 或者三个双引号 &qu ...

  9. python程序-调试Python程序代码的几种方法总结

    程序能一次写完并正常运行的概率很小,基本不超过1%.总会有各种各样的bug需要修正.有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时,哪些变量的值是正确的,哪些变量的值是错误 ...

最新文章

  1. 每一个C#开发者必须知道的13件事情
  2. java makerdd_Spark中parallelize函数和makeRDD函数的区别
  3. 十进制数转换为二进制数
  4. Scikit-learn 数据预处理之鲁棒缩放RobustScaler
  5. Bootstrap弹出层(modal)垂直居中简单解决方案(无需修改js)
  6. java语言标识符的声明规范
  7. http 响应消息解码_响应生成所需的解码策略
  8. 鸿蒙OpenHarmony hi3516开发板,标准系统实现智能门禁
  9. 【名单回顾】CSP-J2 2019年第二轮入门级获奖名单(仅列北京地区小学生)
  10. 量化新手初识Campisi模型
  11. mysql 复制 数据 表结构_MySQL复制表结构和表数据
  12. 重学React基础知识整理——组件间的另类通信“插槽”(五)
  13. R语言实现单因素方差分析
  14. 合并两个有序数组(给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。)
  15. 电商领域知识图谱:常识抽取,表示与应用
  16. 背叛的下场,你敢不敢看
  17. 文件压缩c语言程序代码,C语言程序设计之RLE压缩解压算法
  18. 全志平台通读写寄存器的方法
  19. css自适应图片样式,css怎么让图片自适应?css图片自适应大小的方法介绍
  20. java mysql SSM实现的校园门户平台网站系统源码+含开题报告与需求分析+包安装配置

热门文章

  1. VSCode如何关掉右边的缩略图(预览面板)
  2. Java系列技术之Hibernate5操作数据库-钟洪发-专题视频课程
  3. 如何制作证件照?证件照怎么在线制作?
  4. webservice课件
  5. 历年真题 九宫重排(BFS)
  6. 推荐算法之CB,CF的实现
  7. H3C无线接入控制器特点
  8. 一个项目中不能同时出现两个main函数
  9. python 最准确的图片转文字_使用Tesseract+python进行图片转文字记录
  10. Proverif分析handshake协议