Highlighting a line in program listings in lyx
up vote
5
down vote
favorite
I am not able to find how i can highlight a particular line of code in program listings.
Syntax highlighting seems to work but I dont need particular language keywords to be highlighted.
e.g.
for the code below how can i highlight/bold face char j=malloc......line
public static void function(){
int i=0;
char j = malloc(sizeof(char));
/.......some code....
return;
}
lyx
|
show 1 more comment
up vote
5
down vote
favorite
I am not able to find how i can highlight a particular line of code in program listings.
Syntax highlighting seems to work but I dont need particular language keywords to be highlighted.
e.g.
for the code below how can i highlight/bold face char j=malloc......line
public static void function(){
int i=0;
char j = malloc(sizeof(char));
/.......some code....
return;
}
lyx
Welcome to TeX.SX! Do you know the packagelistings
? I am sure, there is support for this inlyx
too. You should be more specific and provide a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting withdocumentclass{...}
and ending withend{document}
.
– Christian Hupfer
Aug 26 '14 at 20:20
1
@ChristianHupfer LyX useslistings
by default when one does Insert --> Program listing, so the question is more how to this withlistings
, in LyX.
– Torbjørn T.
Aug 26 '14 at 20:21
@TorbjørnT. I don't uselyx
at all...
– Christian Hupfer
Aug 26 '14 at 20:22
@ChristianHupfer What i know is listings package is by default installed in lyx 2.0.6 but highlighting a particular line of code to show it as an interesting event in program listing is not mentioned afaik
– dsingh
Aug 26 '14 at 20:23
Idea: one could use LaTeX escape in the listing: somehow, you need to addescapechar=@
(or perhaps another character, remembering to escape special ones) and then issue a@color{blue}@
at the beginning of the line you wish to highlight. Can't test right now though...
– Sean Allred
Aug 26 '14 at 20:32
|
show 1 more comment
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I am not able to find how i can highlight a particular line of code in program listings.
Syntax highlighting seems to work but I dont need particular language keywords to be highlighted.
e.g.
for the code below how can i highlight/bold face char j=malloc......line
public static void function(){
int i=0;
char j = malloc(sizeof(char));
/.......some code....
return;
}
lyx
I am not able to find how i can highlight a particular line of code in program listings.
Syntax highlighting seems to work but I dont need particular language keywords to be highlighted.
e.g.
for the code below how can i highlight/bold face char j=malloc......line
public static void function(){
int i=0;
char j = malloc(sizeof(char));
/.......some code....
return;
}
lyx
lyx
edited Aug 26 '14 at 20:34
asked Aug 26 '14 at 20:19
dsingh
1554
1554
Welcome to TeX.SX! Do you know the packagelistings
? I am sure, there is support for this inlyx
too. You should be more specific and provide a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting withdocumentclass{...}
and ending withend{document}
.
– Christian Hupfer
Aug 26 '14 at 20:20
1
@ChristianHupfer LyX useslistings
by default when one does Insert --> Program listing, so the question is more how to this withlistings
, in LyX.
– Torbjørn T.
Aug 26 '14 at 20:21
@TorbjørnT. I don't uselyx
at all...
– Christian Hupfer
Aug 26 '14 at 20:22
@ChristianHupfer What i know is listings package is by default installed in lyx 2.0.6 but highlighting a particular line of code to show it as an interesting event in program listing is not mentioned afaik
– dsingh
Aug 26 '14 at 20:23
Idea: one could use LaTeX escape in the listing: somehow, you need to addescapechar=@
(or perhaps another character, remembering to escape special ones) and then issue a@color{blue}@
at the beginning of the line you wish to highlight. Can't test right now though...
– Sean Allred
Aug 26 '14 at 20:32
|
show 1 more comment
Welcome to TeX.SX! Do you know the packagelistings
? I am sure, there is support for this inlyx
too. You should be more specific and provide a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting withdocumentclass{...}
and ending withend{document}
.
– Christian Hupfer
Aug 26 '14 at 20:20
1
@ChristianHupfer LyX useslistings
by default when one does Insert --> Program listing, so the question is more how to this withlistings
, in LyX.
– Torbjørn T.
Aug 26 '14 at 20:21
@TorbjørnT. I don't uselyx
at all...
– Christian Hupfer
Aug 26 '14 at 20:22
@ChristianHupfer What i know is listings package is by default installed in lyx 2.0.6 but highlighting a particular line of code to show it as an interesting event in program listing is not mentioned afaik
– dsingh
Aug 26 '14 at 20:23
Idea: one could use LaTeX escape in the listing: somehow, you need to addescapechar=@
(or perhaps another character, remembering to escape special ones) and then issue a@color{blue}@
at the beginning of the line you wish to highlight. Can't test right now though...
– Sean Allred
Aug 26 '14 at 20:32
Welcome to TeX.SX! Do you know the package
listings
? I am sure, there is support for this in lyx
too. You should be more specific and provide a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with documentclass{...}
and ending with end{document}
.– Christian Hupfer
Aug 26 '14 at 20:20
Welcome to TeX.SX! Do you know the package
listings
? I am sure, there is support for this in lyx
too. You should be more specific and provide a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with documentclass{...}
and ending with end{document}
.– Christian Hupfer
Aug 26 '14 at 20:20
1
1
@ChristianHupfer LyX uses
listings
by default when one does Insert --> Program listing, so the question is more how to this with listings
, in LyX.– Torbjørn T.
Aug 26 '14 at 20:21
@ChristianHupfer LyX uses
listings
by default when one does Insert --> Program listing, so the question is more how to this with listings
, in LyX.– Torbjørn T.
Aug 26 '14 at 20:21
@TorbjørnT. I don't use
lyx
at all...– Christian Hupfer
Aug 26 '14 at 20:22
@TorbjørnT. I don't use
lyx
at all...– Christian Hupfer
Aug 26 '14 at 20:22
@ChristianHupfer What i know is listings package is by default installed in lyx 2.0.6 but highlighting a particular line of code to show it as an interesting event in program listing is not mentioned afaik
– dsingh
Aug 26 '14 at 20:23
@ChristianHupfer What i know is listings package is by default installed in lyx 2.0.6 but highlighting a particular line of code to show it as an interesting event in program listing is not mentioned afaik
– dsingh
Aug 26 '14 at 20:23
Idea: one could use LaTeX escape in the listing: somehow, you need to add
escapechar=@
(or perhaps another character, remembering to escape special ones) and then issue a @color{blue}@
at the beginning of the line you wish to highlight. Can't test right now though...– Sean Allred
Aug 26 '14 at 20:32
Idea: one could use LaTeX escape in the listing: somehow, you need to add
escapechar=@
(or perhaps another character, remembering to escape special ones) and then issue a @color{blue}@
at the beginning of the line you wish to highlight. Can't test right now though...– Sean Allred
Aug 26 '14 at 20:32
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
Like this:
documentclass[12pt]{article}
usepackage{listings}
usepackage{xcolor}
newcommand{hilight}{makebox[0pt][l]{%
color{yellow}rule[-4pt]{0.65linewidth}{14pt}}%
}
begin{document}
begin{lstlisting}[language=C,escapechar=%]
public static void function(){
int i=0;
%hilight%char j = malloc(sizeof(char));
/.......some code....
return;
}
end{lstlisting}
end{document}
Adapted from an answer by Juanjo on latex-community.org.
You can see the result on writeLaTeX.
To achieve the same result in LyX, go to Document->Settings
and navigate to the Preamble
tab. Paste everything from usepackage{listings}
to the end of the begin{document}
block into the preamble and save it.
To insert the program listing into the document, go to Insert -> Program Listing
in the menu. Paste the code into the listing environment. Then hold Ctrl
and right-click on the environment and open the Settings
dialog. Go to the Advanced
tab in the dialog and paste escapechar={%}
into the text area. Configure the other settings however you want them to look.
There is an example LyX file you can download [here]
(https://dl.dropbox.com/s/9dfqtsc0fnftggc/hilite_example.lyx).
thanks for the example but actually i am looking for a way in which i can do the line highlighting while working on lyx document itself. I think i am too lazy to edit the latex document for it, that is the whole purpose of using lyx afaik. Please correct me if I am wrong.
– dsingh
Aug 26 '14 at 21:19
I gave you the whole document to (hopefully) make it clear where everything goes, but there is no reason not to do this in LyX. Put the stuff beforebegin{document}
in your preamble and thebegin{lstlisting}...end{lstlisting}
part in a LaTeX box within your document.
– snim2
Aug 26 '14 at 21:21
I am afraid that when i incorporated your new command function in my document the highlighting is not happening. I followed what you have explained. in my pdf instead i can see the actual command %hilight%char j........
– dsingh
Aug 26 '14 at 21:49
1
I think your issue is with LyX and how to use some of its features. I have added more detail to the answer and an example LyX file.
– snim2
Aug 26 '14 at 22:07
If this has answered your question in full, please can you accept the answer? If not, is there anything specific that is still problematic for you?
– snim2
Aug 28 '14 at 9:42
|
show 4 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Like this:
documentclass[12pt]{article}
usepackage{listings}
usepackage{xcolor}
newcommand{hilight}{makebox[0pt][l]{%
color{yellow}rule[-4pt]{0.65linewidth}{14pt}}%
}
begin{document}
begin{lstlisting}[language=C,escapechar=%]
public static void function(){
int i=0;
%hilight%char j = malloc(sizeof(char));
/.......some code....
return;
}
end{lstlisting}
end{document}
Adapted from an answer by Juanjo on latex-community.org.
You can see the result on writeLaTeX.
To achieve the same result in LyX, go to Document->Settings
and navigate to the Preamble
tab. Paste everything from usepackage{listings}
to the end of the begin{document}
block into the preamble and save it.
To insert the program listing into the document, go to Insert -> Program Listing
in the menu. Paste the code into the listing environment. Then hold Ctrl
and right-click on the environment and open the Settings
dialog. Go to the Advanced
tab in the dialog and paste escapechar={%}
into the text area. Configure the other settings however you want them to look.
There is an example LyX file you can download [here]
(https://dl.dropbox.com/s/9dfqtsc0fnftggc/hilite_example.lyx).
thanks for the example but actually i am looking for a way in which i can do the line highlighting while working on lyx document itself. I think i am too lazy to edit the latex document for it, that is the whole purpose of using lyx afaik. Please correct me if I am wrong.
– dsingh
Aug 26 '14 at 21:19
I gave you the whole document to (hopefully) make it clear where everything goes, but there is no reason not to do this in LyX. Put the stuff beforebegin{document}
in your preamble and thebegin{lstlisting}...end{lstlisting}
part in a LaTeX box within your document.
– snim2
Aug 26 '14 at 21:21
I am afraid that when i incorporated your new command function in my document the highlighting is not happening. I followed what you have explained. in my pdf instead i can see the actual command %hilight%char j........
– dsingh
Aug 26 '14 at 21:49
1
I think your issue is with LyX and how to use some of its features. I have added more detail to the answer and an example LyX file.
– snim2
Aug 26 '14 at 22:07
If this has answered your question in full, please can you accept the answer? If not, is there anything specific that is still problematic for you?
– snim2
Aug 28 '14 at 9:42
|
show 4 more comments
up vote
4
down vote
accepted
Like this:
documentclass[12pt]{article}
usepackage{listings}
usepackage{xcolor}
newcommand{hilight}{makebox[0pt][l]{%
color{yellow}rule[-4pt]{0.65linewidth}{14pt}}%
}
begin{document}
begin{lstlisting}[language=C,escapechar=%]
public static void function(){
int i=0;
%hilight%char j = malloc(sizeof(char));
/.......some code....
return;
}
end{lstlisting}
end{document}
Adapted from an answer by Juanjo on latex-community.org.
You can see the result on writeLaTeX.
To achieve the same result in LyX, go to Document->Settings
and navigate to the Preamble
tab. Paste everything from usepackage{listings}
to the end of the begin{document}
block into the preamble and save it.
To insert the program listing into the document, go to Insert -> Program Listing
in the menu. Paste the code into the listing environment. Then hold Ctrl
and right-click on the environment and open the Settings
dialog. Go to the Advanced
tab in the dialog and paste escapechar={%}
into the text area. Configure the other settings however you want them to look.
There is an example LyX file you can download [here]
(https://dl.dropbox.com/s/9dfqtsc0fnftggc/hilite_example.lyx).
thanks for the example but actually i am looking for a way in which i can do the line highlighting while working on lyx document itself. I think i am too lazy to edit the latex document for it, that is the whole purpose of using lyx afaik. Please correct me if I am wrong.
– dsingh
Aug 26 '14 at 21:19
I gave you the whole document to (hopefully) make it clear where everything goes, but there is no reason not to do this in LyX. Put the stuff beforebegin{document}
in your preamble and thebegin{lstlisting}...end{lstlisting}
part in a LaTeX box within your document.
– snim2
Aug 26 '14 at 21:21
I am afraid that when i incorporated your new command function in my document the highlighting is not happening. I followed what you have explained. in my pdf instead i can see the actual command %hilight%char j........
– dsingh
Aug 26 '14 at 21:49
1
I think your issue is with LyX and how to use some of its features. I have added more detail to the answer and an example LyX file.
– snim2
Aug 26 '14 at 22:07
If this has answered your question in full, please can you accept the answer? If not, is there anything specific that is still problematic for you?
– snim2
Aug 28 '14 at 9:42
|
show 4 more comments
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Like this:
documentclass[12pt]{article}
usepackage{listings}
usepackage{xcolor}
newcommand{hilight}{makebox[0pt][l]{%
color{yellow}rule[-4pt]{0.65linewidth}{14pt}}%
}
begin{document}
begin{lstlisting}[language=C,escapechar=%]
public static void function(){
int i=0;
%hilight%char j = malloc(sizeof(char));
/.......some code....
return;
}
end{lstlisting}
end{document}
Adapted from an answer by Juanjo on latex-community.org.
You can see the result on writeLaTeX.
To achieve the same result in LyX, go to Document->Settings
and navigate to the Preamble
tab. Paste everything from usepackage{listings}
to the end of the begin{document}
block into the preamble and save it.
To insert the program listing into the document, go to Insert -> Program Listing
in the menu. Paste the code into the listing environment. Then hold Ctrl
and right-click on the environment and open the Settings
dialog. Go to the Advanced
tab in the dialog and paste escapechar={%}
into the text area. Configure the other settings however you want them to look.
There is an example LyX file you can download [here]
(https://dl.dropbox.com/s/9dfqtsc0fnftggc/hilite_example.lyx).
Like this:
documentclass[12pt]{article}
usepackage{listings}
usepackage{xcolor}
newcommand{hilight}{makebox[0pt][l]{%
color{yellow}rule[-4pt]{0.65linewidth}{14pt}}%
}
begin{document}
begin{lstlisting}[language=C,escapechar=%]
public static void function(){
int i=0;
%hilight%char j = malloc(sizeof(char));
/.......some code....
return;
}
end{lstlisting}
end{document}
Adapted from an answer by Juanjo on latex-community.org.
You can see the result on writeLaTeX.
To achieve the same result in LyX, go to Document->Settings
and navigate to the Preamble
tab. Paste everything from usepackage{listings}
to the end of the begin{document}
block into the preamble and save it.
To insert the program listing into the document, go to Insert -> Program Listing
in the menu. Paste the code into the listing environment. Then hold Ctrl
and right-click on the environment and open the Settings
dialog. Go to the Advanced
tab in the dialog and paste escapechar={%}
into the text area. Configure the other settings however you want them to look.
There is an example LyX file you can download [here]
(https://dl.dropbox.com/s/9dfqtsc0fnftggc/hilite_example.lyx).
edited Dec 3 at 21:19
answered Aug 26 '14 at 20:48
snim2
361214
361214
thanks for the example but actually i am looking for a way in which i can do the line highlighting while working on lyx document itself. I think i am too lazy to edit the latex document for it, that is the whole purpose of using lyx afaik. Please correct me if I am wrong.
– dsingh
Aug 26 '14 at 21:19
I gave you the whole document to (hopefully) make it clear where everything goes, but there is no reason not to do this in LyX. Put the stuff beforebegin{document}
in your preamble and thebegin{lstlisting}...end{lstlisting}
part in a LaTeX box within your document.
– snim2
Aug 26 '14 at 21:21
I am afraid that when i incorporated your new command function in my document the highlighting is not happening. I followed what you have explained. in my pdf instead i can see the actual command %hilight%char j........
– dsingh
Aug 26 '14 at 21:49
1
I think your issue is with LyX and how to use some of its features. I have added more detail to the answer and an example LyX file.
– snim2
Aug 26 '14 at 22:07
If this has answered your question in full, please can you accept the answer? If not, is there anything specific that is still problematic for you?
– snim2
Aug 28 '14 at 9:42
|
show 4 more comments
thanks for the example but actually i am looking for a way in which i can do the line highlighting while working on lyx document itself. I think i am too lazy to edit the latex document for it, that is the whole purpose of using lyx afaik. Please correct me if I am wrong.
– dsingh
Aug 26 '14 at 21:19
I gave you the whole document to (hopefully) make it clear where everything goes, but there is no reason not to do this in LyX. Put the stuff beforebegin{document}
in your preamble and thebegin{lstlisting}...end{lstlisting}
part in a LaTeX box within your document.
– snim2
Aug 26 '14 at 21:21
I am afraid that when i incorporated your new command function in my document the highlighting is not happening. I followed what you have explained. in my pdf instead i can see the actual command %hilight%char j........
– dsingh
Aug 26 '14 at 21:49
1
I think your issue is with LyX and how to use some of its features. I have added more detail to the answer and an example LyX file.
– snim2
Aug 26 '14 at 22:07
If this has answered your question in full, please can you accept the answer? If not, is there anything specific that is still problematic for you?
– snim2
Aug 28 '14 at 9:42
thanks for the example but actually i am looking for a way in which i can do the line highlighting while working on lyx document itself. I think i am too lazy to edit the latex document for it, that is the whole purpose of using lyx afaik. Please correct me if I am wrong.
– dsingh
Aug 26 '14 at 21:19
thanks for the example but actually i am looking for a way in which i can do the line highlighting while working on lyx document itself. I think i am too lazy to edit the latex document for it, that is the whole purpose of using lyx afaik. Please correct me if I am wrong.
– dsingh
Aug 26 '14 at 21:19
I gave you the whole document to (hopefully) make it clear where everything goes, but there is no reason not to do this in LyX. Put the stuff before
begin{document}
in your preamble and the begin{lstlisting}...end{lstlisting}
part in a LaTeX box within your document.– snim2
Aug 26 '14 at 21:21
I gave you the whole document to (hopefully) make it clear where everything goes, but there is no reason not to do this in LyX. Put the stuff before
begin{document}
in your preamble and the begin{lstlisting}...end{lstlisting}
part in a LaTeX box within your document.– snim2
Aug 26 '14 at 21:21
I am afraid that when i incorporated your new command function in my document the highlighting is not happening. I followed what you have explained. in my pdf instead i can see the actual command %hilight%char j........
– dsingh
Aug 26 '14 at 21:49
I am afraid that when i incorporated your new command function in my document the highlighting is not happening. I followed what you have explained. in my pdf instead i can see the actual command %hilight%char j........
– dsingh
Aug 26 '14 at 21:49
1
1
I think your issue is with LyX and how to use some of its features. I have added more detail to the answer and an example LyX file.
– snim2
Aug 26 '14 at 22:07
I think your issue is with LyX and how to use some of its features. I have added more detail to the answer and an example LyX file.
– snim2
Aug 26 '14 at 22:07
If this has answered your question in full, please can you accept the answer? If not, is there anything specific that is still problematic for you?
– snim2
Aug 28 '14 at 9:42
If this has answered your question in full, please can you accept the answer? If not, is there anything specific that is still problematic for you?
– snim2
Aug 28 '14 at 9:42
|
show 4 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f198064%2fhighlighting-a-line-in-program-listings-in-lyx%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
Welcome to TeX.SX! Do you know the package
listings
? I am sure, there is support for this inlyx
too. You should be more specific and provide a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting withdocumentclass{...}
and ending withend{document}
.– Christian Hupfer
Aug 26 '14 at 20:20
1
@ChristianHupfer LyX uses
listings
by default when one does Insert --> Program listing, so the question is more how to this withlistings
, in LyX.– Torbjørn T.
Aug 26 '14 at 20:21
@TorbjørnT. I don't use
lyx
at all...– Christian Hupfer
Aug 26 '14 at 20:22
@ChristianHupfer What i know is listings package is by default installed in lyx 2.0.6 but highlighting a particular line of code to show it as an interesting event in program listing is not mentioned afaik
– dsingh
Aug 26 '14 at 20:23
Idea: one could use LaTeX escape in the listing: somehow, you need to add
escapechar=@
(or perhaps another character, remembering to escape special ones) and then issue a@color{blue}@
at the beginning of the line you wish to highlight. Can't test right now though...– Sean Allred
Aug 26 '14 at 20:32