I am using the package listings to include some Matlab code in my report.
It works well with comments beginning by %
but does not recognise multiline comments %{ ... %}
:
\usepackage{listings}
\lstset{language=Matlab}
...
\begin{lstlisting}
% normal comment
MATLAB code
%{
This is
a multiline
comment
%}
function [x,y] = test(x)
\end{lstlisting}
gives
instead of
in Matlab.
How can I change \lstset
to make it work?
Best Answer
My recommendation would be to use the package
matlab-prettifier
, which is based onlistings
but provides enhanced features for MATLAB code above and beyond those provided bylistings
'Matlab
language definition (including support for block comments):If, for some reason, you must use
listings
with its existing implementation of theMatlab
language, you can add support for block comments by setting the package'smorecomment
key:Here,
[s]
signifies that we are looking for two delimiters, the first to open a block comment and the second to close it. The following brace groups contain the opening and closing delimiters for a block comment, respectively. Note that both the percent sign and the individual open/close braces must be escaped by backslashes when defining the comment delimiters.