# How to illustrate and execute a MWE within another document

In Show output of a full LaTeX document I provided a partial solution to a similar question. The following code is an attempt to simulate the running of a full MWE within another document as for example a tutorial for LaTeX. The code produces an output as per the image below:

The top part of the box is the code and the bottom part the LaTeX output. The code sandboxes the begin{document}...\end{document} macros in order to disable them within the example code and only just typeset them.

I am looking for a method to import packages or classes at this point and only run them locally within the example. How can this be achieved?

An approach would be to just let ProvidesPackage etc... to empty, disable the \@preamblecmds and use globaldefs to ensure that the code only runs within a group.

\documentclass{article}
\usepackage[listings]{tcolorbox}
\usepackage{amsmath,amsfonts,lipsum}
\begin{document}
\makeatletter
\gdef\@preamblecmds{}
\def\documentclass#1{}
\def\usepackage#1{}
\let\Document\document
\let\Enddocument\enddocument
\def\document{}
\def\enddocument{}
\gdef\after@@{\let\enddocument\Enddocument\let\document\Document}
\newenvironment{tex}{
\tcblisting{boxrule=1pt}}
{\endtcblisting\aftergroup\after@@}
% auto example
\begin{tex}
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\section{Introduction}
Whence we conclude that $\gamma$ is a primitive root modulo $p$. But
\begin{align*}
\gamma^{p-1}-1 &=
g^{p-1} - 1 + \frac{p-1}{1!}g^{p-2}xp +
\frac{(p-1)(p-2)}{2!}g^{p-3}x^2p^2 + \ldots \\
&= p\left(kp + \frac{p-1}{1!}g^{p-2}x +
\frac{(p-1)(p-2)}{2!}g^{p-3}x^2p + \ldots\right).
\end{align*}
\end{document}
\end{tex}
\bigskip
\lipsum[1]
\end{document}

-
Can you work with --shell-escape? – Marco Daniel Mar 25 '12 at 11:20
Is there really merit in this approach? It seems so much easier to have a local TeX run for every example. Otherwise, take a look at the preview package. – Stephan Lehmke Mar 25 '12 at 11:22
@StephanLehmke There is merit while you writing the notes and during editing. Also it doesn't seem all that impossible to code. – Yiannis Lazarides Mar 25 '12 at 11:26
@MarcoDaniel I would rather keep the running of the examples within the document and without --shell-escape. – Yiannis Lazarides Mar 25 '12 at 11:27
You are talking about "the following code". Which code? Is it in your answer to the other question? – Stephan Lehmke Mar 25 '12 at 11:43

Perhaps you could use the docmute package (updated recently). It does what you want, although you still need to reset the counters by hand.

In the MWE, I have used it on your code (written inside the main document), and also on imported code from a testing.tex file, which you should create beforehand.

\documentclass{article}

\usepackage[listings]{tcolorbox}
\usepackage{amsmath,amsfonts,lipsum}

\usepackage{docmute}

\newenvironment{tex}
{\tcblisting{boxrule=1pt}}
{\endtcblisting\bigskip}

\newcommand{\inputtex}[1]{\tcbinputlisting{listing file=#1,boxrule=1pt}\bigskip}

\begin{document}

\begin{tex}
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\section{Introduction}
Whence we conclude that $\gamma$ is a primitive root modulo $p$. But
\begin{align*}
\gamma^{p-1}-1 &=
g^{p-1} - 1 + \frac{p-1}{1!}g^{p-2}xp +
\frac{(p-1)(p-2)}{2!}g^{p-3}x^2p^2 + \ldots \\
&= p\left(kp + \frac{p-1}{1!}g^{p-2}x +
\frac{(p-1)(p-2)}{2!}g^{p-3}x^2p + \ldots\right).
\end{align*}
\end{document}
\end{tex}
\setcounter{section}{0}

\lipsum[1]

\inputtex{testing.tex}

\lipsum[2]

\end{document}

-