# Using \end in internal vertical mode

Please how do I stop at all cost in the following? Note: \leavevmode is no rescue.

\documentclass[crop]{standalone}
\def\z#1#2{%
\ifnum#1<\numexpr#2+1\relax
\noindent\hfill#1%
\ifnum#1=#2\relax
\hfill\null\endgraf
\else
\ifdefined\xz
\hfill\number\numexpr#1+1\hfill\null\endgraf
\else
\let\xz\relax\hfill\null\endgraf
\fi
\fi
\expandafter\z\expandafter{\number\numexpr#1+2\expandafter}%
\expandafter{\number#2\expandafter}%
\fi
}
\begin{document}
\z19
\stop
\end{document}


-
The output image you've attached appears to be produced only if \documentclass{article} is used, but not if \documentclass[crop]{standalone} is in effect. Am I missing something? –  Mico Jul 7 '12 at 2:31
Could you say a little more what you mean by "stop at all costs"? It seems that the loop terminates, at least; do you mean you want to stop executing in the middle of the loop? –  Ryan Reich Jul 7 '12 at 4:01
With the MWE as written, you get ! Missing } inserted. as the error: replace the crop option by preview to get ! You can't use \end' in internal vertical mode., which relates more to the title. –  Joseph Wright Jul 7 '12 at 6:36
@Mico: Joseph Wright has answered the question. I didn't look in standalone.sty to see the boxing that it does. Ditto Ryan Reich. –  Ahmed Musa Jul 8 '12 at 2:10

With the standard settings, standalone class uses an \hbox to work. You can see this if you add
\showthe\currentgrouplevel

before \stop: the reported values are group level 1 and group type 2 ('hbox group'). You simply cannot use the \end primitive inside a box and expect all to be well. Moreover, even if you test for a box using the e-TeX extensions, closing it does not help as the content then disappears as you get no output. So with a complex set up such as the standalone class you really do have to 'stick to the rules' in terms of interface, or write everything yourself.
@AhmedMusa Possibly, but if there is more than once group level then life gets extremely complicated. In that case, you'll need to insert code which will then check again on the group type, and insert \stop` only when you are 'safe'. –  Joseph Wright Jul 8 '12 at 8:15