Add keywords to an existing language in listingsExtend a language with additional keywords?Listings Highlight...
Memory usage: #define vs. static const for uint8_t
Closed set in topological space generated by sets of the form [a, b).
Reading Mishnayos without understanding
Sharepoint metadata URL
Does Skippy chunky peanut butter contain trans fat?
Possible issue with my W4 and tax return
Does it take energy to move something in a circle?
Not a Long-Winded Riddle
Could a warlock use the One with Shadows warlock invocation to turn invisible, and then move while staying invisible?
How can I prevent an oracle who can see into the past from knowing everything that has happened?
What is a good reason for every spaceship to carry a weapon on board?
Why did Luke use his left hand to shoot?
Book where a space ship journeys to the center of the galaxy to find all the stars had gone supernova
Converting very wide logos to square formats
How much light is too much?
Stuck on a Geometry Puzzle
How to create a label containing values from different layers in QGIS
Does the US government have any planning in place to ensure there's no shortages of food, fuel, steel and other commodities?
What makes papers publishable in top-tier journals?
How to not let the Identify spell spoil everything?
Why is that max-Q doesn't occur in transonic regime?
Is there a file that always exists and a 'normal' user can't lstat it?
Why avoid shared user accounts?
I have trouble understanding this fallacy: "If A, then B. Therefore if not-B, then not-A."
Add keywords to an existing language in listings
Extend a language with additional keywords?Listings Highlight Java AnnotationsAdding Keywords to Existing Language for Listings PackageExtend a language with additional keywords?mark LaTeX keywords in listingslistings: General settings for a language seem to be overwritten/not respectedHow can I highlight an interactive GAP session just like so?Customize Color for lstset morekeywordsAdding Keywords to Existing Language for Listings Packagekeywords in other languagelistings keywords highlighting with underscoremultiple classes not working with otherkeywords in listings packagelstnewenvironment/lstset : using colors conditionally emph/emphstyle - SAS: (in=_x) versus if x in (1,2)
I've already read extending a language with additional keywords and I have found kind of a solution, but I'm not quite sure if it's the right way and if I won't have problems in the future.
The scenario is like this: I have different pieces of code in ruby, Java and a custom one, in a random order (not all ruby code first, then all Java, etc).
Here is a MWE to illustrate:
documentclass[11pt]{report}
usepackage{listings}
usepackage{framed}
usepackage{xcolor}
lstset{ %
basicstyle=footnotesizettfamily,
numbers=left,
numberstyle=tinycolor{gray}ttfamily,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=color{black},
captionpos=b,
keywordstyle=color{blue}bf,
commentstyle=color{gray},
stringstyle=color{green},
keywordstyle={[2]color{red}bf},
}
lstdefinelanguage{story}
{morekeywords={Given,When,Then},
sensitive=false,
morekeywords={[2]Scenario},
}
begin{document}
First some ruby code
lstinputlisting[language=Ruby,caption={Ruby}]{ruby_code.rb}
Now some java code
%lstset{language=Java}
%lstset{ morekeywords={[2]@Given,@When,@Then}}
lstinputlisting[language=Java,caption={Java}]{JavaCode.java}
Now some story code
lstinputlisting[language=story,caption={Story}]{story_code.story}
end{document}
I'll put only the java code which is the one troubling and the one I really care:
// JavaCode.java
public class ComportamientoSteps {
Croupier croupier;
@Given("una nueva mano")
public void nuevaMano() {
croupier = new Croupier();
}
@When("el croupier tiene $carta1 y $carta2")
public void elCroupierTiene(int carta1, int carta2) {
croupier.agregarCarta(new Carta(carta1));
croupier.agregarCarta(new Carta(carta2));
}
@Then("$valor deberia plantar")
public void croupierDeberiaPlantar(String valor) {
boolean debe_pedir = valor.equals("no");
assertThat(croupier.debePedirOtraCarta(), equalTo(debe_pedir));
}
}
What I'm trying to do is:
- First, define a common style for al pieces of code: size, colors of
keywords
and[2]keywords
, etc. I want this style for every language. - Then define the very simple story language.
- Lastly, I input the code via
lstinputlisting[language=whatever]{file}
I want to extend the Java language to interpret @Given
, @When
and @Then
as [2]keywords
. In the MWE there is a piece of code commented out, with that piece of code it works (you have to remove the language=Java
in the options), but I don't want to do that every time I have to put Java code, I just want to use the lstinputlisting
with the language
option. Besides, I don't know if setting the language to Java like that won't bring me trouble in future listings.
listings languages keywords
add a comment |
I've already read extending a language with additional keywords and I have found kind of a solution, but I'm not quite sure if it's the right way and if I won't have problems in the future.
The scenario is like this: I have different pieces of code in ruby, Java and a custom one, in a random order (not all ruby code first, then all Java, etc).
Here is a MWE to illustrate:
documentclass[11pt]{report}
usepackage{listings}
usepackage{framed}
usepackage{xcolor}
lstset{ %
basicstyle=footnotesizettfamily,
numbers=left,
numberstyle=tinycolor{gray}ttfamily,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=color{black},
captionpos=b,
keywordstyle=color{blue}bf,
commentstyle=color{gray},
stringstyle=color{green},
keywordstyle={[2]color{red}bf},
}
lstdefinelanguage{story}
{morekeywords={Given,When,Then},
sensitive=false,
morekeywords={[2]Scenario},
}
begin{document}
First some ruby code
lstinputlisting[language=Ruby,caption={Ruby}]{ruby_code.rb}
Now some java code
%lstset{language=Java}
%lstset{ morekeywords={[2]@Given,@When,@Then}}
lstinputlisting[language=Java,caption={Java}]{JavaCode.java}
Now some story code
lstinputlisting[language=story,caption={Story}]{story_code.story}
end{document}
I'll put only the java code which is the one troubling and the one I really care:
// JavaCode.java
public class ComportamientoSteps {
Croupier croupier;
@Given("una nueva mano")
public void nuevaMano() {
croupier = new Croupier();
}
@When("el croupier tiene $carta1 y $carta2")
public void elCroupierTiene(int carta1, int carta2) {
croupier.agregarCarta(new Carta(carta1));
croupier.agregarCarta(new Carta(carta2));
}
@Then("$valor deberia plantar")
public void croupierDeberiaPlantar(String valor) {
boolean debe_pedir = valor.equals("no");
assertThat(croupier.debePedirOtraCarta(), equalTo(debe_pedir));
}
}
What I'm trying to do is:
- First, define a common style for al pieces of code: size, colors of
keywords
and[2]keywords
, etc. I want this style for every language. - Then define the very simple story language.
- Lastly, I input the code via
lstinputlisting[language=whatever]{file}
I want to extend the Java language to interpret @Given
, @When
and @Then
as [2]keywords
. In the MWE there is a piece of code commented out, with that piece of code it works (you have to remove the language=Java
in the options), but I don't want to do that every time I have to put Java code, I just want to use the lstinputlisting
with the language
option. Besides, I don't know if setting the language to Java like that won't bring me trouble in future listings.
listings languages keywords
add a comment |
I've already read extending a language with additional keywords and I have found kind of a solution, but I'm not quite sure if it's the right way and if I won't have problems in the future.
The scenario is like this: I have different pieces of code in ruby, Java and a custom one, in a random order (not all ruby code first, then all Java, etc).
Here is a MWE to illustrate:
documentclass[11pt]{report}
usepackage{listings}
usepackage{framed}
usepackage{xcolor}
lstset{ %
basicstyle=footnotesizettfamily,
numbers=left,
numberstyle=tinycolor{gray}ttfamily,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=color{black},
captionpos=b,
keywordstyle=color{blue}bf,
commentstyle=color{gray},
stringstyle=color{green},
keywordstyle={[2]color{red}bf},
}
lstdefinelanguage{story}
{morekeywords={Given,When,Then},
sensitive=false,
morekeywords={[2]Scenario},
}
begin{document}
First some ruby code
lstinputlisting[language=Ruby,caption={Ruby}]{ruby_code.rb}
Now some java code
%lstset{language=Java}
%lstset{ morekeywords={[2]@Given,@When,@Then}}
lstinputlisting[language=Java,caption={Java}]{JavaCode.java}
Now some story code
lstinputlisting[language=story,caption={Story}]{story_code.story}
end{document}
I'll put only the java code which is the one troubling and the one I really care:
// JavaCode.java
public class ComportamientoSteps {
Croupier croupier;
@Given("una nueva mano")
public void nuevaMano() {
croupier = new Croupier();
}
@When("el croupier tiene $carta1 y $carta2")
public void elCroupierTiene(int carta1, int carta2) {
croupier.agregarCarta(new Carta(carta1));
croupier.agregarCarta(new Carta(carta2));
}
@Then("$valor deberia plantar")
public void croupierDeberiaPlantar(String valor) {
boolean debe_pedir = valor.equals("no");
assertThat(croupier.debePedirOtraCarta(), equalTo(debe_pedir));
}
}
What I'm trying to do is:
- First, define a common style for al pieces of code: size, colors of
keywords
and[2]keywords
, etc. I want this style for every language. - Then define the very simple story language.
- Lastly, I input the code via
lstinputlisting[language=whatever]{file}
I want to extend the Java language to interpret @Given
, @When
and @Then
as [2]keywords
. In the MWE there is a piece of code commented out, with that piece of code it works (you have to remove the language=Java
in the options), but I don't want to do that every time I have to put Java code, I just want to use the lstinputlisting
with the language
option. Besides, I don't know if setting the language to Java like that won't bring me trouble in future listings.
listings languages keywords
I've already read extending a language with additional keywords and I have found kind of a solution, but I'm not quite sure if it's the right way and if I won't have problems in the future.
The scenario is like this: I have different pieces of code in ruby, Java and a custom one, in a random order (not all ruby code first, then all Java, etc).
Here is a MWE to illustrate:
documentclass[11pt]{report}
usepackage{listings}
usepackage{framed}
usepackage{xcolor}
lstset{ %
basicstyle=footnotesizettfamily,
numbers=left,
numberstyle=tinycolor{gray}ttfamily,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=color{black},
captionpos=b,
keywordstyle=color{blue}bf,
commentstyle=color{gray},
stringstyle=color{green},
keywordstyle={[2]color{red}bf},
}
lstdefinelanguage{story}
{morekeywords={Given,When,Then},
sensitive=false,
morekeywords={[2]Scenario},
}
begin{document}
First some ruby code
lstinputlisting[language=Ruby,caption={Ruby}]{ruby_code.rb}
Now some java code
%lstset{language=Java}
%lstset{ morekeywords={[2]@Given,@When,@Then}}
lstinputlisting[language=Java,caption={Java}]{JavaCode.java}
Now some story code
lstinputlisting[language=story,caption={Story}]{story_code.story}
end{document}
I'll put only the java code which is the one troubling and the one I really care:
// JavaCode.java
public class ComportamientoSteps {
Croupier croupier;
@Given("una nueva mano")
public void nuevaMano() {
croupier = new Croupier();
}
@When("el croupier tiene $carta1 y $carta2")
public void elCroupierTiene(int carta1, int carta2) {
croupier.agregarCarta(new Carta(carta1));
croupier.agregarCarta(new Carta(carta2));
}
@Then("$valor deberia plantar")
public void croupierDeberiaPlantar(String valor) {
boolean debe_pedir = valor.equals("no");
assertThat(croupier.debePedirOtraCarta(), equalTo(debe_pedir));
}
}
What I'm trying to do is:
- First, define a common style for al pieces of code: size, colors of
keywords
and[2]keywords
, etc. I want this style for every language. - Then define the very simple story language.
- Lastly, I input the code via
lstinputlisting[language=whatever]{file}
I want to extend the Java language to interpret @Given
, @When
and @Then
as [2]keywords
. In the MWE there is a piece of code commented out, with that piece of code it works (you have to remove the language=Java
in the options), but I don't want to do that every time I have to put Java code, I just want to use the lstinputlisting
with the language
option. Besides, I don't know if setting the language to Java like that won't bring me trouble in future listings.
listings languages keywords
listings languages keywords
edited Apr 13 '17 at 12:35
Community♦
1
1
asked May 21 '13 at 2:09
NicoNico
1,04811022
1,04811022
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Listing is powerful tool , more documentation can be found here:The Listings Package.
Instead of setting global settings you should set styles for each individual listings
Flowing code should give you good picture of how to customise listings.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{xcolor}
lstdefinestyle{basic}{
basicstyle=footnotesizettfamily,
numbers=left,
numberstyle=tinycolor{gray}ttfamily,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=color{black},
captionpos=b,
keywordstyle=color{blue}bf,
commentstyle=color{gray},
stringstyle=color{green},
keywordstyle={[2]color{red}bf},
}
lstdefinelanguage{custom}
{
morekeywords={public, void},
sensitive=false,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]",
}
begin{document}
lstinputlisting[ style=basic, language=Python]{main.java}
lstinputlisting[ style=basic, language=Java]{main.java}
lstinputlisting[ style=basic, language=custom]{main.java}
end{document}
Three version of customised listings presented in the code above
they all use same colours for identity of: strings, comments, keywords.
also I showed how to create new language for your need. you can use existed languages for your foundation and add more keywords to it.
[UPDATE]
Here is set of different styles , I personally prefer this one.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{color}
usepackage{accsupp}
definecolor{dkblue}{rgb}{0,0,0.5}
definecolor{comment}{rgb}{1,0,0}
definecolor{mauve}{rgb}{.627,.126,.941}
definecolor{purple}{rgb}{0.5, 0, 0.545098}
lstdefinestyle{java}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=Java,
tabsize=4,
tabsize=4,
numbers=left,
showstringspaces=false,
numberstyle=tinynoncopynumber,
basicstyle=footnotesizettfamily,
keywordstyle=bfseriescolor[rgb]{.133,.545,.133},
commentstyle=itshapecolor{blue},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
frame=single],
resetmargins=true,
}
lstdefinestyle{mathlab}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=C,
morekeywords={end, elseif},
comment=[l]%, % comments
tabsize=4,
numberstyle=tinynoncopynumber,
showstringspaces=false,
basicstyle=footnotesizettfamily,
keywordstyle=color[rgb]{0,0,1},
commentstyle=itshapecolor[rgb]{.133,.545,.133},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
framexleftmargin=3mm,
frame=single,
}
newcommand{noncopynumber}[1]{%
BeginAccSupp{method=escape,ActualText={}}%
#1%
EndAccSupp{}%
}
begin{document}
lstinputlisting[caption=Matlab style main code: "main.java", style=mathlab]{main.java}
lstinputlisting[caption=Matlab style main code: "main.java", style=java]{main.java}
end{document}
Styles probably are the way to go. However, your answer would be more helpful if you could demonstrate their use on the concrete problem of the OP.
– Daniel
May 21 '13 at 6:01
So you're telling me that I should define a complete style for every language? What if I want to change thekeyword
color later? I should change it in every style? It's not like I have a dozen languages, not even a lot of pieces of code, but still doesn't seem a very LaTeX-way. For instance, when I defined the story-language, it "inherited" thekeyword
and[2]keyword
settings, that was what I wanted to keep it consistent.
– Nico
May 21 '13 at 12:02
@Nico, well you probably don't need to add more keywords , take a look at page 12 it shows different languages that supported. If you want different colours then you should identify each style separate, if you only interesting in different keywords then just put different languages instead of style.
– vveliev
May 21 '13 at 17:57
add a comment |
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%2f115316%2fadd-keywords-to-an-existing-language-in-listings%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
Listing is powerful tool , more documentation can be found here:The Listings Package.
Instead of setting global settings you should set styles for each individual listings
Flowing code should give you good picture of how to customise listings.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{xcolor}
lstdefinestyle{basic}{
basicstyle=footnotesizettfamily,
numbers=left,
numberstyle=tinycolor{gray}ttfamily,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=color{black},
captionpos=b,
keywordstyle=color{blue}bf,
commentstyle=color{gray},
stringstyle=color{green},
keywordstyle={[2]color{red}bf},
}
lstdefinelanguage{custom}
{
morekeywords={public, void},
sensitive=false,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]",
}
begin{document}
lstinputlisting[ style=basic, language=Python]{main.java}
lstinputlisting[ style=basic, language=Java]{main.java}
lstinputlisting[ style=basic, language=custom]{main.java}
end{document}
Three version of customised listings presented in the code above
they all use same colours for identity of: strings, comments, keywords.
also I showed how to create new language for your need. you can use existed languages for your foundation and add more keywords to it.
[UPDATE]
Here is set of different styles , I personally prefer this one.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{color}
usepackage{accsupp}
definecolor{dkblue}{rgb}{0,0,0.5}
definecolor{comment}{rgb}{1,0,0}
definecolor{mauve}{rgb}{.627,.126,.941}
definecolor{purple}{rgb}{0.5, 0, 0.545098}
lstdefinestyle{java}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=Java,
tabsize=4,
tabsize=4,
numbers=left,
showstringspaces=false,
numberstyle=tinynoncopynumber,
basicstyle=footnotesizettfamily,
keywordstyle=bfseriescolor[rgb]{.133,.545,.133},
commentstyle=itshapecolor{blue},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
frame=single],
resetmargins=true,
}
lstdefinestyle{mathlab}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=C,
morekeywords={end, elseif},
comment=[l]%, % comments
tabsize=4,
numberstyle=tinynoncopynumber,
showstringspaces=false,
basicstyle=footnotesizettfamily,
keywordstyle=color[rgb]{0,0,1},
commentstyle=itshapecolor[rgb]{.133,.545,.133},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
framexleftmargin=3mm,
frame=single,
}
newcommand{noncopynumber}[1]{%
BeginAccSupp{method=escape,ActualText={}}%
#1%
EndAccSupp{}%
}
begin{document}
lstinputlisting[caption=Matlab style main code: "main.java", style=mathlab]{main.java}
lstinputlisting[caption=Matlab style main code: "main.java", style=java]{main.java}
end{document}
Styles probably are the way to go. However, your answer would be more helpful if you could demonstrate their use on the concrete problem of the OP.
– Daniel
May 21 '13 at 6:01
So you're telling me that I should define a complete style for every language? What if I want to change thekeyword
color later? I should change it in every style? It's not like I have a dozen languages, not even a lot of pieces of code, but still doesn't seem a very LaTeX-way. For instance, when I defined the story-language, it "inherited" thekeyword
and[2]keyword
settings, that was what I wanted to keep it consistent.
– Nico
May 21 '13 at 12:02
@Nico, well you probably don't need to add more keywords , take a look at page 12 it shows different languages that supported. If you want different colours then you should identify each style separate, if you only interesting in different keywords then just put different languages instead of style.
– vveliev
May 21 '13 at 17:57
add a comment |
Listing is powerful tool , more documentation can be found here:The Listings Package.
Instead of setting global settings you should set styles for each individual listings
Flowing code should give you good picture of how to customise listings.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{xcolor}
lstdefinestyle{basic}{
basicstyle=footnotesizettfamily,
numbers=left,
numberstyle=tinycolor{gray}ttfamily,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=color{black},
captionpos=b,
keywordstyle=color{blue}bf,
commentstyle=color{gray},
stringstyle=color{green},
keywordstyle={[2]color{red}bf},
}
lstdefinelanguage{custom}
{
morekeywords={public, void},
sensitive=false,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]",
}
begin{document}
lstinputlisting[ style=basic, language=Python]{main.java}
lstinputlisting[ style=basic, language=Java]{main.java}
lstinputlisting[ style=basic, language=custom]{main.java}
end{document}
Three version of customised listings presented in the code above
they all use same colours for identity of: strings, comments, keywords.
also I showed how to create new language for your need. you can use existed languages for your foundation and add more keywords to it.
[UPDATE]
Here is set of different styles , I personally prefer this one.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{color}
usepackage{accsupp}
definecolor{dkblue}{rgb}{0,0,0.5}
definecolor{comment}{rgb}{1,0,0}
definecolor{mauve}{rgb}{.627,.126,.941}
definecolor{purple}{rgb}{0.5, 0, 0.545098}
lstdefinestyle{java}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=Java,
tabsize=4,
tabsize=4,
numbers=left,
showstringspaces=false,
numberstyle=tinynoncopynumber,
basicstyle=footnotesizettfamily,
keywordstyle=bfseriescolor[rgb]{.133,.545,.133},
commentstyle=itshapecolor{blue},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
frame=single],
resetmargins=true,
}
lstdefinestyle{mathlab}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=C,
morekeywords={end, elseif},
comment=[l]%, % comments
tabsize=4,
numberstyle=tinynoncopynumber,
showstringspaces=false,
basicstyle=footnotesizettfamily,
keywordstyle=color[rgb]{0,0,1},
commentstyle=itshapecolor[rgb]{.133,.545,.133},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
framexleftmargin=3mm,
frame=single,
}
newcommand{noncopynumber}[1]{%
BeginAccSupp{method=escape,ActualText={}}%
#1%
EndAccSupp{}%
}
begin{document}
lstinputlisting[caption=Matlab style main code: "main.java", style=mathlab]{main.java}
lstinputlisting[caption=Matlab style main code: "main.java", style=java]{main.java}
end{document}
Styles probably are the way to go. However, your answer would be more helpful if you could demonstrate their use on the concrete problem of the OP.
– Daniel
May 21 '13 at 6:01
So you're telling me that I should define a complete style for every language? What if I want to change thekeyword
color later? I should change it in every style? It's not like I have a dozen languages, not even a lot of pieces of code, but still doesn't seem a very LaTeX-way. For instance, when I defined the story-language, it "inherited" thekeyword
and[2]keyword
settings, that was what I wanted to keep it consistent.
– Nico
May 21 '13 at 12:02
@Nico, well you probably don't need to add more keywords , take a look at page 12 it shows different languages that supported. If you want different colours then you should identify each style separate, if you only interesting in different keywords then just put different languages instead of style.
– vveliev
May 21 '13 at 17:57
add a comment |
Listing is powerful tool , more documentation can be found here:The Listings Package.
Instead of setting global settings you should set styles for each individual listings
Flowing code should give you good picture of how to customise listings.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{xcolor}
lstdefinestyle{basic}{
basicstyle=footnotesizettfamily,
numbers=left,
numberstyle=tinycolor{gray}ttfamily,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=color{black},
captionpos=b,
keywordstyle=color{blue}bf,
commentstyle=color{gray},
stringstyle=color{green},
keywordstyle={[2]color{red}bf},
}
lstdefinelanguage{custom}
{
morekeywords={public, void},
sensitive=false,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]",
}
begin{document}
lstinputlisting[ style=basic, language=Python]{main.java}
lstinputlisting[ style=basic, language=Java]{main.java}
lstinputlisting[ style=basic, language=custom]{main.java}
end{document}
Three version of customised listings presented in the code above
they all use same colours for identity of: strings, comments, keywords.
also I showed how to create new language for your need. you can use existed languages for your foundation and add more keywords to it.
[UPDATE]
Here is set of different styles , I personally prefer this one.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{color}
usepackage{accsupp}
definecolor{dkblue}{rgb}{0,0,0.5}
definecolor{comment}{rgb}{1,0,0}
definecolor{mauve}{rgb}{.627,.126,.941}
definecolor{purple}{rgb}{0.5, 0, 0.545098}
lstdefinestyle{java}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=Java,
tabsize=4,
tabsize=4,
numbers=left,
showstringspaces=false,
numberstyle=tinynoncopynumber,
basicstyle=footnotesizettfamily,
keywordstyle=bfseriescolor[rgb]{.133,.545,.133},
commentstyle=itshapecolor{blue},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
frame=single],
resetmargins=true,
}
lstdefinestyle{mathlab}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=C,
morekeywords={end, elseif},
comment=[l]%, % comments
tabsize=4,
numberstyle=tinynoncopynumber,
showstringspaces=false,
basicstyle=footnotesizettfamily,
keywordstyle=color[rgb]{0,0,1},
commentstyle=itshapecolor[rgb]{.133,.545,.133},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
framexleftmargin=3mm,
frame=single,
}
newcommand{noncopynumber}[1]{%
BeginAccSupp{method=escape,ActualText={}}%
#1%
EndAccSupp{}%
}
begin{document}
lstinputlisting[caption=Matlab style main code: "main.java", style=mathlab]{main.java}
lstinputlisting[caption=Matlab style main code: "main.java", style=java]{main.java}
end{document}
Listing is powerful tool , more documentation can be found here:The Listings Package.
Instead of setting global settings you should set styles for each individual listings
Flowing code should give you good picture of how to customise listings.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{xcolor}
lstdefinestyle{basic}{
basicstyle=footnotesizettfamily,
numbers=left,
numberstyle=tinycolor{gray}ttfamily,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=color{black},
captionpos=b,
keywordstyle=color{blue}bf,
commentstyle=color{gray},
stringstyle=color{green},
keywordstyle={[2]color{red}bf},
}
lstdefinelanguage{custom}
{
morekeywords={public, void},
sensitive=false,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]",
}
begin{document}
lstinputlisting[ style=basic, language=Python]{main.java}
lstinputlisting[ style=basic, language=Java]{main.java}
lstinputlisting[ style=basic, language=custom]{main.java}
end{document}
Three version of customised listings presented in the code above
they all use same colours for identity of: strings, comments, keywords.
also I showed how to create new language for your need. you can use existed languages for your foundation and add more keywords to it.
[UPDATE]
Here is set of different styles , I personally prefer this one.
documentclass{article}
usepackage{graphicx}
usepackage{listings}
usepackage{color}
usepackage{accsupp}
definecolor{dkblue}{rgb}{0,0,0.5}
definecolor{comment}{rgb}{1,0,0}
definecolor{mauve}{rgb}{.627,.126,.941}
definecolor{purple}{rgb}{0.5, 0, 0.545098}
lstdefinestyle{java}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=Java,
tabsize=4,
tabsize=4,
numbers=left,
showstringspaces=false,
numberstyle=tinynoncopynumber,
basicstyle=footnotesizettfamily,
keywordstyle=bfseriescolor[rgb]{.133,.545,.133},
commentstyle=itshapecolor{blue},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
frame=single],
resetmargins=true,
}
lstdefinestyle{mathlab}{
belowcaptionskip=1baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=parindent,
language=C,
morekeywords={end, elseif},
comment=[l]%, % comments
tabsize=4,
numberstyle=tinynoncopynumber,
showstringspaces=false,
basicstyle=footnotesizettfamily,
keywordstyle=color[rgb]{0,0,1},
commentstyle=itshapecolor[rgb]{.133,.545,.133},
stringstyle=color{mauve},
directivestyle=bfseriescolor{purple},
framexleftmargin=3mm,
frame=single,
}
newcommand{noncopynumber}[1]{%
BeginAccSupp{method=escape,ActualText={}}%
#1%
EndAccSupp{}%
}
begin{document}
lstinputlisting[caption=Matlab style main code: "main.java", style=mathlab]{main.java}
lstinputlisting[caption=Matlab style main code: "main.java", style=java]{main.java}
end{document}
edited 3 hours ago
Dr. Manuel Kuehner
9,08732769
9,08732769
answered May 21 '13 at 4:28
vvelievvveliev
35029
35029
Styles probably are the way to go. However, your answer would be more helpful if you could demonstrate their use on the concrete problem of the OP.
– Daniel
May 21 '13 at 6:01
So you're telling me that I should define a complete style for every language? What if I want to change thekeyword
color later? I should change it in every style? It's not like I have a dozen languages, not even a lot of pieces of code, but still doesn't seem a very LaTeX-way. For instance, when I defined the story-language, it "inherited" thekeyword
and[2]keyword
settings, that was what I wanted to keep it consistent.
– Nico
May 21 '13 at 12:02
@Nico, well you probably don't need to add more keywords , take a look at page 12 it shows different languages that supported. If you want different colours then you should identify each style separate, if you only interesting in different keywords then just put different languages instead of style.
– vveliev
May 21 '13 at 17:57
add a comment |
Styles probably are the way to go. However, your answer would be more helpful if you could demonstrate their use on the concrete problem of the OP.
– Daniel
May 21 '13 at 6:01
So you're telling me that I should define a complete style for every language? What if I want to change thekeyword
color later? I should change it in every style? It's not like I have a dozen languages, not even a lot of pieces of code, but still doesn't seem a very LaTeX-way. For instance, when I defined the story-language, it "inherited" thekeyword
and[2]keyword
settings, that was what I wanted to keep it consistent.
– Nico
May 21 '13 at 12:02
@Nico, well you probably don't need to add more keywords , take a look at page 12 it shows different languages that supported. If you want different colours then you should identify each style separate, if you only interesting in different keywords then just put different languages instead of style.
– vveliev
May 21 '13 at 17:57
Styles probably are the way to go. However, your answer would be more helpful if you could demonstrate their use on the concrete problem of the OP.
– Daniel
May 21 '13 at 6:01
Styles probably are the way to go. However, your answer would be more helpful if you could demonstrate their use on the concrete problem of the OP.
– Daniel
May 21 '13 at 6:01
So you're telling me that I should define a complete style for every language? What if I want to change the
keyword
color later? I should change it in every style? It's not like I have a dozen languages, not even a lot of pieces of code, but still doesn't seem a very LaTeX-way. For instance, when I defined the story-language, it "inherited" the keyword
and [2]keyword
settings, that was what I wanted to keep it consistent.– Nico
May 21 '13 at 12:02
So you're telling me that I should define a complete style for every language? What if I want to change the
keyword
color later? I should change it in every style? It's not like I have a dozen languages, not even a lot of pieces of code, but still doesn't seem a very LaTeX-way. For instance, when I defined the story-language, it "inherited" the keyword
and [2]keyword
settings, that was what I wanted to keep it consistent.– Nico
May 21 '13 at 12:02
@Nico, well you probably don't need to add more keywords , take a look at page 12 it shows different languages that supported. If you want different colours then you should identify each style separate, if you only interesting in different keywords then just put different languages instead of style.
– vveliev
May 21 '13 at 17:57
@Nico, well you probably don't need to add more keywords , take a look at page 12 it shows different languages that supported. If you want different colours then you should identify each style separate, if you only interesting in different keywords then just put different languages instead of style.
– vveliev
May 21 '13 at 17:57
add a comment |
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%2f115316%2fadd-keywords-to-an-existing-language-in-listings%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