Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
Hi TH, thanks for your quick answer. This solution already works but it also highlights uppercase words within strings and comments. Do you know how to escape this macro if the word is within the mentioned environments or how to activate it only when it has to deal with programming logic? –  LeoR Oct 16 '10 at 19:27
@LeoR: Can you provide a sample? That would make it easier to test out. I'm actually sort of surprised that it would treat words in strings as identifiers since it should be using stringstyle for those instead. Same with comments. –  TH. Oct 16 '10 at 19:30
@TH Sure. I originally thought this problem can be abstracted to arbitrary programming languages so I didn't provide a concrete Prolog example. –  LeoR Oct 16 '10 at 19:47
@LeoR: And sure enough, neither Find nor Found are in boldface. You must be doing something more than you've shown. –  TH. Oct 16 '10 at 20:22
You are right. When I initially used the language setting Prolog, I had a variable called "Name" which was the only one which would be displayed boldface, although I don't know why. So I added \lstdefinelanguage{Prolog} { deletekeywords=[1]{Name} } that removed the boldface presentation of Name but I didn't realize that it also removed the rest of the Prolog related presentation. After I removed that your solution worked fine for me as well. Thanks again. –  LeoR Oct 16 '10 at 20:42
show 1 more comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.