How to emphasize within a listing two successive identifiers separated by a space?

According to the listings package documentation this is an explicit impossibility. Quoted, from page 21 (with some emphasis):

Another problem: by default some characters are not allowed inside keywords, for example -, :, ., and so on. The reference guide covers this problem by introducing some more keys, which let you adjust the standard character table appropriately. But note that white space characters are prohibited inside keywords.

For small instances, there exists no viable alternative at the moment within listings. However, for larger or more general instances, the code examples included in the pgf package documentation (using the codeexample environment) may be of some help.


You can hijack the moredelim key to bend listings to your will... sort of. See below.

Note that this approach is far from perfect. For instance, with the code below, any character sequence starting by every axis x and ending with label will be highlighted like a bona fide keyword does. However, you might be able to put it to good use.

related: How to add a keyword with a blank space in Listings package?

enter image description here

\documentclass{article}

\setlength\parindent{0pt}

\usepackage{xcolor}
\usepackage{listings}

\lstdefinestyle{mytikzPGFstyle}{
    language = TeX,
    basicstyle = \ttfamily,
    keywordstyle = \color{blue},
    frame = single,
    moredelim = [s][keywordstyle]{every\ axis\ x\ }{label},
    moredelim = [s][keywordstyle]{every\ axis\ y\ }{label},
}

\begin{document}

Success:
\begin{lstlisting}[style=mytikzPGFstyle]
\pgfplotsset{
   every axis x label/.append style = {
      font = \small
   },
   every axis y label/.append style = {
      font = \small,
      rotate = -90,
      xshift = 0.5em
   }

   \draw node {every, axis, label, x}; 
}
\end{lstlisting}

Failure:
\begin{lstlisting}[style=mytikzPGFstyle]
every axis x foo label/.append style = {
\end{lstlisting}
\end{document}