![平均能量损失](https://linux22.com/image/300408/%E5%B9%B3%E5%9D%87%E8%83%BD%E9%87%8F%E6%8D%9F%E5%A4%B1.png)
我正在使用包排版我的 MATLAB 代码matlab-prettifier
,本质上是listing
。但是,有时我不需要格式化。我只想要纯粹的未格式化的文本。
例如,在下面的 MWE 中,单词 for 被误认为是 MATLAB 命令,因此以蓝色排版。如何让此行以纯文本、无格式显示?同时,我不希望丢失其他真实 MATLAB 命令的 MATLAB 格式。
平均能量损失
\documentclass[12pt]{article}
% For MATLAB code
\usepackage[framed]{matlab-prettifier}
\usepackage[T1]{fontenc}
\begin{document}
\begin{lstlisting}[mathescape=true,style=Matlab-editor,basicstyle=\mlttfamily\footnotesize]
>> str = 'I am doing this for nothing';
>> str
I am doing this for nothing.
\end{lstlisting}
\end{document}
输出
答案1
免责声明
接下来是一个糟糕且可能效率低下的黑客行为(matlab-prettifier
实际上就像大多数的代码一样)。一个半体面的实现需要更仔细的思考。使用风险自负;你已被警告:)
特别是,这种方法有一个很大的警告:多行命令的第二行和后续行根本不会被突出显示。
\documentclass[12pt]{article}
% For MATLAB code
\usepackage[framed]{matlab-prettifier}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\makeatletter
% switch indicating whether a prompt was detected on the current line
\newif\ifprompt@mlpr@
% set the switch if a prompt was detected on the current line
\lstset{literate={>>}{{\processPrompt@mlpr}}2}
\newcommand\processPrompt@mlpr{%
>{}>%
\ifnum\lst@mode=\lst@Pmode\relax%
\global\prompt@mlpr@true%
\fi
}
% disable highlighting if a prompt was detected on the current line
\lst@AddToHook{Output}{\condDisableHiliting}
\lst@AddToHook{OutputOther}{\condDisableHiliting}
\newcommand\condDisableHiliting{%
\ifprompt@mlpr@%
\else
\let\lst@thestyle\lst@basicstyle%
\fi
}
% reset the switch at the beginning of each "real" line
\lst@AddToHook{EveryPar}{\global\prompt@mlpr@false}
\makeatother
\begin{document}
\begin{lstlisting}[%
mathescape = true,
style = Matlab-editor,
basicstyle = \mlttfamily\footnotesize,
]
>> str = 'I am doing this for nothing';
>> str
I am doing this for nothing.
\end{lstlisting}
\end{document}
答案2
您可以将颜色设置为黑色,如下所示:
\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{escapeinside={<@}{@>}}
% For MATLAB code
\usepackage[framed]{matlab-prettifier}
\usepackage[T1]{fontenc}
\begin{document}
\begin{lstlisting}[mathescape=true,style=Matlab-editor,basicstyle=\mlttfamily\footnotesize]
>> str = 'I am doing this for nothing';
>> str
<@\textcolor{black}{I am doing this for nothing.}@>
\end{lstlisting}
\end{document}
这有点迂回,但我不知道您如何在 lstlisting 环境中转义文本。
答案3
您可以考虑简单地将选项添加mlkeywordstyle=\color{black}
到lstlisting
环境中。例如
\begin{lstlisting}[mathescape=true, style=Matlab-editor, basicstyle=\mlttfamily\footnotesize, mlkeywordstyle=\color{black}]
>> str = 'I am doing this for nothing';
>> str
I am doing this for nothing.
\end{lstlisting}