每当我使用该minted
包突出显示代码时,我都会使用\usemintedstyle{vim}
,除了它看起来像在浅色背景上应用了深色 VIM 配色方案并且在白色背景上难以阅读之外,它看起来还不错。
有什么方法可以改变vim
颜色方案以匹配浅色背景?
答案1
着色由 Pygmentize(Pygments 库的命令行界面)完成。Pygments 中定义的 vim 样式非常适合深色背景,因此“简单”的解决方案是指定黑色背景或选择适合浅色背景的现有样式。
如果你想指定一个新的 Pygments 样式,你可以这样做,但这有点复杂。下面我说明如何从 vim 样式派生出新的样式('myvim'):
步骤 1:找到你的 pygments 样式子目录——在我的计算机上/usr/local/lib/python2.7/site-packages/pygments/styles
第 2 步:在 pygments style 子目录中创建一个新文件,myvim.py
其中包含以下 Python 代码:
from pygments.style import Style
from pygments.token import Keyword, Name, Comment, String, Error, Number, Operator, Generic, Whitespace, Token
from vim import VimStyle
# inherit basic styles from the VimStyle class in vim.py
class MyVimStyle(VimStyle):
# only change what we need by setting class attributes
VimStyle.styles[Token] = "#000000"
VimStyle.styles[Number] = "bold #cd00cd"
步骤 3:打开文件__init__.py
并将新风格添加到STYLE_MAP
字典中:
#: Maps style names to 'submodule::classname'.
STYLE_MAP = {
... leave other styles definitions alone ...
'myvim': 'myvim::MyVimStyle', # add this line at the end
}
步骤 4:编译__init__.py
并myvim.py
转为字节码
$ python __init__.py
$ python myvim.py
步骤 5:假设您所做的一切都正确无误,您可以测试内置样式以及新样式:
\documentclass{article}
\usepackage{minted}
\begin{document}
`manni' style on a white background:
\usemintedstyle{manni}
\begin{minted}{c}
char *test = "1000";
int *test_int = (int*) test;
printf("Machine is %s-endian", (test_int >> 1)? "big":"little");
\end{minted}
`vim' style on a dark background:
\usemintedstyle{vim}
% note that minted seems to screw up the placment of the background here
\begin{minted}[bgcolor=black]{c}
char *test = "1000";
int *test_int = (int*) test;
printf("Machine is %s-endian", (test_int >> 1)? "big":"little");
\end{minted}
Derived `myvim' style on a white background:
\usemintedstyle{myvim}
\begin{minted}{c}
char *test = "1000";
int *test_int = (int*) test;
printf("Machine is %s-endian", (test_int >> 1)? "big":"little");
\end{minted}
\end{document}
您应该看到以下内容: