In the manuals of my packages I use a custom package for code and examples which uses a combination of listings and mdframed for displaying the code. I haven't really intended to make it public (which is why it has a lot of quirks which should be addressed some time...) but it is available on CTAN. However: usage at your own risk!
\documentclass{article}
\usepackage[example]{cnltx}
\begin{document}
\begin{example}
This is a piece of \LaTeX\ code.
\end{example}
\begin{example}[code-only]
This is a piece of \LaTeX\ code.
\end{example}
\begin{example}[side-by-side]
This is a piece of \LaTeX\ code.
\end{example}
\begin{example}[side-by-side,code-left=false]
This is a piece of \LaTeX\ code.
\end{example}
\end{document}

This uses a custom listings style per default called cnltx. But this can be customized. The following example overwrites the style cnltx. There are also options to modify it instead of redefining it. The default style knows that the language is TeX or LaTeX and also knows a lot of (La)TeX macro names and environment names. It's layout depends on a color-scheme which can be changed. This means it is also possible to use the cnltx style with different colors
\documentclass{article}
\usepackage[example]{cnltx}
\lstdefinestyle{mystyle}{
language = [AlLaTeX]TeX,
basicstyle = {\ttfamily\small},
columns = fullflexible ,
texcsstyle = \color{blue},
}
\setcnltx{
listings-options = {style=mystyle} ,
frame-options = ,
code-sep = \dotfill
}
\begin{document}
\begin{example}
This is a piece of \LaTeX\ code.
\end{example}
\begin{example}[code-only]
This is a piece of \LaTeX\ code.
\end{example}
\begin{example}[side-by-side]
This is a piece of \LaTeX\ code.
\end{example}
\begin{example}[side-by-side,code-left=false]
This is a piece of \LaTeX\ code.
\end{example}
\end{document}

It is also possible to display complete documents. This means that shell-escape must be enabled – at least for one run in order to produce the PDF that then is included.
% arara: pdflatex: { shell: on }
\documentclass{article}
\usepackage[example]{cnltx}
\setcnltx{
add-cmds = lipsum
}
\begin{document}
\begin{example}[compile]
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum
\end{document}
\end{example}
\end{document}

Again, there are a number of options to influence the behavior.
% arara: pdflatex: { shell: on }
\documentclass{article}
\usepackage[example]{cnltx}
\setcnltx{
add-cmds = lipsum
}
\begin{document}
\begin{example}[compile,program=lualatex,runs=1,pages=1,
graphics={trim={5cm 18cm 5cm 7cm},clip}]
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum
\end{document}
\end{example}
\end{document}

The package has quite a number of further features like macros \cs{cs name}, \env{env name}, ... (those macros use the same color-scheme as the cnltx listings style), it supports indexing of macro and environment names and so on.