3

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

enter image description here

instead of

enter image description here

in Matlab.

How can I change \lstset to make it work?

1

1 Answer 1

6

My recommendation would be to use the package matlab-prettifier, which is based on listings but provides enhanced features for MATLAB code above and beyond those provided by listings' Matlab language definition (including support for block comments):

\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}

enter image description here

If, for some reason, you must use listings with its existing implementation of the Matlab language, you can add support for block comments by setting the package's morecomment key:

morecomment=[s]{\%\{}{\%\}}

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.

\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}

enter image description here

3
  • Thank you very much! I will use the second solution, as I find hard to change the font and font size of matlab-prettifier to make it look like normal listings. But I keep in mind that this package is useful for Matlab code in Latex.
    – fidekild
    Commented Jun 9, 2016 at 9:17
  • 1
    @fidekild I'm the author of matlab-prettifier. Sadly, the API of the package isn't ideal, in part because it inherits some unfortunate (IMO) design decisions from the listings API. In order to change the font family and/or font size without losing existing settings, users have to reach into the package's internals, which is pretty bad. I've been playing with the idea of improving the API, but I'm also wary of making it too different from listings', which would be a barrier to adoption. Anyway, don't hesitate to ask another question about the problem you're having with the font.
    – jub0bs
    Commented Aug 6, 2016 at 15:19
  • 2
    @Jubobs Thank you very much for your explanation about your package. The solution of Paul Gessier is working very well in my case, but I thank you a lot for your very good package that I use in other Latex files now.
    – fidekild
    Commented Aug 16, 2016 at 15:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .