# Proper usage of otherkeywords option in listings

I'm trying to understand how listings' otherkeywords option really works. The documentation says about it:

Defines keywords that contain other characters, or start with digits. Each given 'keyword' is printed in keyword style, but without changing the 'letter', 'digit' and 'other' status of the characters. This key is designed to define keywords like =>, ->, -->, --, ::, and so on. If one keyword is a subsequence of another (like -- and -->), you must specify the shorter first.

Furthermore, keywords are defined as a 'letter' followed by a sequence of 'letter' or 'digit' characters.

So let's look at an example.

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\lstdefinelanguage{mylang}{
%    alsodigit={<,>,-,*},
otherkeywords={<,>,-,<-,->,<->,<--,-->,***},
morekeywords=[1]{-},
morekeywords=[2]{<,>,<-,->},
morekeywords=[3]{<->,<--,-->,***}
}

\begin{document}
\lstset{
language={mylang},
basicstyle=\ttfamily,
keywordstyle=[1]{\color{red}},
keywordstyle=[2]{\color{green}},
keywordstyle=[3]{\color{blue}}
}

\begin{lstlisting}
-                 red
< > <- ->         green
<-> <-- --> ***   blue
\end{lstlisting}
\end{document}


The output of the first line looks good, - should be treated as a class 1 keyword and be printed in red.

On the second line the confusion begins. < and > are correctly printed in green, but <- and -> aren't. The documentation (as noted above) says that keywords being subsequences of other keywords should be defined first. This is exactly what is done in the example code, the ouput is still wrong.

Even if listings would erroneously break up <- into < and -, I would expect the first < in <- to be printed in green. It's also printed in red, though, which is really confusing, because < never occurs in class 1.

The third line is also wrong, except for the *** keyword, which is probably because neither * nor ** occur as separate keywords.

If we don't skip over the first line in the otherkeywords description, especially the "or start with digits" part, changing <, > and - to 'digit' characters should satisfy all the conditions required by the documentation. However, adding alsodigit={<,>,-,*} makes no difference in the output.

Could anyone give more insights about what's going on here? I'm tempted to say this is a bug, or at least some extra information is missing in the documentation. Unfortunately, debugging macro expansion is too verbose to follow the exact procedure.

• Related in some way: tex.stackexchange.com/questions/472433/… – Steven B. Segletes May 8 at 19:03
• @Duplicate voters: Neither does the linked question ask for common keyword prefixes/subsequences nor does it address how the different catcodes of keyword characters are important nor does it answer why the package gives the result shown in my example. It just gives another example usage of the otherkeywords option. I'm not asking for some example that works correctly but want to know why the given example does not – siracusa May 9 at 12:34
• I agree. I in no way intended for the question to be closed as a result of my link. I have voted for a re-open. If I hadn't already upvoted, I would again. – Steven B. Segletes May 9 at 12:36
• Looks like a bug to me. – schtandard Aug 14 at 7:03
• @schtandard so it does to me. But it's annoying, and I don't have the time searching for a solution right now, hence the bounty. – Skillmon Aug 14 at 7:47