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?


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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