6

I want to put some symbols in green color with lstdefinelanguage in latex, but it seems that symbols are not recognized.

The symbols I need to be colored are =, > and <. Is it possible? How can I do this?

The main file:

\documentclass{article}


\usepackage{graphicx,color}
\usepackage{booktabs}
\usepackage{listings}


\lstdefinelanguage{NeoIDL}{
  sensitive = true,
  keywords = [1]{module, resource, enum, annotation, for, import, entity, path,
  @get, @post, @put, @delete, require, ensure, otherwise, call},
  %ndkeywords={int, \char{}  },
  %ndkeywordstyle=\color{blue}\bfseries,
  morekeywords=[2]{>, <, ==},
  keywordstyle=[2]\color{green},
  numbers=left,
  numberstyle=\scriptsize,
  stepnumber=1,
  numbersep=8pt,
  showstringspaces=false,
  breaklines=true,
  frame=top,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{purple}\ttfamily,
  stringstyle=\color{red}\ttfamily,
  morestring=[b]',
  morestring=[b]"
  }

\begin{document}

\begin{figure}[htb]
\begin{small}
\lstinputlisting[language=NeoIDL,firstnumber=1]{store_pos_servico.neo}
\end{small}
\caption{Service 1}
\end{figure} 

\end{document}

The file to be formatted:

module store {
   (...)

   resource order  {
     path = "/order/{id}";

     @post   int postOrder (Order order)
    require (call store.getOrder(id) == "NotFound"),
    otherwise "InvalidPrecondition"
   };
}
  • (1) welcome, (2) please provide a full minimal example, such tjøhat we can test your code without having to add anything. That makes it a lot easier to provide advice – daleif Jun 9 '16 at 21:56
  • I did it now, @daleif ! – Lucas Lima Jun 9 '16 at 22:09
5

I have found myself in a similar situation, previously, and I was able to solve the problem in this way:

\documentclass{article}


\usepackage{graphicx,color}
\usepackage{booktabs}
\usepackage{listings}


\lstdefinelanguage{NeoIDL}{
  sensitive = true,
  keywords={},
  otherkeywords={% Operators
    >, <, ==
  },
  keywords = [2]{module, resource, enum, annotation, for, import, entity, path,
  @get, @post, @put, @delete, require, ensure, otherwise, call},
  keywordstyle=\color{green},
  keywordstyle=[2]\color{blue},% for example
  numbers=left,
  numberstyle=\scriptsize,
  stepnumber=1,
  numbersep=8pt,
  showstringspaces=false,
  breaklines=true,
  frame=top,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{purple}\ttfamily,
  stringstyle=\color{red}\ttfamily,
  morestring=[b]',
  morestring=[b]"
  }

\begin{document}

\begin{figure}[htb]
\begin{small}
\lstinputlisting[language=NeoIDL,firstnumber=1]{store_pos_servico.neo}
\end{small}
\caption{Service 1}
\end{figure} 

\end{document}

That is, define an empty set of class-1 keywords, add the symbols as “other keywords” belonging to this class, and shift all other types of keywords to higher order classes. But don’t ask why this works, because I don’t know either!

Here is the output I get:

Output of the code

  • Excellent, @Gustavo Mezzeti! I don't need to know how it works, too :). Only one more question: Is it possible to colorize the symbols of left and right parenthes '(' ')'? – Lucas Lima Jun 9 '16 at 23:37
  • @LucasLima: I’m sorry, the other times simply adding , (, ) to the otherkeywords list worked like a charm, but it seems that, in your example, the closing parenthesis is not recognized. Since, as I said, I cannot explain why the trick works when it works, I cannot explain why it doesn’t when it doesn’t either. (:-) – GuM Jun 10 '16 at 0:23

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.