# Avoid paragraph due to trailing empty line in standalone file

Using my standalone class and package it is possible to compile diagrams etc. inside own files stand-alone or as part of a main document. However, a common user error is to keep an empty line between the end of the content and the \end{document} which causes the diagram etc. to be part of a paragraph and the resulting PDF will be \linewidth wide. I'm looking now for possibilities to ignore such trailing lines or remove the paragraph they create.

The following MWE shows the issue and also includes a potential redefinition of the internal \sa@cls@beforeenddocument macro which is placed at the beginning of \end{document}. Maybe this issue can be solves with some \lastbox trickery.

\documentclass{standalone}
\usepackage{tikz}

\makeatletter
\def\sa@cls@beforeenddocument{%
\ifhmode
\unskip
\else
% ???
\fi
\endstandalone
}
\makeatother

\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (1,1);
\end{tikzpicture}

\end{document}


This produces:

The wanted result is:

(Shown is the full "page". The dark border is the background color of Adobe Reader.)

-
You can't undo what has already been put on the main vertical list. The only way I see is to redefine \par to look forward for \end{document} and to take appropriate action if it's found with "nothing" intervening. –  egreg Jul 17 '11 at 13:20
@egreg: :-( I wanted to avoid such things. –  Martin Scharrer Jul 17 '11 at 13:24
When a paragraph has been formed there's very little that can be done. Except acting through the output routine, but this would open a can of worms. Setting \catcode\^^M=10 would ignore empty lines in the input (they would be seen as spaces). This would force to use \par explicitly in such files when a real paragraph is desired. –  egreg Jul 17 '11 at 13:34
@egreg: And when I store the whole content in a saved vbox? Can I then do something more? The whole content should be placed on one page anyway. –  Martin Scharrer Jul 17 '11 at 13:41
The problem would be exactly the same: \vbox{\leavevmode...} builds a paragraph with the current \hsize. –  egreg Jul 17 '11 at 13:46

Setting \catcode\^^M=10 would ignore empty lines in the input (they would be seen as spaces). This would force to use \par explicitly in such files when a real paragraph is desired.
One can also think to redefine \par to look forward in order to see if \end{document} intervenes with "nothing" (except blank spaces or empty lines) intervening, but this can lead to other problems for normal text.
Thanks a lot. Not the answer I was hoping for, though. Maybe I implement my own preview like code which enforces a \hbox instead of \vbox and let users of the standalone class choice which one to use. –  Martin Scharrer Jul 20 '11 at 13:13
I think the varwidth environment/package might be useful here. It seems to handle this case well. –  Martin Scharrer Nov 13 '11 at 8:57
Tanks again @egreg. I overcame this issue in the new v1.0 of standalone by using either the varwidth environment from the package of the same name, now also the option name, and/or by boxing the content myself as part of the new crop option. –  Martin Scharrer Dec 21 '11 at 16:08