5

I defined a command to simplify the writing of code listings of (pseudo-) C code:

\lstdefinestyle{c}{
language=C,
basicstyle=\small\sffamily,
numbers=left,
numberstyle=\tiny,
frame=L,
columns=fullflexible,
showstringspaces=false
}

\lstnewenvironment{ccode}{
\mathligsoff\lstset{style=c}}{}

If I use the environment ccode everything works fine unless apostrophes are used:

\begin{ccode}
  int x';
  // use x
  x = 3;
\end{coode}

In this listing the comment is not printed in italics but in 'straight' characters. If I remove ' or add another ' the italics are restored. This is the complete .tex code:

\documentclass[]{report}
\usepackage{listings}
\usepackage{semantic}
\lstdefinestyle{c}{
  language=C,
  basicstyle=\small\sffamily,
  numbers=left,
  numberstyle=\tiny,
  frame=L,
  columns=fullflexible,
  showstringspaces=false}
\lstnewenvironment{ccode}{
\mathligsoff\lstset{style=c}}{}

\begin{document}
\begin{ccode}
  int x';
  // hi there
  x = 3;
\end{ccode}
\end{document}

EDIT: The accepted answer works flawlessly for me. Just a hint for other readers: If no strings or characters need to be displayed -- and thus no code like 'a' or 'string' -- \lstset{literate={'}{{'}}1} will also do the trick.

  • The single quote is a string delimiter; so everything that comes after x' in your code gets typeset in the default string style (upright). Why do you use ' there? Is it valid C code? – jub0bs Aug 17 '14 at 18:49
  • Ah sure, I forgot about that... No, it is pseudo-code. Maybe I should just use a similar looking character. Is it possible to use some arbitrary unicode sign inside a code listing? – Bastian Aug 17 '14 at 18:53
  • Unicode is not supported by listings. Why not use a package dedicated to typesetting pseudo-code instead of listings? – jub0bs Aug 17 '14 at 19:01
  • Question: is x' some kind of placeholder in your C code? – jub0bs Aug 17 '14 at 19:21
  • Well it was easier to just adopt the listing :-).. – Bastian Aug 17 '14 at 19:50
7

As Jubobs commented while I was working on this answer, the problem is that ' is treated as a string delimiter in C, so everything until the next ' is considered part of a string. You can see this by setting stringstyle=\itshape or similar; everything following the ' is shown as a string.

Discussion in the comments notwithstanding (I would recommend something better suited for pseudo-code instead of listings as well), if you must stick with using listings one solution is to define your own language pseudoc as a copy of c as defined in lstdrvrs, and change the ' character from a b-type delimiter to a m-type (MATLAB-style) delimiter.

\documentclass{article}
\usepackage{listings}

% modified ANSI C definition; copied from lstdrvrs.dtx
\lstdefinelanguage{pseudoC}%
  {morekeywords={auto,break,case,char,const,continue,default,do,double,%
      else,enum,extern,float,for,goto,if,int,long,register,return,%
      short,signed,sizeof,static,struct,switch,typedef,union,unsigned,%
      void,volatile,while},%
  sensitive,%
  morecomment=[s]{/*}{*/},%
  morecomment=[l]//,% nonstandard
  morestring=[b]",%
  morestring=[m]',% changed from `b' to `m'
  moredelim=*[directive]\#,%
  moredirectives={define,elif,else,endif,error,if,ifdef,ifndef,line,%
  include,pragma,undef,warning}%
}[keywords,comments,strings,directives]%

\lstdefinestyle{pseudoc}{
language=pseudoC,
basicstyle=\small\sffamily,
numbers=left,
numberstyle=\tiny,
frame=L,
columns=fullflexible,
showstringspaces=false,
}

\lstnewenvironment{ccode}{
\lstset{style=pseudoc}}{}

\begin{document}
\begin{ccode}
  int x';
  // use x
  x = 3;
  y = 'a string'; // aside: this would be a compiler error :-)
\end{ccode}
\end{document} 

I added the last line of the listing to make sure normal string behavior is preserved:

enter image description here

  • 1
    Yes, that's one workaround. Nothing against your answer, but I think presenting that as pseudocode can be confusing. The quote in x' might lead students to believe that it's valid C syntax. – jub0bs Aug 17 '14 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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