[Tex/LaTex] Highlighting a line in program listings in lyx

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;
}

Best Answer

Like this:

\documentclass[12pt]{article}
\usepackage{listings}
\usepackage{xcolor}

\newcommand{\hilight}{\makebox[0pt][l]{%
    \color{yellow}\rule[-4pt]{0.65\linewidth}{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).