我使用 minted 包来列出代码,但我不太喜欢其中的一些颜色。是否可以配置这些颜色?例如,当我显示颜色来查看 XML 颜色时,关键字显示为绿色,而我并不需要。有人可以解释一下这种情况是否可行吗?
我正在包含该包并包含 XML 代码:
\usepackage{minted}
\inputminted{xml}{codeListings/test.xml.tex}
答案1
让我尝试在 LaTeX 级别向您展示一种方法。
首先我们要分析一下pygmentize高亮代码的方式以及如何在LaTeX中得到高亮的代码。
如果有类似下面的代码:
\begin{minted}{xml}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<description>JAX-WS endpoint</description>
<display-name>WSServlet</display-name>
<servlet-name>WSServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WSServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CompressionFilter</filter-name>
<filter-class>com.bsiag.fleet.external.calculation.webservice.compression.CompressionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CompressionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
\end{minted}
现在环境的内容已写入文件\jobname.pyg
。现在 pygmentize 有机会了。运行它后,您将获得一个名为的文件,\jobname.out.pyg
其内容如下:
\begin{Verbatim}[commandchars=\\\{\}, ]
\PY{c+cp}{\PYZlt{}?xml version=\PYZdq{}1.0\PYZdq{} encoding=\PYZdq{}UTF\PYZhy{}8\PYZdq{}?\PYZgt{}}
\PY{n+nt}{\PYZlt{}web\PYZhy{}app} \PY{n+na}{xmlns:xsi=}\PY{l+s}{\PYZdq{}http://www.w3.org/2001/XMLSchema\PYZhy{}instance\PYZdq{}}
\PY{n+na}{xmlns=}\PY{l+s}{\PYZdq{}http://java.sun.com/xml/ns/javaee\PYZdq{}}
\PY{n+na}{xmlns:web=}\PY{l+s}{\PYZdq{}http://java.sun.com/xml/ns/javaee/web\PYZhy{}app\PYZus{}2\PYZus{}5.xsd\PYZdq{}}
\PY{n+na}{xsi:schemaLocation=}\PY{l+s}{\PYZdq{}http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web\PYZhy{}app\PYZus{}2\PYZus{}5.xsd\PYZdq{}}
\PY{n+na}{version=}\PY{l+s}{\PYZdq{}2.5\PYZdq{}}\PY{n+nt}{\PYZgt{}}
\PY{n+nt}{\PYZlt{}listener\PYZgt{}}
\PY{n+nt}{\PYZlt{}listener\PYZhy{}class\PYZgt{}}com.sun.xml.ws.transport.http.servlet.WSServletContextListener\PY{n+nt}{\PYZlt{}/listener\PYZhy{}class\PYZgt{}}
\PY{n+nt}{\PYZlt{}/listener\PYZgt{}}
\PY{n+nt}{\PYZlt{}servlet\PYZgt{}}
\PY{n+nt}{\PYZlt{}description\PYZgt{}}JAX\PYZhy{}WS endpoint\PY{n+nt}{\PYZlt{}/description\PYZgt{}}
\PY{n+nt}{\PYZlt{}display\PYZhy{}name\PYZgt{}}WSServlet\PY{n+nt}{\PYZlt{}/display\PYZhy{}name\PYZgt{}}
\PY{n+nt}{\PYZlt{}servlet\PYZhy{}name\PYZgt{}}WSServlet\PY{n+nt}{\PYZlt{}/servlet\PYZhy{}name\PYZgt{}}
\PY{n+nt}{\PYZlt{}servlet\PYZhy{}class\PYZgt{}}com.sun.xml.ws.transport.http.servlet.WSServlet\PY{n+nt}{\PYZlt{}/servlet\PYZhy{}class\PYZgt{}}
\PY{n+nt}{\PYZlt{}load\PYZhy{}on\PYZhy{}startup\PYZgt{}}1\PY{n+nt}{\PYZlt{}/load\PYZhy{}on\PYZhy{}startup\PYZgt{}}
\PY{n+nt}{\PYZlt{}/servlet\PYZgt{}}
\PY{n+nt}{\PYZlt{}servlet\PYZhy{}mapping\PYZgt{}}
\PY{n+nt}{\PYZlt{}servlet\PYZhy{}name\PYZgt{}}WSServlet\PY{n+nt}{\PYZlt{}/servlet\PYZhy{}name\PYZgt{}}
\PY{n+nt}{\PYZlt{}url\PYZhy{}pattern\PYZgt{}}/services/*\PY{n+nt}{\PYZlt{}/url\PYZhy{}pattern\PYZgt{}}
\PY{n+nt}{\PYZlt{}/servlet\PYZhy{}mapping\PYZgt{}}
\PY{n+nt}{\PYZlt{}filter\PYZgt{}}
\PY{n+nt}{\PYZlt{}filter\PYZhy{}name\PYZgt{}}CompressionFilter\PY{n+nt}{\PYZlt{}/filter\PYZhy{}name\PYZgt{}}
\PY{n+nt}{\PYZlt{}filter\PYZhy{}class\PYZgt{}}com.bsiag.fleet.external.calculation.webservice.compression.CompressionFilter\PY{n+nt}{\PYZlt{}/filter\PYZhy{}class\PYZgt{}}
\PY{n+nt}{\PYZlt{}/filter\PYZgt{}}
\PY{n+nt}{\PYZlt{}filter\PYZhy{}mapping\PYZgt{}}
\PY{n+nt}{\PYZlt{}filter\PYZhy{}name\PYZgt{}}CompressionFilter\PY{n+nt}{\PYZlt{}/filter\PYZhy{}name\PYZgt{}}
\PY{n+nt}{\PYZlt{}url\PYZhy{}pattern\PYZgt{}}/*\PY{n+nt}{\PYZlt{}/url\PYZhy{}pattern\PYZgt{}}
\PY{n+nt}{\PYZlt{}/filter\PYZhy{}mapping\PYZgt{}}
\PY{n+nt}{\PYZlt{}/web\PYZhy{}app\PYZgt{}}
\end{Verbatim}
现在我们看到,LaTeX 级别的每个突出显示都是由\PY
具有两个强制参数的命令完成的。第一个是样式,第二个是突出显示的字符串。
现在我们需要 和 朋友 的定义\PY
:
\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%%
\let\PY@ul=\relax \let\PY@tc=\relax%%
\let\PY@bc=\relax \let\PY@ff=\relax}
\def\PY@tok#1{\csname PY@tok@#1\endcsname}
\def\PY@toks#1+{\ifx\relax#1\empty\else%%
\PY@tok{#1}\expandafter\PY@toks\fi}
\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%%
\PY@it{\PY@bf{\PY@ff{#1}}}}}}}
\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}
pygmentize 还将特殊字符更改为命令以防止任何冲突:
\def\PYZbs{\char`\\}
\def\PYZus{\char`\_}
\def\PYZob{\char`\{}
\def\PYZcb{\char`\}}
\def\PYZca{\char`\^}
\def\PYZam{\char`\&}
\def\PYZlt{\char`\<}
\def\PYZgt{\char`\>}
\def\PYZsh{\char`\#}
\def\PYZpc{\char`\%%}
\def\PYZdl{\char`\$}
\def\PYZti{\char`\~}
%% for compatibility with earlier versions
\def\PYZat{@}
\def\PYZlb{[}
\def\PYZrb{]}
与上述命令和字符定义相关,突出显示是通过以下方式完成的:
\def\PY@tok@gd{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\def\PY@tok@gu{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\def\PY@tok@gt{\def\PY@tc##1{\textcolor[rgb]{0.00,0.25,0.82}{##1}}}
\def\PY@tok@gs{\let\PY@bf=\textbf}
\def\PY@tok@gr{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
\def\PY@tok@cm{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\def\PY@tok@vg{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\def\PY@tok@m{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\def\PY@tok@mh{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\def\PY@tok@go{\def\PY@tc##1{\textcolor[rgb]{0.50,0.50,0.50}{##1}}}
\def\PY@tok@ge{\let\PY@it=\textit}
\def\PY@tok@vc{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\def\PY@tok@il{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\def\PY@tok@cs{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\def\PY@tok@cp{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}}
\def\PY@tok@gi{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
\def\PY@tok@gh{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\def\PY@tok@ni{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}}
\def\PY@tok@nl{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}}
\def\PY@tok@nn{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@no{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
\def\PY@tok@na{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}}
\def\PY@tok@nb{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@nc{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@nd{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\def\PY@tok@ne{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}}
\def\PY@tok@nf{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\def\PY@tok@si{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\def\PY@tok@s2{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\def\PY@tok@vi{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\def\PY@tok@nt{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@nv{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\def\PY@tok@s1{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\def\PY@tok@sh{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\def\PY@tok@sc{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\def\PY@tok@sx{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@bp{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@c1{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\def\PY@tok@kc{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@c{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\def\PY@tok@mf{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\def\PY@tok@err{\def\PY@bc##1{\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{##1}}}
\def\PY@tok@kd{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@ss{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\def\PY@tok@sr{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\def\PY@tok@mo{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\def\PY@tok@kn{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@mi{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\def\PY@tok@gp{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\def\PY@tok@o{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\def\PY@tok@kr{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@s{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\def\PY@tok@kp{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@w{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\def\PY@tok@kt{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
\def\PY@tok@ow{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\def\PY@tok@sb{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\def\PY@tok@k{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PY@tok@se{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}}
\def\PY@tok@sd{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
如果您想要更改某些内容,则必须在 和 之间\makeatletter
以及makeatother
内部进行\AtBeginDocument
:
这是我的测试文件:
\listfiles
\documentclass[landscape]{article}
\usepackage{geometry}
\usepackage[T1]{fontenc}
\usepackage{minted}
\renewcommand\DeleteFile[1]{}
\makeatletter
\AtBeginDocument{%
\def\PY@tok@c{\let\PY@it=\textbf\def\PY@tc##1{\textcolor[rgb]{0.0,0.0,0.0}{##1}}}
\def\PY@tok@cp{\def\PY@tc##1{\textcolor[rgb]{1.0,0.2,0.5}{##1}}}
\def\PY@tok@nt{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{1.00,0.20,0.00}{##1}}}
}
\makeatother
\begin{document}
\begin{minted}{xml}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<description>JAX-WS endpoint</description>
<display-name>WSServlet</display-name>
<servlet-name>WSServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WSServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CompressionFilter</filter-name>
<filter-class>com.bsiag.fleet.external.calculation.webservice.compression.CompressionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CompressionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
\end{minted}
\end{document}
答案2
您可以使用以下命令调整颜色方案:
\usemintedstyle{name}
‹name› 是已安装配色方案的名称。要获取这些配色方案的列表,请在命令行中发出以下命令:
pygmentize -L styles
要创造自己的风格,按照 Pygments 文档中的说明进行操作。
答案3
更新一下,供大家参考。如果您只想修改一两种样式,前缀不再是PY
,而是PYG
,如果您只想为一种特定的配色方案(例如“perldoc”)覆盖某些内容,您必须执行以下操作:
\makeatletter
\AtBeginDocument{%
\def\PYGperldoc@tok@kt{\let\PYGperldoc@bf=\textbf\def\PYGperldoc@tc##1{\textcolor[HTML]{00688B}{##1}}}
}
\makeatother
但这是一些严重的猴子补丁,与所有猴子补丁一样,非常脆弱,在包/库更新时容易损坏。你已被警告。