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 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.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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).

share|improve this answer
    
That's really brilliant ! I will try this. Thank you. –  Benoît Fraikin Aug 31 '11 at 18:00
    
Someone should accept the answer...and I don't get how the comments are made here? There is no comment symbol e. g. for "compute the god". How is it made? –  Manuel Kuehner Oct 20 '12 at 9:33
    
@manuel.kuehner: looks like Benoit has come back to accept it. I'm not sure what you mean by "a comment symbol" for compute the gcd. Could you elaborate? –  Niel de Beaudrap Oct 23 '12 at 14:17
    
@Niel: Hello! For example \param[in,out] m the second number and. The m the second number and part is a comment, right? How is a comment marked? How does listings know that this is a comment here? In other languages there a spefific comment symbols like % or # or /*. –  Manuel Kuehner Oct 23 '12 at 18:57

You need to redefine the way , is treated by listings through the experimental literate programming feature. This uses the literate[*]<replacement>...<replacement> macro.

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

The above does not define , to be alsoletter. However, literate=*{|}{,}{1} replaces the occurrence of | to , of width 1. Note the star *, as suggestion by the package documentation:

The optional star indicates that literate replacements should not be made in strings, comments, and other delimited text

After the replacement, \param[in|out] becomes \param[in,out], which is recognized as a keyword in \morekeywords[2]{...,\\param[in,out],...}.

Your code segment now has this form:

\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 termination code
 */
\end{lstlisting}

Literate programming in listings

An easy way out would have been to merely use ; instead of , (adding it to alsoletter or alsodigit, of course).

share|improve this answer
    
Thank you for your solution. I would greatly have prefer to do not change the comma. The problem takes place in the slides of one of my course. Therefore, it is a little annoying to modify each listing before putting it in beamer. The \param[in,out] is a doxygen command. However, if I need to, I will. Thanks again. Does your answer mean that the comma cannot be "captured" ? –  Benoît Fraikin Aug 9 '11 at 12:02
    
@Benoit: I'm not sure, but that could be the case. Not sure how listings parses a comma-separated lists... –  Werner Aug 11 '11 at 8:13

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.