Style LaTeX Code in lstlisting

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings,xcolor}

\lstdefinestyle{mystyle}
{
language=[LaTeX]{TeX},
texcsstyle=*\color{blue},
basicstyle=\ttfamily,
moretexcs={mycommand}, % user command highlight
frame=single,
}
\begin{document}

\begin{lstlisting}[style=mystyle]
\documentclass{article}
\usepackage[T1]{fontenc}
\newcommand*{\mycommand}{Hello World!}
\begin{document}
\mycommand
\end{document}
\end{lstlisting}

\end{document}