# Latex Listing Format Error

I'm having some difficulty with getting the listings package to properly format code snippets. In particular, it does not consistently bold keywords and italicize comments.

The code I am currently using is:

\usepackage{listings}
\lstset{language=C,
morekeywords=    {*,rule,endrule,module,endmodule,Action,interface,endinterface,method,endmethod,case,endcase,matches},
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular  underscores
%frame=single,                   % adds a frame around the code
tabsize=2,                      % sets default tabsize to 2 spaces
breaklines=true,                % sets automatic line breaking
breakatwhitespace=true,        % sets if automatic breaks should only happen at whitespace
}
\begin{lstlisting}[caption={code},label={lst:code}]
//  1: t1 = Py - Qy
rule op1_ctrl(oper == 4'd1 && fire_cond);
endrule

//  2: t2 = Px - Qx
rule op2_ctrl(oper == 4'd2 && fire_cond);
endrule
\end{lstlisting}


For example, my code snippet should be displayed like this:

// 1: t1 = Py - Qy

rule op1_ctrl(oper == 4'd1 && fire_cond);

endrule

// 2: t2 = Px - Qx

endrule

What I actually get is:

// 1: t1 = Py - Qy

rule op1_ctrl(oper == 4'd1 && fire_cond);

endrule

// 2: t2 = Px - Qx

endrule

Any ideas why this is happening or how I can fix it?

-
I know that people will be asking for a minimum failing example:o) try to delete one line at a time, and see if the problem persist. –  Hans-Peter E. Kristiansen Jan 20 '12 at 3:52

The problem here is that listings uses ' as string delimiters. That's why everything between the two single quotes in your code is typeset without any formatting. One way around this is to identify a literate replacement text:

\literate=*{'}{'}{1}


This replaces ' with ' of width 1 , doing virtually nothing, but replacing the syntax change associated with ', as in the following example:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}
\lstset{language=C,
morekeywords={*,rule,endrule,module,endmodule,Action,interface,endinterface,method,endmethod,case,endcase,matches},
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular  underscores
%frame=single,                   % adds a frame around the code
tabsize=2,                      % sets default tabsize to 2 spaces
breaklines=true,                % sets automatic line breaking
breakatwhitespace=true,        % sets if automatic breaks should only happen at whitespace
literate=*{'}{'}{1}
}
\begin{lstlisting}[caption={code},label={lst:code}]
//  1: t1 = Py - Qy
rule op1_ctrl(oper == 4'd1 && fire_cond);
endrule

//  2: t2 = Px - Qx
rule op2_ctrl(oper == 4'd2 && fire_cond);