[Tex/LaTex] Calling \end{document} in a macro

includemacros

I have a file, which is meant to be used as a fragment file inside a larger document:

\include{boilerplate}
content
\include{boilerplate}

The goal is to make a fragment file compilable. So inside boilerplate.tex, I have:

\ifx\maindocument\undefined
    \documentclass{article}
    \def\maindocument{}
    \begin{document}
    fragment is now standalone
\else 
    fragment is finished
    \end{document}
\fi

When I compile the fragment, I get:

(\end occurred when \ifx on line 1 was incomplete)

The "cause" of this is that inside my \ifx, I end the document and so the \ifx never finishes. However, everything seems to compile properly. How can I keep this general format, but make the error message go away? I tried putting an \expandafter{\end{document}} but that didn't help.


After incorporating the suggestions about \enddocument (or another macro), I was able to remove the error. However, a few remain:

(\end occurred when \iftrue on line 3 was incomplete)
(\end occurred when \ifnum on line 3 was incomplete)

Here is my boilerplate.tex now:

\def\prestuff{%
    \documentclass{article}
    \def\maindocument{}
    \begin{document}
    fragment is now standalone
}

\def\poststuff{%
    fragment is finished
    \end{document}
}

\ifx\maindocument\undefined
    \expandafter\prestuff
\else 
    \expandafter\poststuff
\fi

So what about these errors?

Best Answer

Another strategy for avoiding too many open conditionals (incidentally, all of these techniques have uses beyond the given one wherever you'd rather not next conditionals unnecessarily) is to define an "execute after" command which is set inside the conditional. So in this case,

\ifx\maindocument\undefined
    \documentclass{article}
    \def\maindocument{}
    \begin{document}
    fragment is now standalone
    \def\donext{}
\else 
    fragment is finished
    \end{document}
    \def\donext{\end{document}}
\fi
\donext

Obviously, you can add more stuff in to the \donext macro.

Update: This is in response to the new errors that you are getting. These come from the \include command. Basically, by ending the document in an included file, you aren't giving the \include command a chance to finish what it normally does. As this involves a few conditionals and other stuff (such as writing to aux files), TeX complains about this.

If you don't need all the fancy extra of \include then you can replace it by \input. One of the main things that \include gives you that \input doesn't is individual aux files for each included file. As you are including the file twice, I'd be concerned that the second one might overwrite the first anyway so would hesitate before using \include. If your "real life" situation is similar to the example in the question, then I don't see any benefit from using \include over \input so this replacement should be safe.

If you do still need to use \include then you could probably hack your way out of it by looking at the \include and the @include commands and putting everything after the \@input@ line in to your \poststuff macro. The following works for me, but I don't guarantee that there isn't something else I've overlooked!

\def\prestuff{%
    \documentclass{article}
    \def\maindocument{}
    \begin{document}
    fragment is now standalone
}

\makeatletter
\def\poststuff{%
    fragment is finished
    \clearpage
    \@writeckpt{boilerplate}%
    \if@filesw
      \immediate\closeout\@partaux
    \fi
  \else
    \deadcycles\z@
    \@nameuse{cp@boilerplate}%
  \fi
  \let\@auxout\@mainaux
    \fi
    \end{document}
}
\makeatother

\ifx\maindocument\undefined
    \expandafter\prestuff
\else 
    \expandafter\poststuff
\fi

(But as I said, using \input avoids this hassle.)

Related Question