[Tex/LaTex] listings – use different style for normal comments /* and doxygen comments /**

commentshighlightinglistings

listings package allows setting basicstyle, keywordstyle, identifierstyle,
commentstyle, stringstyle. Is it possible to set different style for comments that start from /** not just /*?

To make my question more clear. Assume I have set environment

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor}
\usepackage{listings}

\usepackage[scaled=0.92]{beramono}
\usepackage[scaled=0.92]{berasans}
\usepackage[scaled=0.92]{beraserif}

\lstnewenvironment{MyCode}
{\lstset{
language=C++,
basicstyle=\fontfamily{fvm}\selectfont,
keywordstyle=\bfseries, 
identifierstyle=\color{blue},
commentstyle=\fontfamily{fve}\selectfont\color{olive}, 
stringstyle=\fontfamily{fvs}\selectfont,
frame=single,showstringspaces=false,columns=flexible} }
{}

and I have the following code

\begin{document}
\begin{MyCode}
/*
 Multi-line 
 non-Doxygen comments
*/
#include <iostream>

/** Single-line Doxygen comment */
class MyClass {
private: 
  int value; /**< After member Doxygen comment */
public:
  /** 
   * Multi-line Doxygen comment
   * @param int x new value
   * @return bool operation success
   */
  bool setValue(int x /**< [in] also Doxygen */) {
    this->value = x; /* non-Doxygen comment */
    std::cout << "I'm setting value " << x << std::endl;
    /* Single line non-Doxygen comment */
    return true;
  }  
};
\end{MyCode}
\end{document}

There are mixed:

  • usual comments starting with /*
  • Doxygen (javadoc) comments starting with /**

/** itself starts with /* so all comments are formatted according to

commentstyle=\fontfamily{fve}\selectfont\color{olive},  

Is it possible to distinguish between those comments and set another style for /** comments? For example color them red not olive.

Best Answer

The listings package allows for multiple comment delimiters, but only one comment style. However, you can get around this limitation by using the moredelim key. In your particular example, you could write something like

moredelim = [s][\color{ForestGreen}]{/**}{*/}

and the style used for "comments" delimited by \** and *\ will be different to that used for comments delimited by /* and */.

enter image description here

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{listings}
\usepackage[usenames, dvipsnames]{xcolor}

\lstnewenvironment{MyCode}
  {\lstset{
    language         = C++,
    basicstyle       = \ttfamily,
    keywordstyle     = \bfseries,
    identifierstyle  = \color{blue},
    commentstyle     = \color{olive},
    moredelim        = [s][\color{ForestGreen}]{/**}{*/},
    stringstyle      = \color{magenta},
    frame            = single,
    showstringspaces = false,
    columns          = flexible}
  }{}

\begin{document}

\begin{MyCode}
/*
 Multi-line
 non-Doxygen comments
*/
#include <iostream>

/** Single-line Doxygen comment */
class MyClass {
private:
  int value; /**< After member Doxygen comment */
public:
  /**
   * Multi-line Doxygen comment
   * @param int x new value
   * @return bool operation success
   */
  bool setValue(int x /**< [in] also Doxygen */) {
    this->value = x; /* non-Doxygen comment */
    std::cout << "I'm setting value " << x << std::endl;
    /* Single line non-Doxygen comment */
    return true;
  }
};
\end{MyCode}

\end{document}