# 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