Listings color context sensitive keywords (Matlab)












4















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}









share|improve this question























  • 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
    2 days ago











  • @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
    2 days ago


















4















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}









share|improve this question























  • 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
    2 days ago











  • @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
    2 days ago
















4












4








4








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}









share|improve this question














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}






formatting listings code matlab






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Superuser27Superuser27

86816




86816













  • 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
    2 days ago











  • @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
    2 days ago





















  • 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
    2 days ago











  • @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
    2 days ago



















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
2 days ago





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
2 days ago













@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
2 days ago







@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
2 days ago












1 Answer
1






active

oldest

votes


















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}





share|improve this answer
























  • 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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"

    – Superuser27
    2 days ago











  • Why did you comment out the usepackage{listings}?

    – Superuser27
    2 days ago











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    2 days ago






  • 2





    @Superuser27 matlab-prettifier is built on top of listings. So need not load it again. That’s why I commented it.

    – nidhin
    2 days ago











  • yes I'm using pdflatex

    – Superuser27
    2 days ago












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f481490%2flistings-color-context-sensitive-keywords-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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}





share|improve this answer
























  • 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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"

    – Superuser27
    2 days ago











  • Why did you comment out the usepackage{listings}?

    – Superuser27
    2 days ago











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    2 days ago






  • 2





    @Superuser27 matlab-prettifier is built on top of listings. So need not load it again. That’s why I commented it.

    – nidhin
    2 days ago











  • yes I'm using pdflatex

    – Superuser27
    2 days ago
















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}





share|improve this answer
























  • 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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"

    – Superuser27
    2 days ago











  • Why did you comment out the usepackage{listings}?

    – Superuser27
    2 days ago











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    2 days ago






  • 2





    @Superuser27 matlab-prettifier is built on top of listings. So need not load it again. That’s why I commented it.

    – nidhin
    2 days ago











  • yes I'm using pdflatex

    – Superuser27
    2 days ago














6












6








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}





share|improve this answer













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}






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









nidhinnidhin

3,5821927




3,5821927













  • 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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"

    – Superuser27
    2 days ago











  • Why did you comment out the usepackage{listings}?

    – Superuser27
    2 days ago











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    2 days ago






  • 2





    @Superuser27 matlab-prettifier is built on top of listings. So need not load it again. That’s why I commented it.

    – nidhin
    2 days ago











  • yes I'm using pdflatex

    – Superuser27
    2 days ago



















  • 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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"

    – Superuser27
    2 days ago











  • Why did you comment out the usepackage{listings}?

    – Superuser27
    2 days ago











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    2 days ago






  • 2





    @Superuser27 matlab-prettifier is built on top of listings. So need not load it again. That’s why I commented it.

    – nidhin
    2 days ago











  • yes I'm using pdflatex

    – Superuser27
    2 days ago

















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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"

– Superuser27
2 days ago





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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"

– Superuser27
2 days ago













Why did you comment out the usepackage{listings}?

– Superuser27
2 days ago





Why did you comment out the usepackage{listings}?

– Superuser27
2 days ago













@Superuser27 are you using pdflatex to compile?

– nidhin
2 days ago





@Superuser27 are you using pdflatex to compile?

– nidhin
2 days ago




2




2





@Superuser27 matlab-prettifier is built on top of listings. So need not load it again. That’s why I commented it.

– nidhin
2 days ago





@Superuser27 matlab-prettifier is built on top of listings. So need not load it again. That’s why I commented it.

– nidhin
2 days ago













yes I'm using pdflatex

– Superuser27
2 days ago





yes I'm using pdflatex

– Superuser27
2 days ago


















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f481490%2flistings-color-context-sensitive-keywords-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

數位音樂下載

When can things happen in Etherscan, such as the picture below?

格利澤436b