9

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:

enter image description here

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}
David Carlisle
  • 757,742
yannisl
  • 117,160
  • 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. – yannisl Mar 25 '12 at 11:26
  • @MarcoDaniel I would rather keep the running of the examples within the document and without --shell-escape. – yannisl 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
  • I might want to put the \section after \begin{document} if I were you :-) – ienissei Mar 25 '12 at 12:04
  • @StephanLehmke Code added. – yannisl Mar 25 '12 at 12:21
  • @ienissei Thanks:) Proof that \begin{document} was neutered! – yannisl Mar 25 '12 at 12:22
  • You should disable \@preamblecmds before the main \begin{document}: when it has acted it's impossible to recover the original definitions of the "preamble only" commands. However, using \globaldefs=-1 would break all packages that use \gdef or \xdef for their workings. And what about section numbers and the like? LaTeX relies on the fact that \stepcounter acts globally. – egreg Mar 25 '12 at 12:43
  • @egreg The idea as per this http://tex.stackexchange.com/questions/49245/show-output-of-a-full-latex-document/49248#49248 is to re-adjust the counters afterwards (as part of the package). Will amend \@premablecmds. – yannisl Mar 25 '12 at 13:29
  • @Yiannis: Thanks for the code. Unfortunately I can't experiment because apparently my TeX Distribution is too old ;-) So I'll just look what comes out of this. – Stephan Lehmke Mar 25 '12 at 16:57
  • @StephanLehmke You probably need an update on tcolorbox. – yannisl Mar 25 '12 at 19:09

1 Answers1

2

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}
David Carlisle
  • 757,742
ienissei
  • 6,223