Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
I wouldn't use \include and prefer \input. The errors are indeed caused by \include. –  egreg Jul 28 '11 at 8:55
    
I've updated my answer in response to your updated question. –  Loop Space Jul 28 '11 at 9:00
add comment

5 Answers

up vote 11 down vote accepted

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

share|improve this answer
add comment

Wehn TeX reads your input, it executes \end{document} before the \fi. Inside \end{document} there is the TeX \end primitive, and it should never be used without closing all conditionals. So you can either use the low-level \endocument macro (as suggested by mhp):

  ...
  \expandafter\enddocument
\fi

or for a more general solution, use your own macros to deal with the conditional material

  ...
  \def\mymacro{\end{document}}
  \expandafter\mymacro
\fi

The later approach allows you to include multiple items to be inserted after the \fi.

share|improve this answer
add comment

Don't use \include except for including independent chapters in the main text body. Use the raw inclusion command \input instead, then the remaining warnings will disappear.

share|improve this answer
add comment

Instead of expandafter\end{document}, try \expandafter\enddocument.

share|improve this answer
add comment

The standalone package could achieve what you want.

%boilerplate.tex
\documentclass{standalone}
\begin{document}
Content of boilerplate
\end{document}

%main.tex

\documentclass{article} %or whatever
\usepackage{standalone}

\begin{document}
\include{boilerplate} % Perhaps input would be better?
Other text
\include{boilerplate}
\end{document}

Make sure any packages you use in boilerplate are included in the main document. standalone automatically strips out the preambles of the included file.

share|improve this answer
add comment

Your Answer

 
discard

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.