Listings color context sensitive keywords (Matlab)What can I use to typeset MATLAB code in my document?listings incorrectly highlights PHP keywordsForbidding keywords in listingsListings: color numbers only out of keywordsListings - two word keywordsDefining keywords for SQL using listingsHow can I avoid the use of default styles when no listings language is set?Multiline Matlab comments in package ListingsLiterating in listings with and without star at the same timelstnewenvironment/lstset : using colors conditionally emph/emphstyle - SAS: (in=_x) versus if x in (1,2)Avoid highlighting keywords following certain words in listings

How do I extract a value from a time formatted value in excel?

How to safely derail a train during transit?

How long to clear the 'suck zone' of a turbofan after start is initiated?

How do I find the solutions of the following equation?

Why escape if the_content isnt?

when is out of tune ok?

Anatomically Correct Strange Women In Ponds Distributing Swords

Would a high gravity rocky planet be guaranteed to have an atmosphere?

Pole-zeros of a real-valued causal FIR system

CREATE opcode: what does it really do?

Is the destination of a commercial flight important for the pilot?

Lay out the Carpet

Why not increase contact surface when reentering the atmosphere?

What does "I’d sit this one out, Cap," imply or mean in the context?

Are student evaluations of teaching assistants read by others in the faculty?

Opposite of a diet

How does buying out courses with grant money work?

Two monoidal structures and copowering

How to be diplomatic in refusing to write code that breaches the privacy of our users

Method to test if a number is a perfect power?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Gears on left are inverse to gears on right?

Is this apparent Class Action settlement a spam message?

Closest Prime Number



Listings color context sensitive keywords (Matlab)


What can I use to typeset MATLAB code in my document?listings incorrectly highlights PHP keywordsForbidding keywords in listingsListings: color numbers only out of keywordsListings - two word keywordsDefining keywords for SQL using listingsHow can I avoid the use of default styles when no listings language is set?Multiline Matlab comments in package ListingsLiterating in listings with and without star at the same timelstnewenvironment/lstset : using colors conditionally emph/emphstyle - SAS: (in=_x) versus if x in (1,2)Avoid highlighting keywords following certain words in listings













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]colorblue, 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:



documentclassscrreprt
usepackagecolor
usepackagelistings
usepackagefilecontents

beginfilecontents*foo.m
for i=1:3
x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
endfilecontents*


definecolormygreenRGB28,172,0
definecolormylilasRGB170,55,241
lstsetlanguage=Matlab,%
%basicstyle=colorred,
breaklines=true,%
morekeywords=matlab2tikz,
keywordstyle=colorblack,%
morekeywords=[2]1, keywordstyle=[2]colorblack,
identifierstyle=colorblack,%
stringstyle=colormylilas,
commentstyle=colormygreen,%
showstringspaces=false,%without this there will be a symbol in the places where there is a space
numbers=left,%
numberstyle=tiny colorblack,% size of the numbers
numbersep=9pt, % this defines how far the numbers are from the text
emph=[2](end),emphstyle=[2]colorblack, %doesn't work...
emph=[1]for,end,break,emphstyle=[1]colorblue, %some words to emphasise
%emph=[2]word1,word2, emphstyle=[2]style,

begindocument
lstinputlistingfoo.m
enddocument









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
    yesterday











  • @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
    yesterday
















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]colorblue, 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:



documentclassscrreprt
usepackagecolor
usepackagelistings
usepackagefilecontents

beginfilecontents*foo.m
for i=1:3
x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
endfilecontents*


definecolormygreenRGB28,172,0
definecolormylilasRGB170,55,241
lstsetlanguage=Matlab,%
%basicstyle=colorred,
breaklines=true,%
morekeywords=matlab2tikz,
keywordstyle=colorblack,%
morekeywords=[2]1, keywordstyle=[2]colorblack,
identifierstyle=colorblack,%
stringstyle=colormylilas,
commentstyle=colormygreen,%
showstringspaces=false,%without this there will be a symbol in the places where there is a space
numbers=left,%
numberstyle=tiny colorblack,% size of the numbers
numbersep=9pt, % this defines how far the numbers are from the text
emph=[2](end),emphstyle=[2]colorblack, %doesn't work...
emph=[1]for,end,break,emphstyle=[1]colorblue, %some words to emphasise
%emph=[2]word1,word2, emphstyle=[2]style,

