[Tex/LaTex] How to use a comma in lstlisting keywords

listings

I am creating a language for the doxygen tool. I need to create a set of new keywords. When I try to use the comma in one of them, it does not highlighting. I protect comma with a backslash, but it seems to be useless.

Does anyone know how I can use comma in new keywords ?

Here is what I have tried.

\lstdefinelanguage{doxygen}%
{
    alsoletter={\\,[,],/,*,\,},%
    morekeywords=[2]{/**,*/,///,\\brief,\\author,\\date,%
                     \\return,\\param[in,out],\\param[in],\\param[out],%
                     \\pre,\\post},%
    otherkeywords={},%
    morestring=[b]"
}

The problem is with the \param[in,out] keyword.

[EDIT]
Here is a minimal example

% !TEX TS-program = pdflatex
\documentclass[11pt]{article}

\usepackage[dvipsnames,usenames]{xcolor}
\usepackage{courier}
\usepackage{listings}

\lstdefinelanguage{doxygen}%
{
    alsoletter={\\,[,],/,*,\,},%
    morekeywords=[2]{/**,*/,\\brief,%
                     \\return,\\param[in,out],\\param[in],\\param[out]}
}

\lstdefinestyle{basic}{
    basicstyle={\small \ttfamily},
    keywordstyle={\bfseries\color{black}},
    linewidth={\textwidth}
}

\begin{document}

\begin{lstlisting}[language=doxygen,style=basic]
/** 
  \brief compute the gcd 

  \param[in] n the first number
  \param[in,out] m the second number and 
                   and gcd after the function returns
  \return terminaison code
 */
\end{lstlisting}

\end{document}  

Thank you.

Best Answer

It seems to me that the problem is not that you want \param[in,out] — and also \param[in] and \param[out] — to be keywords; the problem is that you would like arguments in square brackets to be typeset in the same way that a keyword would. You can do this by defining a special kind of "comment" (really, just a span of characters) which is delimited by [ and ], and which is typeset as a keyword would be.

Using your same pre-amble and example:

\newcommand\keywordstyle{\bfseries\color{black}}

\lstdefinelanguage{doxygen}{
  alsoletter={\\,/,*},
  morekeywords=[2]{/**,*/,\\brief,\\return,\\param},
  morecomment=[n][\keywordstyle]{[}{]}
}

\lstdefinestyle{basic}{
    basicstyle={\small\ttfamily},
    keywordstyle={\keywordstyle},
    linewidth={\textwidth}
}

Result:

Listing with hilighting

As I have defined the style for [...] blocks, it will correctly highlight nested pairs of brackets — wherever they occur, which may or may not be what you want for doxygen (which I am not familiar with).

Related Question