5

In Matlab there's two ways to use end:

  1. to end ifs, whiles etc.
  2. to refer to the last item in an array/vector (example: v(end) or v(end-1)).

I want to use listings to include some matlab code in my document, and I came across this answer explaining how to do it (including a very nice formatting). The problem is that the line emph=[1]{for,end,break},emphstyle=[1]\color{blue}, treats all ends equally, highlighting even when they shouldn't (case 2 above). In this very basic example:
enter image description here

My code would actually be a bit more complex but provided a valid solution is given I should be able to adapt it. Thanks a lot for your help!

Here's the MWE to the output above:

\documentclass{scrreprt}
\usepackage{color}
\usepackage{listings}
\usepackage{filecontents}

\begin{filecontents*}{foo.m}
for i=1:3
  x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
\end{filecontents*}


\definecolor{mygreen}{RGB}{28,172,0}
\definecolor{mylilas}{RGB}{170,55,241}
\lstset{language=Matlab,%
    %basicstyle=\color{red},
    breaklines=true,%
    morekeywords={matlab2tikz},
    keywordstyle=\color{black},%
    morekeywords=[2]{1}, keywordstyle=[2]{\color{black}},
    identifierstyle=\color{black},%
    stringstyle=\color{mylilas},
    commentstyle=\color{mygreen},%
    showstringspaces=false,%without this there will be a symbol in the places where there is a space
    numbers=left,%
    numberstyle={\tiny \color{black}},% size of the numbers
    numbersep=9pt, % this defines how far the numbers are from the text
    emph=[2]{(end)},emphstyle=[2]\color{black}, %doesn't work...
    emph=[1]{for,end,break},emphstyle=[1]\color{blue}, %some words to emphasise
    %emph=[2]{word1,word2}, emphstyle=[2]{style},    
}
\begin{document}
\lstinputlisting{foo.m}
\end{document}
  • This will be rather difficult because it requires parsing (is this statement closing a for/if/while block or not) instead of pattern matching (is this a known keyword with possibly a bit of local context). So I think it will not be possible. A small test of different editors and highlighters (gEdit, Geany and Pygments) also showed that none of them does it right, are you aware of any highlighter outside of Matlab/Octave itself that actually produces the desired behavior? – Marijn Mar 26 at 10:10
  • @Marijn I tried to define emph=[2]{(end)} for example (also in the code above), which should match only the 2nd "end", but I can't get it to override the blue highlighting. – Superuser27 Mar 26 at 10:14
6

Use matlab-prettifier package.

enter image description here

\documentclass{scrreprt}
%\usepackage{listings}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{filecontents}

\begin{filecontents*}{foo.m}
for i=1:3
  x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
\end{filecontents*}

\lstset{
  style              = Matlab-editor,
  basicstyle         = \mlttfamily,
  escapechar         = ",
  mlshowsectionrules = true,
}

\begin{document}
\lstinputlisting{foo.m}
\end{document}
  • The code doesn't compile: "Sorry, but miktex-makepk did not succeed. The log file hopefully contains the information to get MiKTeX going again: C:\Users[redacted]\AppData\Local\MiKTeX\2.9\miktex\log\miktex-makepk.log Process exited with error(s)" – Superuser27 Mar 26 at 10:28
  • Why did you comment out the \usepackage{listings}? – Superuser27 Mar 26 at 10:29
  • @Superuser27 are you using pdflatex to compile? – nidhin Mar 26 at 10:48
  • 2
    @Superuser27 matlab-prettifier is built on top of listings. So need not load it again. That’s why I commented it. – nidhin Mar 26 at 11:02
  • yes I'm using pdflatex – Superuser27 Mar 26 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.