# LaTeX Source Code Listing

I want a code listing, but the code I'm listing is LaTeX. When I simply use:

\usepackage{listings}
...
\begin{lstlisting}
...LaTeX document goes here...
\end{lstlisting}


The listing looks like this:

I want it to look a little more professional than this. Can anyone help me out?

-
a trivial approach is to use the verbatim package. – barbara beeton May 24 '12 at 12:20

Use \lstset to customize your listings; select the specific language and a nice monospaced font; for example:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{bera}
\usepackage{listings}

\lstset{%
basicstyle=\small\ttfamily,
language=[LaTeX]{TeX}
}

\begin{document}

\begin{lstlisting}
\documentclass{article}
\usepackage{listings}

\begin{document}
Hello world!
\end{document}
\end{lstlisting}

\end{document}


-

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,
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,
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}

-
@Forgiver: Full LaTeX files do appear to work fine, at least when the output produced is one page. Do you have an example that does not work? – Peter Grill May 24 '12 at 5:52
Yes I mentioned in the Notes section that the package must be loaded in the main file. – Peter Grill May 24 '12 at 6:09
@Forgiver: Different \documentclass still produces output (see updated images). However, the effect of changing \documentclass will not be apparent. – Peter Grill May 24 '12 at 6:27
@Forgiver: Yes, like I said the \documentclass and \usepacakge are ignored. So if you want to show something specific to a class that you are not using you need to define those macros beforehand. – Peter Grill May 24 '12 at 6:34

\documentclass{article}
\usepackage{listings,xcolor}
\begin{document}

\lstset{%
basicstyle=\small\ttfamily,language={[LaTeX]TeX},   numbersep=5mm, numbers=left, numberstyle=\tiny, % number style
breaklines=true,frame=single,framexleftmargin=8mm, xleftmargin=8mm,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
backgroundcolor=\color{green!5},frameround=fttt,escapeinside=??,
rulecolor=\color{red},
morekeywords={% Give key words here                                         % keywords
maketitle},
keywordstyle=\color[rgb]{0,0,1},                    % keywords
stringstyle=\color[rgb]{0.627,0.126,0.941}  % strings
%columns=fullflexible
}%

\begin{lstlisting}
\documentclass{article}
\usepackage{listings}
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle
Hello World!
% This is a comment.
\end{document}
\end{lstlisting}

\end{document}


-

It is my own favorite settings:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\lstset
{
language={[LaTeX]TeX},
%alsolanguage={PGF/TikZ},
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{red},
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
basicstyle=\small\tt,
keywordstyle=\color{blue}\sf,
identifierstyle=\color{magenta},
backgroundcolor=\color{yellow!10},
tabsize=2,
columns=flexible,
}

\begin{document}

\begin{lstlisting}
\documentclass{article}
\usepackage{listings}
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle
Hello World!
% This is a comment.
\end{document}
\end{lstlisting}

\end{document}

-