9

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:

enter image description here

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}
3
  • Not sure about the tags on this question. Feel free to edit them if anyone thinks of better tags. Commented Mar 13, 2014 at 4:33
  • 2
    What is the practical application of this type of behavior?
    – cslstr
    Commented Mar 13, 2014 at 5:57
  • @cslstr: I have numerous files with a standard header and I use tokens to specify meta data for each file before \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. Commented Mar 19, 2014 at 18:03

2 Answers 2

10

This is a common problem with environ, and the way I get around it is to define those commands it has a problem with:

\makeatletter
\providecommand{\env@document@save@env}{}% To keep environ happy
\providecommand{\env@document@process}{}%
\RenewEnviron{document}{}% Ignore everything within the "document" environment.
\makeatother

A quick-and-dirty way of achieving what you're after is to use

\renewcommand{\document}{\endgroup\endinput}

which terminates the reading of the \input document as soon as it reaches \begin{document}.

2
  • Works great. Can you elaborate on "not clean" comment. Commented Mar 13, 2014 at 4:50
  • @PeterGrill: You don't have to, but it's cleaner to close the group started by \begin.
    – Werner
    Commented Mar 13, 2014 at 4:51
5

I'd (locally) define

\def\begin#1{\endinput}`

so the file stops at that point, or perhaops (I didn't understand your use case really

 \def\documentclass[#1]#2{\endinput}

and stop even earlier (but if you are doing that just not inputting the file would be easier)

4
  • Not sure I understand you first bit of code. Did you mean \def\document{\endinput}? I just want to disable the document environment, so want everything before \begin{document} to be executed, with \documentclass and \usepackage being the only exceptions. Commented Mar 13, 2014 at 21:40
  • @PeterGrill well I meant what I wrote you could fudge \document instead but I did \begin it's rare to have any environment before document so stopping at the first \begin seemed reasonable Commented Mar 13, 2014 at 21:46
  • Ahh, I see. Yes, stopping at the first \begin should be fine. Commented Mar 13, 2014 at 21:50
  • @DavidCarlisle: filecontents could be a problem with such a redefinition, since it relies on \begin...\end constructs.
    – Werner
    Commented Mar 19, 2014 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .