Problem in listings, colored split command

MWE:

\documentclass[a4paper,11pt]{book}
\usepackage{color}
\usepackage{listings}

\lstdefinelanguage{mytex}[LaTeX]{TeX}{
alsoletter={\\},
morekeywords=[3]{\\mbox,},
literate={\{}{{\textcolor{blue}{\{}}}{1} {\}}{{\textcolor{blue}{\}}}}{1},
}

\lstset{language=mytex}

\lstdefinestyle{mystyle1}{
basicstyle=\normalsize\ttfamily,
keywordstyle=[3]{\color{green}},
}

\lstset{style=mystyle1}

\begin{document}

\begin{lstlisting}
\mbox{}
\end{lstlisting}

\begin{lstlisting}
f\mbox{}f
\end{lstlisting}

\end{document}


and displaying

How can I do green, second \mbox?

Note: @karlkoeller's answer image. There is one space f between \mbox{}f.

• alsoletter makes \ a letter. So to listings the string f\mbox is similar to, e.g., fimbox hence there's no intention at all of highlighting just “a part of a keyword”. Feb 24 '15 at 1:39
• @Manuel, I understand. Feb 24 '15 at 1:47
• @Özgür There is not any space in the output of my code. Anyway, see the edit. Mar 1 '15 at 15:17

The best way to highlight LaTeX commands is to use texcsstyle instead of keywordstyle and moretexcs instead of morekeywords.

In your case, \mbox is already listed as a LaTeX keyword, so you need first to undeclare it from the default list of keywords

deletetexcs={mbox}


and then to add it in the 3 list

moretexcs=[3]{part,mbox,chapter}


To highlight them as you wanted, add the option

texcsstyle=*[3]{\color{green}}


to your style (the asterisk tells listings to highlight \ as well).

MWE:

\documentclass[a4paper,11pt]{book}
\usepackage{color}
\usepackage{listings}

\lstdefinelanguage{mytex}[LaTeX]{TeX}{
deletetexcs={mbox},
moretexcs=[3]{part,mbox,chapter},
literate={\{}{{\textcolor{blue}{\{}}}{1} {\}}{{\textcolor{blue}{\}}}}{1},
}

\lstset{language=mytex}

\lstdefinestyle{mystyle1}{
basicstyle=\normalsize\ttfamily,
texcsstyle=*[3]{\color{green}},
}

\lstset{style=mystyle1}

\begin{document}

\begin{lstlisting}
\mbox{}
\end{lstlisting}

\begin{lstlisting}
f\mbox{}f
\end{lstlisting}

\end{document}


Output:

EDIT

If you don't like the standard listings spacing, use the option columns=fullflexible in your \lstset.

MWE:

\documentclass[a4paper,11pt]{book}
\usepackage{color}
\usepackage{listings}

\lstdefinelanguage{mytex}[LaTeX]{TeX}{
deletetexcs={mbox},
moretexcs=[3]{part,mbox,chapter},
literate={\{}{{\textcolor{blue}{\{}}}{1} {\}}{{\textcolor{blue}{\}}}}{1},
}

\lstset{language=mytex,columns=fullflexible}

\lstdefinestyle{mystyle1}{
basicstyle=\normalsize\ttfamily,
texcsstyle=*[3]{\color{green}},
}

\lstset{style=mystyle1}

\begin{document}

\begin{lstlisting}
\mbox{}
\end{lstlisting}

\begin{lstlisting}
f\mbox{}f
\end{lstlisting}

\end{document}


• There is one space f between \mbox{}f. Is there a solve? @karlkoeller. Feb 24 '15 at 13:23
• @Özgür There isn't any space in the output of my code. Feb 24 '15 at 15:48
• @karlkkoeller, add image in question. Mar 1 '15 at 13:20
• @Özgür See the edit. I confirm that there is no space in the output, anyway. Mar 1 '15 at 15:18
• Perfect working. Mar 1 '15 at 15:58