The listings package is a great tool to typeset source code beautifully and according to one’s personal taste due to a range of customizable settings. Among the many languages supported off the shelf are MATLAB and its free software sibling GNU Octave. Unfortunately the latest version 1.4 of the listings package dates back to February 2007 which might be the reason why block comments for MATLAB and Octave are not supported by default. Let’s say your code contains constructs like this:
%{ This is a multi-line comment in the form of a block comment. %}
You might be surprised to find out that while the first and last line of the snippet are properly styled as comments (because of the leading percentage sign), the two lines in the middle are not. The problem here is that the language definitions in the file lstlang1.sty are missing the entry morecomment=[s]{\%\{}{\%\}}
. (I’ve added the color-coding simply to make this mess of braces and backslashes more humanly parseable.)
So how do you get block comments to work? Well, first of all you could locate lstlang1.sty and add the morecomment entry to the MATLAB and Octave language definitions to enable block comments throughout your LaTeX installation.
However, you may not want to modify the “official” file, for instance if you’re sending your LaTeX sources to other people which would then have to make those same modifications or get a different output. Being limited to modifying your own files, you could also put these lines into the preamble of your document:
\lstdefinelanguage{Matlab}{morecomment=[s]{\%\{}{\%\}}} \lstdefinelanguage{Octave}{morecomment=[s]{\%\{}{\%\}}}
While this works insofar as it does give you block comments, it somehow messes up other parts like keyword emphasis—not a viable alternative. There’s one more easy, global solution in adding the following to your document’s preamble:
\lstset{language=Matlab} % or language=Octave \lstset{morecomment=[s]{\%\{}{\%\}}}
The downside of this approach is that it only works if all listings throughout the document are using the same language, i.e. MATLAB. As soon as you specify a different language in e.g. a lstlisting environment, block comments will no longer work.
I know of one more global if somewhat clumsy solution: You could copy-and-paste the MATLAB and/or Octave language definitions from lstlang1.sty to the preamble of your document. There you can then add the morecomment line from above to enable block comments. This approach works globally, allows for various languages used throughout your document, requires no modification of “system” files, and doesn’t mess up anything—well, except for your preamble that now contains a couple dozen lines of language definition, but that’s mostly a problem from an aesthetics standpoint.
Finally, my preferred method. This one is only a one-liner, but it comes at the expense of being required for every listing that utilizes block comments. Let’s say you want to include an external MATLAB/Octave source file into your LaTeX document using \lstinputlisting
. This command allows you to specify options particular to this one listing by putting them in square brackets right after the command name. My initial approach was to write
\lstinputlisting[language=Octave, morecomment=[s]{\%\{}{\%\}}]{filename.m}
Unfortunately this didn’t work as LaTeX failed to understand the nested square brackets. First I thought that this problem would render this solution impossible. Later though, I remembered to put curly braces around the whole argument of the morecomment parameter and voilà, it worked. So instead of the above, write
\lstinputlisting[language=Octave, morecomment={[s]{\%\{}{\%\}}}]{filename.m}
This will give you proper block comment styling for the included source file filename.m. The same method works for lstlisting environments or \lstinputlisting
commands. Happy block-commenting!