Monokai 主题令人烦恼地突出显示 unicode 字符

Monokai 主题令人烦恼地突出显示 unicode 字符

我最近开始使用 monokai 主题,minted因为它与 XeLaTeX 配合得很好。我发现在代码中使用 unicode 字符时,它们会以红色突出显示。这种行为在mintedPython 的默认主题中是看不到的,所以我猜这与 monokai 主题有关。关于如何删除难看的突出显示,有什么想法吗?

问题如下所示(unicode 括号以红色突出显示):

Illustration of the highlighting problem

编辑这是一个最小的工作示例:

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{color}
\usepackage{tkz-tab}


\usepackage{minted}
\usepackage{fontspec}
\setmonofont{DejaVu Sans Mono}

\usemintedstyle{monokai}
\definecolor{codeBg}{HTML}{282822}
\definecolor{textBg}{HTML}{1c1c17}

\setminted[python]{
    breaklines=true,
    encoding=utf8,
    fontsize=\footnotesize,
    bgcolor=codeBg
}

\begin{document}

\pagecolor{textBg}
\color{white}

\begin{multicols*}{2}

Commençons par un exemple : vérifions que la matrice
\[A = \begin{pmatrix}3&9&-9\\2&0&0\\3&3&3\end{pmatrix}\] 
est nilpotente d'indice 3.
\begin{minted}{python}
>>> from matrix import *
>>> A = Matrix([
... [3, 9,-9],
... [2, 0, 0],
... [3, 3,-3]
... ])
>>> A**2

⎛0 0   0 ⎞
⎜6 18 -18⎟
⎝6 18 -18⎠

>>> A**3

⎛0 0 0⎞
⎜0 0 0⎟
⎝0 0 0⎠

\end{minted}

\end{multicols*}
\end{document}

答案1

正如评论中指出的那样,Pygmentize 词法分析器可能是(取决于所涉及的编程语言)非常挑剔关于 Unicode 字符,并可能将它们标记为错误标记。这由 monokai 风格继承。

真正的解决方法是修复词法分析器。哎呀,我现在明白了你的问题的正确解决方法。出于感情原因,我保留了最初的解决方法。但跳到底部进行真正的修复。

编辑

  • 我没有观察到 OP 的代码加载了所有inputenc, fontenc, fontspec\setmainfont。我已删除inputencfontenc在编辑中加载。

  • 在我感性地保留的方法中,替换文本\PYGmonokai@tok@err没有使用正确前缀的宏。我也修复了它。但我现在不记得什么时候激活了@bc。(需要检查 Pygmentize latex 模板)。可能是bc用于背景颜色,但现在我不记得了。


我没有立即minted从文档中找到合适的钩子,所以我决定修补fancyvrb Verbatim所使用的环境minted。我也有点忘记了钩子 fancyvrb,所以我通过粗暴地完成了这件事etoolbox

\documentclass[12pt]{article}

%%%% \usepackage[utf8]{inputenc}% NO! fontspec loaded below
%%%% \usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{color}
\usepackage{tkz-tab}


\usepackage{minted}
\usepackage{fontspec}
\setmonofont{DejaVu Sans Mono}

\usemintedstyle{monokai}
\definecolor{codeBg}{HTML}{282822}
\definecolor{textBg}{HTML}{1c1c17}

\setminted[python]{
    breaklines=true,
    encoding=utf8,
    fontsize=\footnotesize,
    bgcolor=codeBg
}


\makeatletter
\usepackage{etoolbox}
\AtBeginEnvironment{Verbatim}{%
%\show\PYGmonokai@tok@err
   \def\PYGmonokai@tok@err {\def\PYGmonokai@bc##1{\strut ##1}}%
%\show\PYGmonokai@tok@err
}%
\makeatother
\begin{document}

\pagecolor{textBg}
\color{white}

\begin{multicols*}{2}

Commençons par un exemple : vérifions que la matrice
\[A = \begin{pmatrix}3&9&-9\\2&0&0\\3&3&3\end{pmatrix}\] 
est nilpotente d'indice 3.

\begin{minted}{python}
>>> from matrix import *
>>> A = Matrix([
... [3, 9,-9],
... [2, 0, 0],
... [3, 3,-3]
... ])
>>> A**2

⎛0 0   0 ⎞
⎜6 18 -18⎟
⎝6 18 -18⎠

>>> A**3

⎛0 0 0⎞
⎜0 0 0⎟
⎝0 0 0⎠

\end{minted}

\end{multicols*}
\end{document}

enter image description here

请注意,Pygmentize latex 样式表存在一些错误,这些错误由 minted 继承。以下是 Pygments 问题跟踪器的一些链接。(这些问题似乎不会很快得到解决)


回到真正的问题:您正在使用python词法分析器进行交互式会话。您应该使用pycon词法分析器。

$ pygmentize -L lexers | grep -C 1 py
    ANTLR With Perl Target (filenames *.G, *.g)
* antlr-python:
    ANTLR With Python Target (filenames *.G, *.g)
--
    Bro (filenames *.bro)
* bst, bst-pybtex:
    BST (filenames *.bst)
--
* cobol:
    COBOL (filenames *.cob, *.COB, *.cpy, *.CPY)
* cobolfree:
--
    Cypher (filenames *.cyp, *.cypher)
* cython, pyx, pyrex:
    Cython (filenames *.pyx, *.pxd, *.pxi)
* d-objdump:
--
    Ioke (filenames *.ik)
* ipython2, ipython:
    IPython 
* ipython3:
    IPython3 
* ipythonconsole:
    IPython console session 
--
    NSIS (filenames *.nsi, *.nsh)
* numpy:
    NumPy 
--
    Puppet (filenames *.pp)
* py3tb:
    Python 3.0 Traceback (filenames *.py3tb)
* pycon:
    Python console session 
* pypylog, pypy:
    PyPy Log (filenames *.pypylog)
* pytb:
    Python Traceback (filenames *.pytb)
* python, py, sage:
    Python (filenames *.py, *.pyw, *.sc, SConstruct, SConscript, *.tac, *.sage)
* python3, py3:
    Python 3 

因此这里是:

\begin{minted}{pycon}
>>> from matrix import *
>>> A = Matrix([
... [3, 9,-9],
... [2, 0, 0],
... [3, 3,-3]
... ])
>>> A**2

⎛0 0   0 ⎞
⎜6 18 -18⎟
⎝6 18 -18⎠

>>> A**3

⎛0 0 0⎞
⎜0 0 0⎟
⎝0 0 0⎠

\end{minted}

enter image description here

相关内容