# Highlight variables prefixed with % and $lstlisting I'm trying to get syntaxhighlighting comparable to this: (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 }}{} \definecolor{variableColor}{HTML}{AA7700} \definecolor{globalColor}{HTML}{FF1493} %\colorlet{globalColor}{ForestGreen!100} \colorlet{commentSColor}{ForestGreen!100} \colorlet{commentMColor}{ForestGreen!100} \colorlet{stringColor}{Blue!100} \colorlet{tagColor}{Blue!80!black} \definecolor{concatColor}{HTML}{008200} \colorlet{thisColor}{Red!100} \colorlet{identifierColor}{Blue} \definecolor{datablocksColor}{HTML}{444444} \definecolor{declarationColor}{HTML}{006699} \colorlet{functionColor}{white!30!black} \lstdefinelanguage{TorqueScript}{ basicstyle=\ttfamily, showstringspaces=false sensitive=false, 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=[2]{SPC,@,TAB}, keywords=[3]{this}, 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}, morestring=[s][\color{stringColor}]{"}{"}, morestring=[s][\color{tagColor}]{'}{'}, morecomment=[l][\color{commentSColor}]{//}, morecomment=[s][\color{commentMColor}]{/*}{*/}, % Variables moredelim=*[s][\color{variableColor}]{\%}{\ }, moredelim=*[s][\color{globalColor}]{\$}{\ },
}

\lstdefinestyle{TS}{
language=TorqueScript,
% Actual keywords
keywordstyle=[0]{\color{identifierColor}},
% Datablocks
keywordstyle=[1]{\color{datablocksColor}},
% Concatenators
keywordstyle=[2]{\color{concatColor}},
% this
keywordstyle=[3]{\color{thisColor}},
% functions
keywordstyle=[4]{\color{functionColor}},
% Actual keywords
keywordstyle=[5]{\bfseries \color{declarationColor}},
%
xleftmargin=\parindent,
%identifierstyle=\color{blue}
}

• – 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}

\makeatletter

% state variables''
\newif\ifincomment\incommentfalse
\newif\ifinstring\instringfalse
\newif\ifinTS
% --- patch to automatically highlight identifier starting by @
% (only outside strings and comments, though) ---

% local variables
\newif\if@identifierStartsByDollar@
\newif\if@identifierStartsByPercent@
\newcount\currentchar

\def\splitfirstchar#1{\@splitfirstchar#1\@nil}
\def\@splitfirstchar#1#2\@nil{\gdef\@testChar{#1}\gdef\@restTestChar{#2}}

\def\@testChar%
{%
% copy the first token in \the\lst@token to \@testChar
\expandafter\splitfirstchar\expandafter{\the\lst@token}%
%
% reset switch
\@identifierStartsByPercent@false%
\@identifierStartsByDollar@false%
%
% equality test
\ifthenelse{\equal{\@testChar}{\textdollar}}{\@identifierStartsByDollar@true}{}%
\ifthenelse{\equal{\@testChar}{\%}}{\@identifierStartsByPercent@true}{}%
%
% apply class style if not within string or comment
\ifinTS
\ifincomment
\else
\ifinstring
\else
\if@identifierStartsByPercent@
% Handle specialcase "%this"
\ifnum\pdf@strcmp{\@restTestChar}{this}=0
\def\lst@thestyle{\color{thisColor}}%
\else
\def\lst@thestyle{\color{variableColor}}%
\fi
\fi
\if@identifierStartsByDollar@
\def\lst@thestyle{\color{globalColor}}%
\fi
\fi
\fi
\fi
}
\let\@ddedToOutput\@testChar
\makeatother


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:

\ifthenelse{\equal{\@testChar}{\%}}{\@identifierStartsByPercent@true}{}%


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.