If you are typsetting LaTeX code and want to show the output, I would recommend the showexpl
package as it shows the code and the output in one step:

Code:
\documentclass{article}
\usepackage{showexpl}
\usepackage{xcolor}
\usepackage{lipsum}
\lstdefinestyle{myLatexStyle}{
language=TeX,
basicstyle=\small\ttfamily,
backgroundcolor=\color{yellow},
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
commentstyle=\color{red},
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
morekeywords={align,begin},
pos=l
}
\begin{document}
\noindent
Here is a basic example:
\begin{LTXexample}[style=myLatexStyle,width=0.60\linewidth]
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle
Hello World!
% This is a comment.
\end{document}
\end{LTXexample}
\end{document}
It was suggested in the comments that the LTXexample
environment can not typeset a complete LaTeX input file with \documentclass
and \usepackage
. This does not appear to be the case as these two basic examples illustrate:

Since the LTXexample
environment is designed to show the effect of code snippets, multi-page documents do not not show so well.
Notes:
- One restriction I have found is that the packages must be included in the main file. So note that to show the use of the
\lipsum
package in the example below, it necessitated updating the preamble to include that.
- Even though the example here shows a different document class, it appears that
\documentclass
is ignored which can be shown by using \documentclass{foobar}
which still produces output.
Code:
\documentclass{article}
\usepackage{showexpl}
\usepackage{xcolor}
\usepackage{lipsum}
\lstdefinestyle{myLatexStyle}{
language=TeX,
basicstyle=\small\ttfamily,
backgroundcolor=\color{yellow},
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
commentstyle=\color{red},
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
morekeywords={align,begin},
pos=l
}
\begin{document}
\noindent
Here is a basic example:
\begin{LTXexample}[style=myLatexStyle,width=0.60\linewidth]
\documentclass{article}
\usepackage{listings}
\begin{document}
Hello world!
\end{document}
\end{LTXexample}
\noindent
Here is an example of using the \texttt{lipsum} package:
\begin{LTXexample}[style=myLatexStyle,width=0.60\linewidth]
\documentclass{book}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\end{document}
\end{LTXexample}
\end{document}
verbatim
package.