latex中代码⾼亮显⽰宏包minted⽤法
latex中代码⾼亮显⽰宏包minted⽤法
1. 前提
使⽤minted需要安装python环境,以及Pygments模块,因为minted依赖于该模块提供超过300种语⾔的语法⾼亮。
latex编译时也需要添加选项-shell-escape,因为它需要调⽤Pygments程序,所以要通过该选项告诉tex编译器。
2. 基本⽤法:
\begin{minted}{<language>}
<code>
\end{minted}
⼀个完整的例⼦是这样的:
\documentclass{article}
\upackage{minted}
\begin{document}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{document}
除了可以使⽤minted环境外,也可以使⽤\mint命令来代替该环境,⽐如:
\mint{python}|import this|
另外,也可以在⾏内使⽤,⽐如:
情绪测试words\mintinline{python}{print(x**2)}words
也可以通过⽂件来输⼊需要⾼亮的代码:
\inputminted[<options>]{<language>}{<filename>}.
minted宏包也提供了listing浮动体环境,⽤于代码的浮动显⽰。⽐如:
\begin{listing}[H]迟来的春天
\mint{cl}/(car (cons 1 '(2)))/
\caption{Example of a listing.}
\label{lst:example}
\end{listing}
还可以利⽤\listoflistings⽣成代码listing的⽬录。
3. 更换不同的⾼亮风格
评论有同学问,默认的风格不好看,怎么改⾼亮的风格。这的确是⼀个重要问题,我们使⽤minted就是为了好看的⾼亮效果的。所以这⾥我们先介绍怎么更换风格再详细介绍minted宏包的其它细节。
由于Pygments已经提供了很多样式,所以切换的操作,只是调⽤对应的样式即可。命令为:
\umintedstyle{stylename}
⽰例代码如下:
\documentclass{minimal}
\upackage[b6paper]{geometry}
\upackage[b6paper]{geometry}
\upackage{ctex}
\upackage{minted}
\umintedstyle{lovelace}
%Styles:
%~~~~~~~
%* default:
% The default style (inspired by Emacs 22).
%* emacs:
% The default style (inspired by Emacs 22).
%* friendly:
% A modern style bad 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 ud in the perldoc code blocks.
%* pastie:
% Style similar to the pastie default style.
%* borland:
航天手抄报
% Style similar to the style ud 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, bad 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 ud 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:
%* 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 u 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 u 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.
\begin{document}
\begin{minted}[linenos=true]{c++}
#include <iostream>
int main() {
std::cout << "Hello "
<< "world"
<< std::endl;
}
\end{minted}
\begin{minted}[linenos=true]{python}
# -*- coding: utf-8 -*-
import torch
import numpy as np
from matplotlib import pyplot as plt
import random
#创建输⼊和输出随机张量
#x = torch.randn(N, D_in)
#y = torch.randn(N, D_out)
datanp = np.loadtxt('res.csv', delimiter=',')
猪油渣怎么做好吃
\end{minted}
\end{document}
结果为:
若换成vs,则结果为:
如果我们给代码块加上背景还可以实现dark风格的样式⽐如:
\documentclass{minimal}
\upackage[b6paper]{geometry}
\upackage{ctex}
\upackage{xcolor}
\upackage{minted}
\umintedstyle{paraiso-dark}
\begin{document}
\begin{minted}[bgcolor=black,linenos=true]{c++}
#include <iostream>
int main() {
std::cout << "Hello "
<< "world"
<< std::endl;
}
\end{minted}
\begin{minted}[bgcolor=black,linenos=true]{python} # -*- coding: utf-8 -*-
import torch
import numpy as np
from matplotlib import pyplot as plt
import random
#创建输⼊和输出随机张量
#x = torch.randn(N, D_in)
#y = torch.randn(N, D_out)
datanp = np.loadtxt('res.csv', delimiter=',')
\end{minted}
\end{document}