Featured Book

LaTeX Beginners Guide

Who is online

In total there are 5 users online :: 0 registered, 0 hidden and 5 guests (based on users active over the past 5 minutes)
Most users ever online was 1327 on Tue Nov 05, 2013 7:11 pm

Users browsing this forum: No registered users and 5 guests

Partner Sites

TeXwelt - Fragen und Antworten goLaTeX
TeXblog
TeXnicCenter
TeXample

Listings package: otherkeywords / comments question

Add tags Information and discussion about specific document classes and how to create your own document classes.

Listings package: otherkeywords / comments question

Postby alastairandrew on Sun Aug 31st, 2008

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  •  Open in writeLaTeX
% 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.
alastairandrew
 
Posts: 8
Joined: Sun Aug 31st, 2008

Re: Listings package: otherkeywords / comments question

Postby alastairandrew on Sun Aug 31st, 2008

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]%
alastairandrew
 
Posts: 8
Joined: Sun Aug 31st, 2008


Return to Document Classes

Who is online

Users browsing this forum: No registered users and 5 guests