TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!



share|improve this question
Not sure about the tags on this question. Feel free to edit them if anyone thinks of better tags. – Peter Grill Mar 13 '14 at 4:33
What is the practical application of this type of behavior? – cslstr Mar 13 '14 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. – Peter Grill Mar 19 '14 at 18:03
up vote 10 down vote accepted

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

share|improve this answer
Works great. Can you elaborate on "not clean" comment. – Peter Grill Mar 13 '14 at 4:50
@PeterGrill: You don't have to, but it's cleaner to close the group started by \begin. – Werner Mar 13 '14 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)

share|improve this answer
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. – Peter Grill Mar 13 '14 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 – David Carlisle Mar 13 '14 at 21:46
Ahh, I see. Yes, stopping at the first \begin should be fine. – Peter Grill Mar 13 '14 at 21:50
@DavidCarlisle: filecontents could be a problem with such a redefinition, since it relies on \begin...\end constructs. – Werner Mar 19 '14 at 18:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.