Emph or morekeywords does not show any effect

I would like a number of selected (sub-)strings to become highlighted as keywords (or emphasized):

\documentclass{article}

\usepackage{filecontents}
\usepackage{listings}

\lstdefinestyle{MyStyle} {
boxpos=c,%
breaklines=true,%
showlines=false,%
numbers=left,   %numbers=none,%
numberstyle=\tiny,%
firstnumber=1,%
numbersep=5pt,%
showstringspaces=false,%
frame=none,%frame=single,%
basicstyle=\footnotesize\ttfamily,
keywordstyle=\underline,%
captionpos=b, %
literate={<-}{{$\leftarrow{}$}}2,
rangeprefix=\#\/\/\ ,
rangesuffix=\ \/\/}

\begin{filecontents*}{src.code}
#// start //
A  <- A 'a' a:B;
B  <- B 'b' b:<alnum>;
#// end //
\end{filecontents*}

\begin{document}

\lstset{includerangemarker=false,
style=MyStyle}
\lstinputlisting[linerange=start-end,alsoletter={:},morekeywords={A,B,a:,b:}]{src.code}

\end{document}


I simply fail to get this right. While, in the example, occurrences of "A", "B", "b:" get marked (underlined) as exptected, "a:" is not. I would appreciate some hints.

• I should add: I want the letters between grave accents to be highlighted, as well as the prefixes "a:" and "b:". – mrcalvin May 6 '18 at 22:57

In lisings, a keyword is always a complete word, so a maximal sequence of letters. After alsoletter={:}, a:B is a sequence of letters which is not a keyword, so it is no highlighted as a keyword. You can fix this by avoiding alsoletter={:}, then : is no longer a letter, so it can be used to separate words. Of course, thien you can no longer use morekeywords={a:,b:}. The \listings package has a special key otherkeywords for this purpose: otherkeywords contains keywords which do contain non-letters.

For example:

\documentclass{article}
\usepackage{filecontents}
\usepackage{listings}

\begin{filecontents*}{src.code}
#// start //
A  <- A 'a' a:B;
B  <- B 'b' b:<alnum>;
#// end //
\end{filecontents*}

\begin{document}
\lstinputlisting[keywordstyle=\underline,morekeywords={A,B,a:,b:},otherkeywords={a:,b:}]{src.code}
\end{document}


• A clear and consise answer, much appreciated! I wanted to fix two typos as beauty spots, but they are too minor: lisings -> listings, thien -> then ;) – mrcalvin May 7 '18 at 17:20