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:


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. :-)


  • 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.






%   \begin{document}
%       lots of text here
%       \SetSomeVarable{\SomeVar}{Some Value}
%       \begin{SomeEnvironment}
%           lots more stuff here as well
%       \end{SomeEnvironment}
%   \end{document}


    \renewcommand{\documentclass}[2][]{}%  remove def'n of \documentclass
    \renewcommand{\usepackage}[2][]{}%     ignore any \usepackage{}
%   \RenewEnviron{document}{}% Ignore everything within the "document" environment.

    \begingroup% What happens in the group stays in the group!



  • Not sure about the tags on this question. Feel free to edit them if anyone thinks of better tags. Mar 13, 2014 at 4:33
  • 2
    What is the practical application of this type of behavior?
    – cslstr
    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. Mar 19, 2014 at 18:03

2 Answers 2


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

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

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


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

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

I'd (locally) define


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


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

  • 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. 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 Mar 13, 2014 at 21:46
  • Ahh, I see. Yes, stopping at the first \begin should be fine. Mar 13, 2014 at 21:50
  • @DavidCarlisle: filecontents could be a problem with such a redefinition, since it relies on \begin...\end constructs.
    – Werner
    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 .