Open gsalzer opened 3 months ago
A workaround is to split preamble.tex
at the line \begin{document}
into \preamble1.tex
and preamble2.tex
and to put \begin{document}
into the main file.
% main.tex
\input{preamble1.tex}
\begin{document}
\input{preamble2.tex}
\XXX
\end{document}
% preamble1.tex
\documentclass{article}
\newcommand\XXX{The command XXX is defined.}
\usepackage{subfiles}
% preamble2.tex
% Everything from preamble.tex that came after \begin{document}
% In this example, there was nothing, so this file is empty.
The following issue was raised on tex.stackexchange.com in the post "Undefined control sequence. \begin{document}" in subfile when compiling in TeXstudio.
Consider the following files:
When LaTeXing
sub.tex
, we get the following error:The problem arises because when encountering
\begin{document}
inpreamble.tex
, thesubfiles
package skips the rest of the file (here:preamble.tex
) and assumes that it has thus skipped the rest of the document. However, in the example above, there is still the rest ofmain.tex
, which means that\XXX
will be processed as part of the preamble. As\XXX
generates output, we get the observed error message.Alternatively, when loading, in
preamble.tex
, the packagesubfiles
with the optionv1
:we get the error
Here, the lines after
\begin{document}
are skipped as a macro argument delimited by\end{document}
. As argument parsing is interrupted when reaching the end of the filepreamble.tex
and not continued in the main file, we get the error shown above.