[Tex/LaTex] Emphasize word beginning with uppercase letters in code with lstlisting package


Hi I am using the lstlisting package to format my Prolog code within my tex file.
In Prolog all variables begin with an uppercase letter.
I would like to emphasize them.
Is there a way to for example add all uppercase letters to the keyword list or adjusting their style. I imagine there is something like this:


I tried


That made everything except for strings bold.

The solution should also only apply to the actual programming language, s.t. uppercase words within strings and comments are not changed.

In the following example FoundClass and Message would be emphasized, but Find and Found not since they are either within a comment or a string.

somerule(Message,FoundClass) :-
    %Find property
    atomic_list_concat(['Found class which fulfills property:' ,FoundClass],Message).

Thanks for your help.

Best Answer

There's nothing quite so simple. However, you can give your own macro as the argument to identifierstyle and that can examine the \lst@token token list to see if the list starts with an uppercase letter. I'm not sure how robust this solution is though.



I only tested it on the QuickSort example on Wikipedia.

This answer is slightly difficult to figure out exactly what it's doing so let me try to explain. The listings package will use the execute the \idstyle macro whenever it is trying to typeset an identifier and the actual text will be in the \lst@token token register. So all \idstyle does is it expands the token register using \the\lst@token and then \id@style will expand.

\is@style has a delimited argument (#2) which will scoop up every thing up to the \relax token. That is, #1 will contain the first token and #2 will contain all of the rest. Then, the category code of #1 will be compared to that of \relax. This is true if #1 is a control sequence and false otherwise. The second if checks that the token matches its own uppercase code; that is, it checks if the token is an uppercase character. If it is, then \bfseries executes.

Related Question