Document ClassesListings package: otherkeywords / comments question

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

Listings package: otherkeywords / comments question

Post by alastairandrew »

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.

Code: Select all

% My language definition
\lst@definelanguage[]{OPL}%
	{
	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},
	otherkeywords={..,=,<,>,<=,=>,==},
	comment=[l][\color{eclipse-comments}]{//},%
	morecomment=[s][\color{eclipse-comments}]{/*}{*/},%
	string=[b][\color{eclipse-strings}]\``,%
	morestring=[b][\color{eclipse-strings}]',%
	basicstyle=\sffamily,
	keywordstyle=\color{eclipse-keywords},
	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]%

\definecolor{eclipse-keywords}{rgb}{0.55,0,0.337}
\definecolor{eclipse-strings}{rgb}{0.165,0,1}
\definecolor{eclipse-comments}{rgb}{0.247,0.498,0.373}

% This is the test code I'm trying to format, the = within the /* */ comment style show up as the eclipse-keywords colour rather than
% the correct eclipse-comments colour. 
\begin{lstlisting}[language=OPL]
// A nice comment
maximize
	sum(i in 1..n)  c[i]*x[i]
 subject to
	forall (i in 1..n) sum(j in 1..n) d[i,j] <= s;
/*	
*int i = 3;
*float pi = 3.14;
*enum Days {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};
*/
\end{lstlisting}
If anyone could give me any pointers or advice I'd really appreciate it.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

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

Re: Listings package: otherkeywords / comments question

Post by alastairandrew »

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=[\color{eclipse-strings}]\``,%
morestring=[\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]%
Post Reply