I'm trying to get syntaxhighlighting comparable to this: Sample highlighting

(with $-prefixed variables as well) But I'm having issues with the %-prefixed identifiers. I tried doing it with

moredelim=*[s][\color{variableColor}]{\%}{\ }

Which has the obvious issue that it doesn't stop highlighting it if you seperate keywords with a dot. I also tried the solution proposed bu Jubobs here but I could not make it work with the % or $ marks.

This is my current code:

\lstnewenvironment{TorqueScript}{\lstset{ style=TS }}{}


    keywords=[0]{if, else, },
    keywords=[1]{StaticShapeData, ParticleEmitterData, ParticleEmitterNodeData, ParticleData, ParticleEmitterNode,ClientGroup, SimGroup, SimObject,
        GuiControl, GuiBitmapBorderControl, GuiBitmapControl, GuiControlProfile, GuiTextListCtrl, GuiScrollCtrl, GuiTextCtrl, GuiPanel, PlayerData, Material, StaticShapeData,}
    keywords=[4]{delete, messageboxok, exec, echo, getcount, commandtoclient, commandtoserver, schedule, getObject, addObject, bind, getRowNumById, addRow, sortNumerical, clearSelection, setRowById, removeRowById, getWord, setWord, getWordCount, getField, setField, StripMLControlChars}, 
    keywords=[5]{function, datablock, singleton, new},
    % Variables
    moredelim=*[s][\color{variableColor}]{\%}{\ },
    moredelim=*[s][\color{globalColor}]{\$}{\ },

    % Actual keywords
    % Datablocks
    % Concatenators
    % this
    % functions
    % Actual keywords
    keywordstyle=[5]{\bfseries \color{declarationColor}},
  • See tex.stackexchange.com/questions/142582/…. – jub0bs Dec 2 '14 at 16:58
  • alsoletter=\% – jub0bs Dec 2 '14 at 17:02
  • Sorry if I wasn't specific enough but I need to be able to distinguish between %-prefix and $-prefix (so they can have different colors) and using the "identifier style" highlights too much, i.e. identifiers without the prefix as well, which it shouldn't. Thanks for the help though! – LukasPJ Dec 2 '14 at 17:51
  • @Jubobs since you found this already, maybe you could assisst me on the code you posted in the other thread? It seems to fail because it tries to up-quote the $-sign on the $-prefixed variables which results in the following error: "Improper alphabetic constant. $" I believe that code could do the trick if it worked with % and $. – LukasPJ Dec 2 '14 at 17:58

I expanded on Jubobs solution and by lots of research on the internet came up with a solution that handled this case.

\lstnewenvironment{TorqueScript}{\lstset{ style=TS }\inTStrue}{\inTSfalse}


% ``state variables''
% --- patch to automatically highlight identifier starting by @
% (only outside strings and comments, though) ---

% local variables


    % copy the first token in \the\lst@token to \@testChar
    % reset switch
    % equality test
    % apply class style if not within string or comment
    % Handle specialcase "%this"

With that code which is largely just an expansion upon Jubobs code, I was able to get the following result:


Most notable difference is that I had to use:


Because the \ifnum would complain about "Improper alphabetic character".

Also worth to notice is the \ifinTS I added to make sure this didn't affect other languages.

|improve this answer|||||

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.