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

I am writing my thesis using a college provided template which uses subfiles. I have was stuck for a few hours on an issue wherein some comments are being interpreted by the compiler when the subfiles package is used and a subfile is compiled.

For example, this is my main file named Main.tex:



My subfile named Hello.tex contains:

Hello world!

When the compile the main file (Main.tex), there is no problem. But when I try to compile the Hello.tex file, the compilation fails with the error:

! LaTeX Error: Missing \begin{document}.

When I remove the \end{document} comment in the main file, things are fine once again. Weirdly, there is no problem with other comments in the same place. Only an \end{document} comment seems to be interpreted.

Anyone know why this is happening?

share|improve this question
up vote 2 down vote accepted

The subfiles.cls works by reading the file given as document class option, but letting the document environment equivalent to the comment environment provided by verbatim. This environment reads its contents verbatim and it is delimited by the string \end{verbatim} (it's not completely correct, but it's not relevant) and percent characters are standard characters.

The comment environment implementation is quite easy: it does the same as verbatim, but when the latter prints a line, the former simply throws it out.

Thus "hiding" \end{document} behind a comment character serves no purpose.

In your case the %\end{document} line ends the implicit comment environment and \subfile{Hello} executes \skip@preamble which makes \documentclass, \document and \enddocument into no-ops. Thus Hello world is found when the check against paragraphs in the preamble is still active, because it's deactivated when the real \document command is executed, which here never is.

So, no \end{document} behind comments, sorry.

share|improve this answer

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.