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
In Matlab there's two ways to use end:
- to end
ifs,whiles etc. - to refer to the last item in an array/vector (example:
v(end)orv(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:
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
add a comment |
In Matlab there's two ways to use end:
- to end
ifs,whiles etc. - to refer to the last item in an array/vector (example:
v(end)orv(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:
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
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 defineemph=[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
add a comment |
In Matlab there's two ways to use end:
- to end
ifs,whiles etc. - to refer to the last item in an array/vector (example:
v(end)orv(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:
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
In Matlab there's two ways to use end:
- to end
ifs,whiles etc. - to refer to the last item in an array/vector (example:
v(end)orv(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:
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
formatting listings code matlab
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 defineemph=[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
add a comment |
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 defineemph=[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
add a comment |
1 Answer
1
active
oldest
votes
Use matlab-prettifier package.

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
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 theusepackagelistings?
– Superuser27
yesterday
@Superuser27 are you using pdflatex to compile?
– nidhin
yesterday
2
@Superuser27matlab-prettifieris built on top oflistings. So need not load it again. That’s why I commented it.
– nidhin
yesterday
yes I'm using pdflatex
– Superuser27
yesterday
|
show 2 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Use matlab-prettifier package.

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
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 theusepackagelistings?
– Superuser27
yesterday
@Superuser27 are you using pdflatex to compile?
– nidhin
yesterday
2
@Superuser27matlab-prettifieris built on top oflistings. So need not load it again. That’s why I commented it.
– nidhin
yesterday
yes I'm using pdflatex
– Superuser27
yesterday
|
show 2 more comments
Use matlab-prettifier package.

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
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 theusepackagelistings?
– Superuser27
yesterday
@Superuser27 are you using pdflatex to compile?
– nidhin
yesterday
2
@Superuser27matlab-prettifieris built on top oflistings. So need not load it again. That’s why I commented it.
– nidhin
yesterday
yes I'm using pdflatex
– Superuser27
yesterday
|
show 2 more comments
Use matlab-prettifier package.

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
Use matlab-prettifier package.

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
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 theusepackagelistings?
– Superuser27
yesterday
@Superuser27 are you using pdflatex to compile?
– nidhin
yesterday
2
@Superuser27matlab-prettifieris built on top oflistings. So need not load it again. That’s why I commented it.
– nidhin
yesterday
yes I'm using pdflatex
– Superuser27
yesterday
|
show 2 more comments
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 theusepackagelistings?
– Superuser27
yesterday
@Superuser27 are you using pdflatex to compile?
– nidhin
yesterday
2
@Superuser27matlab-prettifieris built on top oflistings. 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
|
show 2 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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