LaTeX forum ⇒ Document ClassesListings package: otherkeywords / comments question

Information and discussion about specific document classes and how to create your own document classes.
alastairandrew
Posts: 8
Joined: Sun Aug 31, 2008 10:08 pm

Listings package: otherkeywords / comments question

Postby alastairandrew » Sun Aug 31, 2008 10:25 pm

Hi there,

I've been using the Listings package to develop a new language definition to display some source code for an academic project I'm working on. The language I want to display has various assignment operators akin to <=, <-, :=: etc. and I've declared them in the otherkeywords section. The problem I'm having is that when styling my comments to appear green the operators declared as otherkeywords don't get styled by the comment style and remain their original keyword style. The confusing thing is that the otherkeywords style is set using the keywordstyle command and the keywords themselves behave normally within a commented section.

  1. % My language definition
  2. \lst@definelanguage[]{OPL}%
  3. {
  4. keywords={maximize, minimize, subject, to, forall, sum, solve, int, int+, float, float+, enum,
  5. ftoi, mod, abs, maxint, sqrt, ceil, floor, distToInt, frac, trunc, infinity, first, last, card, ord, next,
  6. prev, range, in, struct, prod, min, max, union, inter, not, initialize, var, dmin, dmax, dsize,
  7. bound, dnexthigher, alldifferent, circuit, distribute, try, endtry, tryall, if, endif, then, else, while, select,
  8. once, search, when, onValue, generate, generationMin, generationMax, generateSeq},
  9. otherkeywords={..,=,<,>,<=,=>,==},
  10. comment=[l][\color{eclipse-comments}]{//},%
  11. morecomment=[s][\color{eclipse-comments}]{/*}{*/},%
  12. string=[b][\color{eclipse-strings}]\``,%
  13. morestring=[b][\color{eclipse-strings}]',%
  14. basicstyle=\sffamily,
  15. keywordstyle=\color{eclipse-keywords},
  16. tabsize=4,
  17. showstringspaces=false,
  18. numbers=left, %% number lines
  19. numberstyle=\tiny, %% the style of the numbers in the side
  20. numberblanklines=true,
  21. showspaces=false,
  22. showtabs=false,
  23. }[keywords,comments,strings]%
  24.  
  25. \definecolor{eclipse-keywords}{rgb}{0.55,0,0.337}
  26. \definecolor{eclipse-strings}{rgb}{0.165,0,1}
  27. \definecolor{eclipse-comments}{rgb}{0.247,0.498,0.373}
  28.  
  29. % This is the test code I'm trying to format, the = within the /* */ comment style show up as the eclipse-keywords colour rather than
  30. % the correct eclipse-comments colour.
  31. \begin{lstlisting}[language=OPL]
  32. // A nice comment
  33. maximize
  34. sum(i in 1..n) c[i]*x[i]
  35. subject to
  36. forall (i in 1..n) sum(j in 1..n) d[i,j] <= s;
  37. /*
  38. *int i = 3;
  39. *float pi = 3.14;
  40. *enum Days {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};
  41. */
  42. \end{lstlisting}


If anyone could give me any pointers or advice I'd really appreciate it.

Link:
BBcode:
HTML:
Hide post links
Show post links

alastairandrew
Posts: 8
Joined: Sun Aug 31, 2008 10:08 pm

Postby alastairandrew » Sun Aug 31, 2008 11:07 pm

I found a way around the problem by using the classoffset and declaring the otherkeywords as more keywords instead.

For future reference here is the amended version:

% My language definition
\lst@definelanguage[]{OPL}%
{
classoffset=0,
keywords={maximize, minimize, subject, to, forall, sum, solve, int, int+, float, float+, enum,
ftoi, mod, abs, maxint, sqrt, ceil, floor, distToInt, frac, trunc, infinity, first, last, card, ord, next,
prev, range, in, struct, prod, min, max, union, inter, not, initialize, var, dmin, dmax, dsize,
bound, dnexthigher, alldifferent, circuit, distribute, try, endtry, tryall, if, endif, then, else, while, select,
once, search, when, onValue, generate, generationMin, generationMax, generateSeq},
keywordstyle=\color{eclipse-keywords}\bfseries,
classoffset=1,
morekeywords={..,=,<,>,<=,=>,==},
keywordstyle=\color{black}\bfseries,
classoffset=0,
comment=[l][\color{eclipse-comments}]{//},%
morecomment=[s][\color{eclipse-comments}]{/*}{*/},%
string=[b][\color{eclipse-strings}]\``,%
morestring=[b][\color{eclipse-strings}]',%
basicstyle=\sffamily,

tabsize=4,
showstringspaces=false,
numbers=left, %% number lines
numberstyle=\tiny, %% the style of the numbers in the side
numberblanklines=true,
showspaces=false,
showtabs=false,
}[keywords,comments,strings]%

Link:
BBcode:
HTML:
Hide post links
Show post links


Return to “Document Classes”

Who is online

Users browsing this forum: No registered users and 1 guest