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.
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:49listings
. Why not use a package dedicated to typesetting pseudo-code instead oflistings
? – jub0bs Aug 17 '14 at 19:01x'
some kind of placeholder in your C code? – jub0bs Aug 17 '14 at 19:21