3

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.

1
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – jub0bs
    Nov 26, 2014 at 18:46

1 Answer 1

3

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.