Yes, you did read the title correctly!
I want to ignore everything that is between \begin{document}
and \end{document}
when I \input
a file. I thought that that would be very simple, just use the environ
package:
\RenewEnviron{document}{}
But it turn out not be that easy. The MWE below yields the correct results:
but only with the comments intact. If you un-comment the commented lines then end up with:
LaTeX Error: \env@document@save@env undefined
Interestingly, ignoring this error one can continue past this, and still get the correct results. :-)
Notes:
- I realize that in there can be other things in the preamble, so this is by no means a general solution. For the places where I need this functionality, the preamble only has
\documentclass
and two\input
/\usepackage
which loads all the.sty
files needed so this will suffice for my needs.
Code:
\documentclass{article}
\usepackage{environ}
\usepackage{standalone}
\newtoks{\MyToken}
\usepackage{filecontents}
\begin{filecontents*}{foo.tex}
\documentclass{standalone}
\usepackage{MyStandardPackages}
\MyToken={foo}
% \begin{document}
% lots of text here
%
% \SetSomeVarable{\SomeVar}{Some Value}
%
% \begin{SomeEnvironment}
% lots more stuff here as well
% \end{SomeEnvironment}
% \end{document}
\end{filecontents*}
\MyToken={XXX}
\newcommand{\DisablePreamble}{%
\renewcommand{\documentclass}[2][]{}% remove def'n of \documentclass
\renewcommand{\usepackage}[2][]{}% ignore any \usepackage{}
% \RenewEnviron{document}{}% Ignore everything within the "document" environment.
}%
\newcommand*{\ExtractMyToken}[1]{%
\begingroup% What happens in the group stays in the group!
\DisablePreamble%
\input{#1}%
\global\MyToken=\expandafter{\the\MyToken}%
\endgroup%
}%
\begin{document}
MyToken=\the\MyToken
\ExtractMyToken{foo}
MyToken=\the\MyToken
\end{document}
\begin{document}
. I needed to extract this meta data (but none of the content) in another file. Typically this kind of stuff is done in TeX via an.aux
file but I was trying to avoid a temporary file, and just extract the data directly.