在 Minted、列表中的数学模式中,& 符号(用于矩阵对齐)仍然出现吗?

在 Minted、列表中的数学模式中,& 符号(用于矩阵对齐)仍然出现吗?

这是 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/1988pygmentsrepo 中建议添加\catcode`\&=4。截至 2023 年 9 月 28 日尚无进展。
  • 2023 年 9 月 28 日,答案已更新,提供了更强大的解决方法,因此我打开了https://github.com/gpoore/fvextra/issues/18fvextrarepo 中查看是否会自动恢复带有fvextra的 catcode 。 如果是这样,我建议将选项传递给环境,而不是总是将其删除并使用。&mathescape
    pygmentsmathescapeVerbatimcodes={...}
  • 2023 年 10 月 1 日,有条件应用补丁。

在此处输入图片描述

相关内容