I am a PhD student in A.I. that is using the listings package to render its code snippets.

I am trying to define a language environment for Drools - a quite famous Production Rules System - that is meant to display the snippets much like the Eclipse perspective used to edit rules.

Despite all my efforts, I still have a few issues/questions that I cannot solve by myself. Could you please give me some help? First of all, an MWE with a Drools code snippet:




    alsoletter={:, },
    morekeywords=[3]{declare,end,extends,from,insert,modify,not,new,no loop,query,retract,rule,salience,then,this,when, eval},
    morekeywords=[4]{after,before,during,over window:length,over window:time,overlaps},
    morekeywords=[5]{{no loop},over window:length,over window:time},
%%% default style
    % identifierstyle=\normalfont,


declare MyEvent
  length  : long = 0 @[2]
  time    : long     @[1]

rule "Transformation rule for 'Initiates' objects"
no loop
    $e: event, $ep: eventPtrn, 
    $f: fluent, $fp: fluentPtrn, 
    $c: context )
  Helper h = Helper.getInstance(); 
  // 'h' is a wrapper to ease conversion with $*
  String r = h.transform($e, $ep, $f, $fp, $c);

rule "Resulting rule"
  $e: TurnOn( $t: time )
  $f: lightOn() // a query returning this fluent instance
  holdsAt( powerAvail(), $t )
  insert(new Declip($e, $f, $t));


And finally the questions:

  1. Why have I to add a \lstset{morekeywords={}} statement just after \usepackage{listings} to avoid warnings on compilation on every $ symbol within my listing?
  2. How do I introduce keywords composed of two or more words? I am trying to accomplish that by using alsoletter={:, } but it does not work with the whitespace. I also tried with \ but with no luck. Just in case you are about to suggest me to define length,loop,no,over,time,window as single-word keywords, consider that it is not possible because otherwise valid identifiers as length (see the declare piece of code) would get the format of keywords.
  3. How do I format integer and real numbers? Is there any more elegant (or simple) way to do that other than the two methods already present on this site? Such workarounds are quite cumbersome and difficult to maintain when I do change something...
  4. How do I properly highlight variables (they are introduced by a $ symbol in Drools)? In the MWE I am trying to exploit keywordsprefix=\$, but I get those special keywords highlighted in comments too: is there any way to tell listings not to look in comments as it is possible to do with moredelim? Is this a bug? Shall I report it to the maintainer of the package? Just to add more details to the discussion: I also tried to use moredelim=[l][\color{variableBlue}\itshape]\$, but all the rest of the line get highlighted too. Finally I also tried with moredelim=[s][\color{variableBlue}\itshape]{\$}{:},moredelim=[s][\color{variableBlue}\itshape]{\$}{\ },etc. but it seems I cannot have more delimiters with the same starting symbol; in addition, termination symbol gets highlighted too while it should not (and if I introduce an asterisk in front of the pattern, both the starting and terminating symbols are hidden which is, again, not what I expect).
  5. I have found no mention of how to specify default formats for a language (remember, I am planning to obtain an environment that looks the same to the Drools editor): usually it is totally decoupled, so a user may introduce its own style with a \lstset{...} block. However, am I doing something completely fool by adding formats to the \lstdefinelanguage{...} block? Is there any best practice to follow?

Thanks in advance for your time.

closed as not a real question by Joseph Wright Dec 2 '12 at 10:58

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome to TeX.SE. – Peter Grill Nov 2 '12 at 2:29
  • 1
    I think you should split this into separate questions so that then those questions and answers will be more likely to help future visitors as they will (in theory) be more easily to locate should someone else have a similar question. If the questions are related, then perhaps they should be combined into one. Upon a cursory read, it appears that 1 and 4 might be related issues, so if you agree then making those into one question would make sense. – Peter Grill Nov 2 '12 at 2:31
  • 1
    As it stands, this does not fit the site format: we really do need one issue per question. – Joseph Wright Dec 2 '12 at 10:59