The problem is that the \begin{...}
and \end{...}
pair commands automatically create a "group" so that, in effect, the &
and \\
are "out of scope" for the tabular, while inside the "production" environment they just show up at a place where the compiler is not expecting them.
A second problem with your definitions is that, even if they would work, they would be adding an extra \\
at the end of the tabular, adding an unwanted space at the end. Perhaps some more appropriate definitions would be
\documentclass[a4paper,10pt]{article}
\newcommand{\production}[1]{#1 ::= &}
\newenvironment{grammar}{\tabular{p{3cm}l}}{\endtabular}
\begin{document}
\begin{grammar}
\production{XmlStartTag} ... \\
\production{XmlOtherTag} ... \\
\production{XmlEndTag} ...
\end{grammar}
\end{document}
Note no \\
at the end of the last production. Also in the definition of the grammar
you don't need to repeat the work of \begin
/\end
, and you can instead directly use \tabular
and \endtabular
.
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.)
Best Answer
The default definition of
\Big
and friends sets the delimiter as a left delimiter, so having a subscript on it ends up being the first element in the delimited list and therefore does not get placed relative to the delimiter. This can be avoided by defining\Big
and friends in the opposite way: Make the delimiter a right delimiter, such that the subscript becomes a subscript of the whole delimited list.While this fixes the placement, it can have side effects on how the subscript is placed (mostly
\nulldelimiterskip
is zero and the style is always scriptstyle) but for most usecases this shouldn't matter.Given that
amsmath
defines\big
based on\bBigg@
it's enough to change the definition of that macro: