I am using the Latex Listings package to include code in a document. I need to implemented syntax highlighting similar to what you see in a standard IDE.

I've been able to assign colors to comments, keywords, and strings, but I am having some trouble figuring out how to color the numbers properly.

Here's an example of how it should look:

As you can see, anytime a word after the equals sign starts with a digit, all following characters until (but not including) the next semicolon or space are colored. When a word starts with a digit, all characters in the word should be colored, even if they are not digits. Otherwise, none of the digits are colored.

As of right now, I cannot think of a case where a word before the equals sign would start with a digit, so it will probably be acceptable to find a solution that colors all words starting with a digit.

In this case, digits are: 0-9.

Here's an MWE in Latex, but as you can see it doesn't have any coloring for the numbers.

Thanks for linking those threads. However, neither addresses the issue of coloring numbers only after the equals sign. I've tried the solutions in both threads, but they still color numbers that occur to left of the = sign, which is not what I want.
– LynNov 6 '12 at 21:19

So just to clarify: you want the entire text of anything that starts with a digit following an equal sign (with possible a leading space) to be colored up until the closing semi-colon. No other digits are to be colored. So, can it be assumed that the "digits" that are to be colored restricted to [a-fA-F0-9h]? Are there other characters that may show up here?
– Peter GrillNov 6 '12 at 21:35

1

You shoudl also adjust the text: "if the first digit after the equals sign is a number, all following digits until (but not including) the next semicolon are colored". Also can you provide a list of all the characters that are digits. You example include h. Any other characters?
– Peter GrillNov 6 '12 at 21:52

1

There is also an issue with your comment: "...that starts with a digit", and since you include a in this list, then doesn't that mean that the a1 should be colored? I think I know what you mean, but you should review to make sure that you have all the cases properly specd.
– Peter GrillNov 6 '12 at 21:55

2 Answers
2

Notes:

I have used newtoggle from the etoolbox package as I prefer that syntax versus the \newif syntax. But if you don't want to include an additional package it should be pretty straightforward to adapt this to use \newif or some other conditional methods.

Further Enhancements:

This does not work if there is a non-digit within a number. So 16hxFF will not be properly displayed, but 16x (with trailing space) is fine.

Review of Peter Grill's approach

Any non-digit within a number breaks the highlighting of this number (as reported by Peter).

Numbers get partially highlighted within strings, which seems to be a bug; I guess numbers shouldn't be highlighted at all in strings.

listings is unable to recognise (and properly highlight) an identifier if the latter contains any character subject to a literate replacement. Here, for instance, return is not recognised as a keyword (and therefore not highlighted in red) because it contains the character e, which is subject to a literate replacement.

An alternative approach

Here is a similar but more robust approach, which remedies all three of the limitations discussed above. The only obvious problem is code duplication. Edit: see egreg's answer for a much leaner approach!