Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

screenshot of listing generated by above code

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

share|improve this question
    
a trivial approach is to use the verbatim package. –  barbara beeton May 24 '12 at 12:20
add comment

4 Answers

up vote 14 down vote accepted

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}

enter image description here

share|improve this answer
add comment

Same as Gonzalo's answer but with some spice added...

\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
        commentstyle=\color[rgb]{0.133,0.545,0.133},    % comments
        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}

enter image description here

share|improve this answer
add comment

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:

enter image description here

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:

enter image description here

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}
share|improve this answer
    
@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
add comment

It is my own favorite settings:

enter image description here

\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},
    commentstyle=\color{cyan},
    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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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