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.)
\parto look forward for\end{document}and to take appropriate action if it's found with "nothing" intervening. – egreg Jul 17 '11 at 13:20\catcode`\^^M=10would ignore empty lines in the input (they would be seen as spaces). This would force to use\parexplicitly in such files when a real paragraph is desired. – egreg Jul 17 '11 at 13:34\vbox{\leavevmode...}builds a paragraph with the current\hsize. – egreg Jul 17 '11 at 13:46