begindocument
lstinputlistingfoo.m
enddocument









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
    yesterday











  • @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
    yesterday














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]colorblue, 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:



documentclassscrreprt
usepackagecolor
usepackagelistings
usepackagefilecontents

beginfilecontents*foo.m
for i=1:3
x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
endfilecontents*


definecolormygreenRGB28,172,0
definecolormylilasRGB170,55,241
lstsetlanguage=Matlab,%
%basicstyle=colorred,
breaklines=true,%
morekeywords=matlab2tikz,
keywordstyle=colorblack,%
morekeywords=[2]1, keywordstyle=[2]colorblack,
identifierstyle=colorblack,%
stringstyle=colormylilas,
commentstyle=colormygreen,%
showstringspaces=false,%without this there will be a symbol in the places where there is a space
numbers=left,%
numberstyle=tiny colorblack,% size of the numbers
numbersep=9pt, % this defines how far the numbers are from the text
emph=[2](end),emphstyle=[2]colorblack, %doesn't work...
emph=[1]for,end,break,emphstyle=[1]colorblue, %some words to emphasise
%emph=[2]word1,word2, emphstyle=[2]style,

begindocument
lstinputlistingfoo.m
enddocument









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]colorblue, 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:



documentclassscrreprt
usepackagecolor
usepackagelistings
usepackagefilecontents

beginfilecontents*foo.m
for i=1:3
x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
endfilecontents*


definecolormygreenRGB28,172,0
definecolormylilasRGB170,55,241
lstsetlanguage=Matlab,%
%basicstyle=colorred,
breaklines=true,%
morekeywords=matlab2tikz,
keywordstyle=colorblack,%
morekeywords=[2]1, keywordstyle=[2]colorblack,
identifierstyle=colorblack,%
stringstyle=colormylilas,
commentstyle=colormygreen,%
showstringspaces=false,%without this there will be a symbol in the places where there is a space
numbers=left,%
numberstyle=tiny colorblack,% size of the numbers
numbersep=9pt, % this defines how far the numbers are from the text
emph=[2](end),emphstyle=[2]colorblack, %doesn't work...
emph=[1]for,end,break,emphstyle=[1]colorblue, %some words to emphasise
%emph=[2]word1,word2, emphstyle=[2]style,

begindocument
lstinputlistingfoo.m
enddocument






formatting listings code matlab






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









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
    yesterday











  • @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
    yesterday


















  • 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
    yesterday











  • @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
    yesterday

















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
yesterday





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
yesterday













@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
yesterday






@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
yesterday











1 Answer
1






active

oldest

votes


















6














Use matlab-prettifier package.



enter image description here



documentclassscrreprt
%usepackagelistings
usepackage[numbered,framed]matlab-prettifier
usepackagefilecontents

beginfilecontents*foo.m
for i=1:3
x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
endfilecontents*

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


begindocument
lstinputlistingfoo.m
enddocument





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
    yesterday











  • Why did you comment out the usepackagelistings?

    – Superuser27
    yesterday











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    yesterday






  • 2





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

    – nidhin
    yesterday











  • yes I'm using pdflatex

    – Superuser27
    yesterday










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



documentclassscrreprt
%usepackagelistings
usepackage[numbered,framed]matlab-prettifier
usepackagefilecontents

beginfilecontents*foo.m
for i=1:3
x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
endfilecontents*

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


begindocument
lstinputlistingfoo.m
enddocument





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
    yesterday











  • Why did you comment out the usepackagelistings?

    – Superuser27
    yesterday











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    yesterday






  • 2





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

    – nidhin
    yesterday











  • yes I'm using pdflatex

    – Superuser27
    yesterday















