1

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

enter image description here

How can I do green, second \mbox?

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

enter image description here

3
  • 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”.
    – Manuel
    Feb 24 '15 at 1:39
  • @Manuel, I understand.
    – Özgür
    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
5

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:

enter image description here


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} 

enter image description here

5
  • There is one space f between \mbox{}f. Is there a solve? @karlkoeller.
    – Özgür
    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.
    – Özgür
    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.
    – Özgür
    Mar 1 '15 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.