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 have read the listing manual document. I would like to use the lstsample environment.

It looks like below:

example

Basically, the output is on the left and the code is placed on the right.

But I don't find where this environment is defined. Is it easy to define a new environment like this?

share|improve this question
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 1 at 19:15

3 Answers 3

up vote 5 down vote accepted

The complete package listings can be found at CTAN.

http://www.ctan.org/pkg/listings

The package provides a huge dtx-file and a Makefile. Inside the dtx-file you can find the definition (see code below).

However if you use the make all the compilation produces the file listings-devel.pdf where you can find the user documentation and the developer documentation.

If this part is to hard please have a look at the package showexpl

% \subsection{The \texttt{lstsample} environment}
%
% \begin{environment}{lstsample}
% We store the verbatim part and write the source code also to file.
%    \begin{macrocode}
\lst@RequireAspects{writefile}
%    \end{macrocode}
%    \begin{macrocode}
\newbox\lst@samplebox
\lstnewenvironment{lstsample}[3][]
    {\global\let\lst@intname\@empty
     \gdef\lst@sample{#2}%
     \setbox\lst@samplebox=\hbox\bgroup
         \setkeys{lst}{language={},style={},tabsize=4,gobble=5,%
             basicstyle=\small\ttfamily,basewidth=0.51em,point={#1}}
         #3%
         \lst@BeginAlsoWriteFile{\jobname.tmp}}
    {\lst@EndWriteFile\egroup
%    \end{macrocode}
% Now |\lst@samplebox| contains the verbatim part.
% If it's too wide, we use atop and below instead of left and right.
%    \begin{macrocode}
     \ifdim \wd\lst@samplebox>.5\linewidth
         \begin{center}%
             \hbox to\linewidth{\box\lst@samplebox\hss}%
         \end{center}%
         \lst@sampleInput
     \else
         \begin{center}%
         \begin{minipage}{0.45\linewidth}\lst@sampleInput\end{minipage}%
         \qquad
         \begin{minipage}{0.45\linewidth}%
             \hbox to\linewidth{\box\lst@samplebox\hss}%
         \end{minipage}%
         \end{center}%
     \fi}
%    \end{macrocode}
% The new keyword class \keyname{point}.
%    \begin{macrocode}
\lst@InstallKeywords{p}{point}{pointstyle}\relax{keywordstyle}{}ld
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{lstxsample}
% Omitting |\lst@EndWriteFile| leaves the file open.
%    \begin{macrocode}
\lstnewenvironment{lstxsample}[1][]
    {\begingroup
         \setkeys{lst}{belowskip=-\medskipamount,language={},style={},%
             tabsize=4,gobble=5,basicstyle=\small\ttfamily,%
             basewidth=0.51em,point={#1}}
         \lst@BeginAlsoWriteFile{\jobname.tmp}}
    {\endgroup
     \endgroup}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\lst@sampleInput}
% inputs the `left-hand' side.
%    \begin{macrocode}
\def\lst@sampleInput{%
    \MakePercentComment\catcode`\^^M=10\relax
    \small\lst@sample
    {\setkeys{lst}{SelectCharTable=\lst@ReplaceInput{\^\^I}%
                                  {\lst@ProcessTabulator}}%
     \leavevmode \input{\jobname.tmp}}\MakePercentIgnore}
%    \end{macrocode}
% \end{macro}
%
share|improve this answer

Here's a rude way to do it so abusing the minipage environment:

\begin{figure}[H]
\begin{minipage}[b]{0.5\linewidth}
\begin{lstlisting}[frame=trBL]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.5\linewidth}
{\setstretch{.9} 
\begin{verbatim}
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\end{verbatim}
}
\end{minipage}
\end{figure}

You might as well need the packages to make it work:

\usepackage{setspace} %make the \setstrech 
\usepackage{listings} %lstlisting function
\usepackage[spanish]{babel} %i work under spanish documents so if it doesnt work put this
\usepackage{float} %this one is important so the text before and after the object whit [H] will work
share|improve this answer

As mentioned by Marco Daniel in his answer, the LTXexample environment from the showexpl package easily allows you to display source code beside the output that that produces. Here is an example:

enter image description here

Usually the LTXexample environment is used to display the LaTeX source code and the associated output:

enter image description here

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{showexpl}

\lstdefinestyle{ListingSample}{
    basicstyle=\small\ttfamily,
    numbers=none,
    keywordstyle=\color{blue}\bfseries,
    morekeywords={begin,end,for,maxint,to,do},
    pos=l,
}


\lstdefinestyle{myLatexStyle}{
    language=TeX,
    basicstyle=\small\ttfamily,
    numbers=none,
    backgroundcolor=\color{yellow},
    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
    commentstyle=\color{red},
    showstringspaces=false,
    keywordstyle=\color{blue}\bfseries,
    pos=l,
}

\begin{document}
\begin{LTXexample}[style=ListingSample]
\begin{lstlisting}
for i:=maxint to 0 do
begin
    { do nothing }
end;
\end{lstlisting}
\end{LTXexample}


\begin{LTXexample}[style=myLatexStyle]
\newcommand{\Command}[1]{%
   \texttt{\textbackslash#1}%
}%

Inline math is specified within 
a pair of \$ as in $E = mc^2$.

Display math uses \Command{[}
and \Command{]}.
For example: \[ E = mc^2 \]
\end{LTXexample}

\end{document}
share|improve this answer

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.