![套件清單中的多行 Matlab 註釋](https://rvso.com/image/335504/%E5%A5%97%E4%BB%B6%E6%B8%85%E5%96%AE%E4%B8%AD%E7%9A%84%E5%A4%9A%E8%A1%8C%20Matlab%20%E8%A8%BB%E9%87%8B.png)
答案1
我的建議是使用該套件matlab-prettifier
,它基於'語言定義listings
,但為 MATLAB 程式碼提供了超出這些功能的增強功能(包括對區塊註解的支援):listings
Matlab
\documentclass{article}
\usepackage{matlab-prettifier}
\lstset{style=Matlab-editor}
\begin{document}
\begin{lstlisting}
% normal comment
MATLAB code
%{
This is
a multiline
comment
%}
function [x,y] = test(x)
\end{lstlisting}
\end{document}
如果由於某種原因,您必須使用listings
該語言的現有實現Matlab
,則可以透過設定包的morecomment
鍵來添加對區塊註釋的支援:
morecomment=[s]{\%\{}{\%\}}
在這裡,[s]
表示我們正在尋找兩個分隔符,第一個用於打開區塊註釋,第二個用於關閉它。以下大括號組分別包含區塊註解的開始和結束分隔符號。請注意,在定義註解分隔符號時,百分號和單獨的開/閉大括號都必須用反斜線轉義。
\documentclass{article}
\usepackage{listings}
\lstset{
language=Matlab,
basicstyle=\ttfamily,
morecomment=[s]{\%\{}{\%\}},
}
\begin{document}
\begin{lstlisting}
% normal comment
MATLAB code
%{
This is
a multiline
comment
%}
function [x,y] = test(x)
\end{lstlisting}
\end{document}