6

I am using listings to display custom keywords in bold. In my listings, those keywords are sometimes followed by a comma. However, in the example below, any comma following a keyword will prevent the keyword from being recognized.

Rendering of the code below

How can I make listings recognize CustomKeyword and if as keywords, even when they're immdiately followed by a comma ?

\usepackage{lstlistings}

\lstdefinelanguage{csharp}{
  alsoletter={@,=,>,},
  morekeywords={return, if, is, int, CustomKeyword, else, or, bool},
  sensitive=true,
  morecomment=[l]{//},
  morecomment=[s]{/*}{*/},
  morestring=[b]"
}

\lstset{
  language=csharp,
  showstringspaces=false,
  columns=spaceflexible,%fullflexible,
  mathescape=true,
  numbers=none,
  numberstyle=\tiny,
  basicstyle=\codestyle
} 

\begin{lstlisting}[columns=fullflexible]
CustomKeyword  // OK in bold.
CustomKeyword, // Not in bold anymore.
if //OK in bold
if, // Not in bold anymore
\end{lstlisting}
5
  • I'm sure you mean "comma", but just in case you really did want your code to have a "coma", we have answers for that at tex.stackexchange.com/questions/122116/…. I am just having fun, of course. Welcome to the site. Jan 22, 2015 at 14:48
  • Please turn your code into a minimal working example (MWE). Write alsoletter=@=>, instead of alsoletter={@,=,>,},; the latter defines , as a "letter", which means that listings treats it as a valid character in identifiers and keywords.
    – jub0bs
    Jan 22, 2015 at 14:48
  • 1
    Thank you for pointing this out @StevenB.Segletes now corrected Jan 22, 2015 at 14:51
  • Corrected spelling will help for future searches of the topic. Jan 22, 2015 at 14:51
  • @Jubobs your solution works. Please post it so that it will help people looking for the same answer. Jan 22, 2015 at 14:53

1 Answer 1

5

The problematic line is

alsoletter={@,=,>,},

You shouldn't use commas to delimit the characters you want to declare as "letters". If you do, listings takes those commas literally and declares the comma itself as a "letter"; as a result, listings treats if, as an identifier distinct from if.

Correct alsoletter syntaxes include

alsoletter={@=>},

and even (much less readable!)

alsoletter=@=>,

(Note that the comma at the end of that line is simply interpreted as a delimiter for listings key-value pairs, not as a character declared as "letter".)

enter image description here

\documentclass{article}

\usepackage{listings}

\lstdefinelanguage{csharp}{
  alsoletter={@=>},
  morekeywords={return, if, is, int, CustomKeyword, else, or, bool},
  sensitive=true,
  morecomment=[l]{//},
  morecomment=[s]{/*}{*/},
  morestring=[b]",
}

\lstset{
  language=csharp,
  showstringspaces=false,
  columns=spaceflexible,%fullflexible,
  mathescape=true,
  numbers=none,
  numberstyle=\tiny,
} 

\begin{document}
\begin{lstlisting}[columns=fullflexible]
CustomKeyword  // OK in bold.
CustomKeyword, // in bold too :)
if //OK in bold
if, // in bold too
\end{lstlisting}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .