Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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);
pa0.loadAddSub(0, 5, 7, 1, vecCnt);
pa1.loadAddSub(0, 5, 7, 1, vecCnt);
pa2.loadAddSub(0, 5, 7, 1, vecCnt);
pa3.loadAddSub(0, 5, 7, 1, vecCnt);
endrule

//  2: t2 = Px - Qx
rule op2_ctrl(oper == 4'd2 && fire_cond);
pa0.loadAddSub(0, 4, 6, 2, vecCnt);
pa1.loadAddSub(0, 4, 6, 2, vecCnt);
pa2.loadAddSub(0, 4, 6, 2, vecCnt);
pa3.loadAddSub(0, 4, 6, 2, vecCnt);
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);

pa0.loadAddSub(0, 5, 7, 1, vecCnt);

pa1.loadAddSub(0, 5, 7, 1, vecCnt);

pa2.loadAddSub(0, 5, 7, 1, vecCnt);

pa3.loadAddSub(0, 5, 7, 1, vecCnt);

endrule

// 2: t2 = Px - Qx

rule op2_ctrl(oper == 4'd2 && fire_cond); pa0.loadAddSub(0, 4, 6, 2, vecCnt);

pa1.loadAddSub(0, 4, 6, 2, vecCnt);

pa2.loadAddSub(0, 4, 6, 2, vecCnt);

pa3.loadAddSub(0, 4, 6, 2, vecCnt);

endrule

What I actually get is:

// 1: t1 = Py - Qy

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

pa0.loadAddSub(0, 5, 7, 1, vecCnt);

pa1.loadAddSub(0, 5, 7, 1, vecCnt);

pa2.loadAddSub(0, 5, 7, 1, vecCnt);

pa3.loadAddSub(0, 5, 7, 1, vecCnt);

endrule

// 2: t2 = Px - Qx

rule op2_ctrl(oper == 4'd2 && fire_cond); pa0.loadAddSub(0, 4, 6, 2, vecCnt);

pa1.loadAddSub(0, 4, 6, 2, vecCnt);

pa2.loadAddSub(0, 4, 6, 2, vecCnt);

pa3.loadAddSub(0, 4, 6, 2, vecCnt);

endrule

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

share|improve this question
1  
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

1 Answer 1

up vote 3 down vote accepted

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:

enter image description here

\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);
pa0.loadAddSub(0, 5, 7, 1, vecCnt);
pa1.loadAddSub(0, 5, 7, 1, vecCnt);
pa2.loadAddSub(0, 5, 7, 1, vecCnt);
pa3.loadAddSub(0, 5, 7, 1, vecCnt);
endrule

//  2: t2 = Px - Qx
rule op2_ctrl(oper == 4'd2 && fire_cond);
pa0.loadAddSub(0, 4, 6, 2, vecCnt);
pa1.loadAddSub(0, 4, 6, 2, vecCnt);
pa2.loadAddSub(0, 4, 6, 2, vecCnt);
pa3.loadAddSub(0, 4, 6, 2, vecCnt);
endrule
\end{lstlisting}
\end{document}
share|improve this answer
    
Thank you so much! This works perfectly –  Lyn Jan 20 '12 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.