# How to make a LTXexample-like environment by using the package listings.sty only?

For some reason, I am forced to create a LTXexample-like environment.

The following is my effort, but it cannot work. :-)

I don't know how to fix the problem.

\documentclass[dvips,dvipsnames]{article}
\usepackage{xcolor}

\usepackage{listings}
\lstset{%
literate=   {ï}{}0
{»}{}0
{¿}{}0,
breaklines=true,
breakindent=0pt,
basicstyle=\ttfamily\tiny,
keywordstyle=\color{blue}\sffamily\bfseries,
stringstyle=\rmfamily,
showstringspaces=false,
backgroundcolor=\color{Yellow!30},
language={PSTricks}%
}

\usepackage{pstricks}

\newcommand{\temporary}{}

\newenvironment{myLTXexample}%
{%
\renewcommand{\temporary}%
\bgroup\ignorespaces%
}
{%
\egroup%
\begin{minipage}{0.4\linewidth}%
\begin{lstlisting}
\temporary%
\end{lstlisting}
\end{minipage}
%
\hspace{5mm}
%
\begin{minipage}{0.4\linewidth}
\temporary
\end{minipage}
}

\begin{document}

\begin{myLTXexample}
testing.... \LaTeX !
\end{myLTXexample}

\end{document}


EDIT 1: This example is the simplified version of my real scenario. :-)

-
\renewcommand{\temporary}%? –  Kilgore Dec 19 '10 at 3:46
@Kilgore, ????? –  xport Dec 19 '10 at 4:00
why can't you use LTXexample? Your example defines nothing new? –  Herbert Dec 19 '10 at 7:57
@Herbert, I need more flexible way to put code region and formatted region. For example, I want the code part in the left cell and the formatted part in the right cell of a tabular row. There are many formations I want to make that is difficult to obtain using LTXexample. –  xport Dec 19 '10 at 8:48
then look at the package environ, it can collect environments and lets you use it later. –  Herbert Dec 19 '10 at 14:27

Here is a working solution with fancyvrb. The & before \tabularnewline is needed, the code itself is read by the hedaer definition.

\documentclass{article}

\usepackage{pstricks,fancyvrb,array,listings}
\lstset{basicstyle=\ttfamily\small}

\def\endExample{\end{VerbatimOut}
\input{\jobname.tmp}}
\newcommand\Example{%
\VerbatimEnvironment
\begin{VerbatimOut}{\jobname.tmp}}

\begin{document}

\begin{tabular}{@{} m{0.5\linewidth}@{}
>{\lstinputlisting{\jobname.tmp}}m{0.5\linewidth} @{}}

\begin{Example}
\pspicture(3,2)
\psframe*[linecolor=blue!30](3,2)
\endpspicture
\end{Example}
&
\tabularnewline

\begin{Example}
\pspicture(3,2)
\psframe*[linecolor=red!30](3,2)
\endpspicture
\end{Example}
&
\tabularnewline

\end{tabular}

\end{document}

-
thanks for your help. It is very satisfactory result. 19 hours must be elapsed to send the bounty. –  xport Dec 22 '10 at 13:26

You can't collect your listing in a command "for later use". listings has to change a lot of catcodes and this won't work when the code has already been read (catcodes are fixed at this moment). You would need \scantokens to reset them.

Also you can't hide \end{lstlisting} deep in some code: listings has to find the end of the listing without executing the code in between. That's why listings has its own \newenvironment command (\lstnewenvironment).

If you want to write your own LTXexample you will have to copy the method of showexpl. Basically LTXexample does this:

\newenvironment{LTXexample}
{\lst@BeginWriteFile{\SX@codefile}} %At start of env begin to write the code to a file
{\lst@EndWriteFile % stop writing to file
%now reinput code file \SX@codefile "as listing"
%reinput code file "as result"
}

-