6














Use matlab-prettifier package.



enter image description here



documentclassscrreprt
%usepackagelistings
usepackage[numbered,framed]matlab-prettifier
usepackagefilecontents

beginfilecontents*foo.m
for i=1:3
x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
endfilecontents*

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


begindocument
lstinputlistingfoo.m
enddocument





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
    yesterday











  • Why did you comment out the usepackagelistings?

    – Superuser27
    yesterday











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    yesterday






  • 2





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

    – nidhin
    yesterday











  • yes I'm using pdflatex

    – Superuser27
    yesterday













6












6








6







Use matlab-prettifier package.



enter image description here



documentclassscrreprt
%usepackagelistings
usepackage[numbered,framed]matlab-prettifier
usepackagefilecontents

beginfilecontents*foo.m
for i=1:3
x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
endfilecontents*

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


begindocument
lstinputlistingfoo.m
enddocument





share|improve this answer













Use matlab-prettifier package.



enter image description here



documentclassscrreprt
%usepackagelistings
usepackage[numbered,framed]matlab-prettifier
usepackagefilecontents

beginfilecontents*foo.m
for i=1:3
x(i)=i;
end % this should be blue

y = x(end); % this shouldn't
endfilecontents*

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


begindocument
lstinputlistingfoo.m
enddocument






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









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
    yesterday











  • Why did you comment out the usepackagelistings?

    – Superuser27
    yesterday











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    yesterday






  • 2





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

    – nidhin
    yesterday











  • yes I'm using pdflatex

    – Superuser27
    yesterday

















  • 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
    yesterday











  • Why did you comment out the usepackagelistings?

    – Superuser27
    yesterday











  • @Superuser27 are you using pdflatex to compile?

    – nidhin
    yesterday






  • 2





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

    – nidhin
    yesterday











  • yes I'm using pdflatex

    – Superuser27
    yesterday
















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
yesterday





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
yesterday













Why did you comment out the usepackagelistings?

– Superuser27
yesterday





Why did you comment out the usepackagelistings?

– Superuser27
yesterday













@Superuser27 are you using pdflatex to compile?

– nidhin
yesterday





@Superuser27 are you using pdflatex to compile?

– nidhin
yesterday




2




2





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

– nidhin
yesterday





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

– nidhin
yesterday













yes I'm using pdflatex

– Superuser27
yesterday





yes I'm using pdflatex

– Superuser27
yesterday

















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







-code, formatting, listings, matlab

Popular posts from this blog

Creating 100m^2 grid automatically using QGIS?Creating grid constrained within polygon in QGIS?Createing polygon layer from point data using QGIS?Creating vector grid using QGIS?Creating grid polygons from coordinates using R or PythonCreating grid from spatio temporal point data?Creating fields in attributes table using other layers using QGISCreate .shp vector grid in QGISQGIS Creating 4km point grid within polygonsCreate a vector grid over a raster layerVector Grid Creates just one grid

What is this called? Old film camera viewer?What makes a good film camera?What to do with an old film camera?What should one look for when buying a used film camera?What is the value and age of this pre-1967 Ricoh 35 mm camera?DSLR recommendation, question about old Canon 35mm film Camera & lensesCan anyone identify the silver rangefinder-style camera in this advertisement?What kind of a Polaroid 600-camera is this?Will an old film camera still work even when not used in a very long time?What is this camera / Can I develop the film?How to fit an action camera into antique (bellows) housing?What to check when buying used and old film bodies?

Why is this plane circling around the Lucknow airport every day?Why do aircraft on Flight Radar 24 jump around randomly sometimes?What airport has this walkway over a taxiway?How does Chicago O'Hare's tower sequence aircraft at peak capacity?Which airport is featured in this Delta commercial?After a crash, for how long is the airport closed?Can a passenger plane stand still in the air, or hover at a fixed location above a ground?What are those trucks towing around, and why?What is this airport outside of Cairo, Egypt?Which US airport has the lowest circling MDH?What is this airport video?