# LaTeX documentation with verbatim environment

I am trying to document some LaTeX macros concerning marginals. I think a good way for the documentation would be the LaTeX code on the verso page and the output on the recto page. So I came up with this MWE:

\documentclass{article}

\usepackage{lipsum}
\usepackage{verbatim}

\newenvironment{documentationpage}%
{\endgraf\verbatim}%
{\endverbatim}

\begin{document}
%
\begin{documentationpage}
This is a sentence explaining the footnote macro.\footnote{This is a footnote.}
This is a sentence explaining the footnote macro.\footnote{This is a footnote, too.}
\end{documentationpage}
%
\end{document}


Now, I would like to typeset This is a sentence...footnote, too.} first with and then without the framing documentationpage environment.

So, the output is supposed to be similar to

\begin{documentationpage}
This is a sentence explaining the footnote macro.\footnote{This is a footnote.}
This is a sentence explaining the footnote macro.\footnote{This is a footnote, too.}
\end{documentationpage}
\newpage
This is a sentence explaining the footnote macro.\footnote{This is a footnote.}
This is a sentence explaining the footnote macro.\footnote{This is a footnote, too.}
\newpage


Can I achieve this with (Lua-)LaTeX?

There is the showexpl package which allows to display code and result. You might be able to redefine its style to add the \newpage.

Another package is ydoc-expl from my ydoc bundle. It is still in alpha stage, however. You can redefine \PrintExample to change how the code example is displayed. For your application you can use the following code.

The package stores the code into an external file which can be included as code using e.g. \lstinputlisting or typeset using \input.

\documentclass{article}
\usepackage{ydoc-expl}

\lstdefinestyle{examplecode}{%
basicstyle=\ttfamily\small,
breaklines,
numbers=none,language=tex,
}

\makeatletter
\def\PrintExample{%
\lstinputlisting [style=examplecode]{\ydoc@exafile}
\newpage
\noindent
\input{\ydoc@exafile}%
\newpage
}
\makeatother
\begin{document}

\begin{examplecode}
This is a sentence explaining the footnote macro.\footnote{This is a footnote.}
This is a sentence explaining the footnote macro.\footnote{This is a footnote, too.}
\end{examplecode}

\end{document}


### Update:

Here the underlying code extracted and modified so that you have the requested environment name:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{listings}
\makeatletter
\lst@RequireAspects{writefile}

\lstdefinestyle{documentationpage}{%
basicstyle=\ttfamily\scriptsize,
breaklines,
language=[latex]tex,
}

\lstnewenvironment{documentationpage}{%
\setbox\@tempboxa\hbox\bgroup% to avoid any spaces sneaking in
%\lstset{gobble=2}% settings for code extraction
\lst@BeginWriteFile{\jobname.dpg}%
}{%
\lst@EndWriteFile
\egroup
\endgraf\noindent
\lstinputlisting[style=documentationpage]{\jobname.dpg}%
\newpage
\input{\jobname.dpg}%
\newpage
}
\makeatother

\begin{document}

\begin{documentationpage}
This is a sentence explaining the footnote macro.\footnote{This is a footnote.}
This is a sentence explaining the footnote macro.\footnote{This is a footnote, too.}
\end{documentationpage}

\end{document}


### Result (for both shown implementations)

• That is great. The first code gives me a ! Undefined control sequence. \endexamplecode ...begingroup \MakePercentComment \catcode =5\relax \Prin... l.24 \end{examplecode}` (and works when ignoring it) and the second one works fine. – Andy Nov 13 '11 at 21:40
• @Andy: Great. It works for me, but maybe just with the latest version. :-( Good that I added the second code then. – Martin Scharrer Nov 13 '11 at 21:44