Listings color context sensitive keywords (Matlab)
In Matlab there's two ways to use end
:
- to end
if
s,while
s 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]color{blue},
treats all end
s 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:
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
add a comment |
In Matlab there's two ways to use end
:
- to end
if
s,while
s 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]color{blue},
treats all end
s 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:
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
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
18 hours ago
@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
18 hours ago
add a comment |
In Matlab there's two ways to use end
:
- to end
if
s,while
s 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]color{blue},
treats all end
s 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:
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
In Matlab there's two ways to use end
:
- to end
if
s,while
s 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]color{blue},
treats all end
s 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:
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
formatting listings code matlab
asked 18 hours ago
Superuser27Superuser27
78816
78816
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
18 hours ago
@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
18 hours ago
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
18 hours ago
@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
18 hours 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
18 hours 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
18 hours 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
18 hours 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
18 hours ago
add a comment |
1 Answer
1
active
oldest
votes
Use matlab-prettifier
package.
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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"
– Superuser27
18 hours ago
Why did you comment out theusepackage{listings}
?
– Superuser27
18 hours ago
@Superuser27 are you using pdflatex to compile?
– nidhin
17 hours ago
2
@Superuser27matlab-prettifier
is built on top oflistings
. So need not load it again. That’s why I commented it.
– nidhin
17 hours ago
yes I'm using pdflatex
– Superuser27
16 hours ago
|
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.
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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"
– Superuser27
18 hours ago
Why did you comment out theusepackage{listings}
?
– Superuser27
18 hours ago
@Superuser27 are you using pdflatex to compile?
– nidhin
17 hours ago
2
@Superuser27matlab-prettifier
is built on top oflistings
. So need not load it again. That’s why I commented it.
– nidhin
17 hours ago
yes I'm using pdflatex
– Superuser27
16 hours ago
|
show 2 more comments
Use matlab-prettifier
package.
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]AppDataLocalMiKTeX2.9miktexlogmiktex-makepk.log Process exited with error(s)"
– Superuser27
18 hours ago
Why did you comment out theusepackage{listings}
?
– Superuser27
18 hours ago
@Superuser27 are you using pdflatex to compile?
– nidhin
17 hours ago
2
@Superuser27matlab-prettifier
is built on top oflistings
. So need not load it again. That’s why I commented it.
– nidhin
17 hours ago
yes I'm using pdflatex
– Superuser27
16 hours ago
|
show 2 more comments
Use matlab-prettifier
package.
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}
Use matlab-prettifier
package.
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}
answered 18 hours 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
18 hours ago
Why did you comment out theusepackage{listings}
?
– Superuser27
18 hours ago
@Superuser27 are you using pdflatex to compile?
– nidhin
17 hours ago
2
@Superuser27matlab-prettifier
is built on top oflistings
. So need not load it again. That’s why I commented it.
– nidhin
17 hours ago
yes I'm using pdflatex
– Superuser27
16 hours ago
|
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
18 hours ago
Why did you comment out theusepackage{listings}
?
– Superuser27
18 hours ago
@Superuser27 are you using pdflatex to compile?
– nidhin
17 hours ago
2
@Superuser27matlab-prettifier
is built on top oflistings
. So need not load it again. That’s why I commented it.
– nidhin
17 hours ago
yes I'm using pdflatex
– Superuser27
16 hours 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
18 hours 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
18 hours ago
Why did you comment out the
usepackage{listings}
?– Superuser27
18 hours ago
Why did you comment out the
usepackage{listings}
?– Superuser27
18 hours ago
@Superuser27 are you using pdflatex to compile?
– nidhin
17 hours ago
@Superuser27 are you using pdflatex to compile?
– nidhin
17 hours 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
17 hours ago
@Superuser27
matlab-prettifier
is built on top of listings
. So need not load it again. That’s why I commented it.– nidhin
17 hours ago
yes I'm using pdflatex
– Superuser27
16 hours ago
yes I'm using pdflatex
– Superuser27
16 hours ago
|
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
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
18 hours 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
18 hours ago