# listings package : highlight word composed by an alphabetic and a numeric part

I would like to highlight several keywords with an alphabetic and a numeric part with different color.

My problem is the number of keywords can become huge from r0 to r191 for example. And I have several keywords. Is there a way to escape to write all the possible keywords ?

Is there a way to define left delimiter just for a word and not for a line ?

The sample bellow do exactly what I want but with writing all the keywords (example keywords[2] and keywords[3])...

\documentclass[a4paper, 12pt,english]{article}
\usepackage{color}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkgreen}{rgb}{0.0,0.4,0.0}
\definecolor{darkred}{rgb}{0.6,0.0,0.0}

\usepackage{listings}
\lstdefinelanguage{mylanguage}
keywords=[2]{r0,r1,r2},
keywords=[3]{p00,p01,p10,p11},
moredelim=[s][\footnotesize\textit]{<}{>},
keywordstyle=[1]\normalsize\color{gray}\bfseries\ttfamily,
keywordstyle=[2]\color{darkgreen},
keywordstyle=[3]\color{darkred}}

\begin{document}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,language=mylanguage}
\begin{lstlisting}
p00 add r0 , r1 , r2
\end{lstlisting}

\end{document}

-
Welcome to TeX.sx! ... are you writing assembler? –  Sean Allred Feb 26 '13 at 15:52
yes, it is my assembler language –  user26467 Feb 26 '13 at 15:54
listings supports assembler, although only specific dialects. It would be useful to see how they define it for themselves; the structure is pretty consistent. I'm going to research where it defines the languages (grep to the rescue!), but two heads are better than one, no? –  Sean Allred Feb 26 '13 at 16:00
It looks like the answer won't come that easy. A quick look at listing's source code (search "Assembler") reveals that they do it the dumb way, too. There is probably a way to script this, but it won't use the listings package itself. Your best bet is some sort of text generating command, perhaps (depending on how \lstdefinelanguage works). –  Sean Allred Feb 26 '13 at 16:04
ok thanks, i will generate all my alphanumeric keywords ... a little "brute force" but ok –  user26467 Feb 26 '13 at 16:09

Huge hat tip to David, first of all, for providing a solution to a tricky intermediate problem. I couldn't have done this without his help.

You can do this quite easily by simply generating the list with LaTeX itself (it is Turing-complete, after all, hehe). Notice that this requires the forloop package (a really simple macro, actually).

\documentclass[a4paper, 12pt,english]{article}
\usepackage{color,forloop}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkgreen}{rgb}{0.0,0.4,0.0}
\definecolor{darkred}{rgb}{0.6,0.0,0.0}

\newcounter{ct}
\makeatletter
% Since we don't want an initial comma in the list, we use \@gobble
\def\rlist{\@gobble}
%  Using the counter ct, range its value from 0 to 191
\forloop{ct}{0}{\value{ct} < 192}{%
% \edef expands the definition before it actually
% binds the two.  Thus, \rlist expands like a chameleon's tongue.
\edef\rlist{\rlist,r\arabic{ct}}
}

\makeatother
\def\listwithrs#1\relax{%
\lstdefinelanguage{mylanguage}
keywords=[2]{#1},  % we stick in the list here
keywords=[3]{p00,p01,p10,p11},
moredelim=[s][\footnotesize\textit]{<}{>},
keywordstyle=[1]\normalsize\color{gray}\bfseries\ttfamily,
keywordstyle=[2]\color{red},
keywordstyle=[3]\color{darkred}}}

\usepackage{listings}

% Since we want to know the value of \rlist before we define the language,
% we use \expandafter to 'skip' \listwithrs so \rlist can be evaluated.
\expandafter\listwithrs\rlist\relax

\begin{document}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,language=mylanguage}
\begin{lstlisting}
p00 add r0 , r1 , r2 , r3 , r4
\end{lstlisting}

\end{document}

-
thanks!! How can I pass a second list argument to \listwithrs ? I tried \def\listwithrs#1#2... and \expandafter\listwithrs\rlist\plist\relax but unsuccessful –  user26467 Feb 26 '13 at 23:02
I tried the same thing at first. I'll have to direct you to David's solution again (linked above) for that answer. Be sure to upvote it!! –  Sean Allred Feb 26 '13 at 23:06