我的标签如下:
\documentclass{book}
\usepackage{minted}
\usemintedstyle{default}
\begin{document}
\begin{minted}[]{python}
saying = "It's turtles all the way down"
\end{minted}
\usemintedstyle{bw}
\begin{minted}[]{python}
saying = "It's turtles all the way down"
\end{minted}
\end{document}
在第二个列表中,我需要更改all colors to B/W
,但文本已更改为italic style
,我该如何仅更改颜色?请提出建议....
答案1
为了获得黑白风格的变体,本次尝试在本地禁用颜色命令。
\documentclass{article}
\usepackage{minted}
\newcommand{\mintedStyleBw}{%
\renewcommand\fcolorbox[3][]{##3}%
\renewcommand\textcolor[3][]{##3}%
}
\makeatletter
% let minted accept fancyvrb option "formatcom*"
\minted@def@optfv{formatcom*}
\makeatother
\begin{document}
\subsection*{Colorful \texttt{minted} output}
\begin{minted}{python}
saying = "It's turtles all the way down" # comment
a = 1 + 3 + len(list())
\end{minted}
inline: \mintinline{python}|saying = "It's turtles all the way down" # comment|
\subsection*{Black and white \texttt{minted} output}
\begin{minted}[formatcom*=\mintedStyleBw]{python}
saying = "It's turtles all the way down" # comment
a = 1 + 3 + len(list())
\end{minted}
inline: \mintinline[formatcom*=\mintedStyleBw]{python}|saying = "It's turtles all the way down" # comment|
\end{document}
此外,可以定义一个新minted
选项,例如bwstyle
,用于切换黑白(子)样式。这使用户界面更加清晰。
\documentclass{article}
\usepackage{minted}
\makeatletter
\newcommand{\minted@style@bw}{%
\renewcommand\fcolorbox[3][]{##3}%
\renewcommand\textcolor[3][]{##3}%
}
% define new minted option "bwstyle"
\minted@def@opt@switch{bwstyle}
\fvset{formatcom*={%
\ifthenelse{\equal{\minted@get@opt{bwstyle}{true}}{true}}
{\minted@style@bw}{}%
}}
\makeatother
\begin{document}
\subsection*{Colorful \texttt{minted} output}
\begin{minted}{python}
saying = "It's turtles all the way down" # comment
a = 1 + 3 + len(list())
\end{minted}
inline: \mintinline{python}|saying = "It's turtles all the way down" # comment|
\subsection*{Black and white \texttt{minted} output}
\begin{minted}[bwstyle]{python}
saying = "It's turtles all the way down" # comment
a = 1 + 3 + len(list())
\end{minted}
inline: \mintinline[bwstyle]{python}|saying = "It's turtles all the way down" # comment|
\end{document}
一些说明:
- 样式中使用的颜色
Pygments
在 python 源代码中是硬编码的,最后在文件\textcolor[rgb]{0.73,0.73,0.73}{...}
中转换为.pygstyle
。 \PYG@xxx
与修改 python 或tex 样式代码相比,禁用颜色命令需要的努力更少。- 首先,这个答案使用
fancyvrb
选项codes*
插入颜色命令补丁。但与不同fancyvrb
,fvextra
限制范围code*
,它被设计为仅接受 catcode 更改(然后存储在\FancyVerbCodes
),到标记化步骤,因此使得\mintinline
和\Verb
环境VerbEnv
在\FancyVerbCodes
排版步骤中被忽略。现在使用另一个选项,它也formatcom*
适用于。 报告给,请参阅\inlinemint
fvextra
https://github.com/gpoore/fvextra/issues/17。