这是 tex 代码
\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{mathtools}
\usepackage{listings}
\usepackage{minted}
\begin{document}
\begin{minted}[escapeinside=||,mathescape=true, breaklines]{python}
# $\begin{vmatrix}\lambda _{a_1}^{b_1} & \dots & \lambda _{a_1}^{b_p}\\\vdots & \ddots & \vdots\\\lambda _{a_p}^{b_1} & \dots & \lambda _{a_p}^{b_p}\end{vmatrix}$
\end{minted}
\begin{lstlisting}[escapeinside=||,mathescape=true, breaklines]{python}
# $\begin{vmatrix}\lambda _{a_1}^{b_1} & \dots & \lambda _{a_1}^{b_p}\\\vdots & \ddots & \vdots\\\lambda _{a_p}^{b_1} & \dots & \lambda _{a_p}^{b_p}\end{vmatrix}$
\end{lstlisting}
\end{document}
下面是输出
如何&
从输出中删除?
答案1
可能是latex 格式化程序pygments
。目前,mathescape
使用时,生成的Verbatim
环境将如下所示
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
<verbatim content>
\end{Verbatim}
此处fancyvrb
选项中codes={...}
的类别代码&
未恢复,因此仍然在(其他)中,您会在输出中12
看到逐字字符。&
minted
和包的解决方法fvextra
:
\documentclass{article}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{minted}
\begin{document}
\subsection*{\Verb|minted| and \Verb|Verbatim| envs, actual}
\begin{minted}[mathescape]{python}
# catcode of & = $\text{\the\catcode`\&}, \begin{vmatrix} a & b \\ c & d \end{vmatrix}$
\end{minted}
\begin{Verbatim}[commandchars=\\\{\}, mathescape]
# catcode of & = $\text{\the\catcode`\&}, \begin{vmatrix} a & b \\ c & d \end{vmatrix}$
\end{Verbatim}
\makeatletter
% This patches fvextra option "mathescape", but unfortunately it won't work
% for minted, since the pygments-generated (till v2.16.1) "Verbatim" env
% always drop "mathescape" option and use its own "codes={...}" settings.
%
% See https://github.com/pygments/pygments/issues/1988
\appto\FV@MathEscape{\catcode`\&=4\relax}
% This workaround works for minted (v2.8).
\FV@AddToHook\FV@CatCodesHook{%
% if inside a minted with mathescape, change catcode of &
\ifthenelse{%
\(\NOT{\isundefined{\minted@cmd}}\)% used to determin if inside a minted
\AND
\equal{\minted@get@opt{mathescape}{false}}{true}}
{\catcode`\&=4\relax}
{}%
}
\makeatother
\subsection*{\Verb|minted| and \Verb|Verbatim| envs, with workaround}
\begin{minted}[mathescape, breaklines]{python}
# minted with mathescape
# catcode of & = $\text{\the\catcode`\&}, \begin{vmatrix} a & b \\ c & d \end{vmatrix}$
\end{minted}
\begin{minted}[breaklines]{python}
# minted, no mathescape
# catcode of & = $\text{\the\catcode`\&}, \begin{vmatrix} a & b \\ c & d \end{vmatrix}$
\end{minted}
% NOTE:
% Using "mathescape" now causes all occurances of $, ^, _, and in addition &
% gain their normal catcodes in verbatim content.
\begin{Verbatim}[commandchars=\\\{\}, mathescape, breaklines]
# Verbatim with mathescape, now \& outside of \$...\$ has to be escaped
# catcode of \& = $\text{\the\catcode`\&}, \begin{vmatrix} a & b \\ c & d \end{vmatrix}$
\end{Verbatim}
\begin{Verbatim}[breaklines]
# Verbatim, no mathescape
# catcode of & = $\text{\the\catcode`\&}, \begin{vmatrix} a & b \\ c & d \end{vmatrix}$
\end{Verbatim}
\subsection*{\Verb|lstlisting| env}
\begin{lstlisting}[basicstyle=\ttfamily, escapeinside=||, mathescape, breaklines]{python}
# catcode of & = $\text{\the\catcode`\&}, \begin{vmatrix} a & b \\ c & d \end{vmatrix}$
\end{lstlisting}
\end{document}
版本历史
- 2021年12月11日,我打开了https://github.com/pygments/pygments/issues/1988在
pygments
repo 中建议添加\catcode`\&=4
。截至 2023 年 9 月 28 日尚无进展。 - 2023 年 9 月 28 日,答案已更新,提供了更强大的解决方法,因此我打开了https://github.com/gpoore/fvextra/issues/18在
fvextra
repo 中查看是否会自动恢复带有fvextra
的 catcode 。 如果是这样,我建议将选项传递给环境,而不是总是将其删除并使用。&
mathescape
pygments
mathescape
Verbatim
codes={...}
- 2023 年 10 月 1 日,有条件应用补丁。