Package incompatibilites: etoolbox, hyperref, and bm, standalone?

(Martin has posted an answer, but there is a bit more to this!) The problem arises due to an issue with the definition of \@@end. This is where the LaTeX kernel stores the TeX \end primitive (so that \end can be reused for environments). The bm package does various tests for \@@end which rely on it being the primitive. What is happening is that etoolbox does

\patchcmd\enddocument
  {\deadcycles}
  {\let\AfterEndDocument\@firstofone
   \@afterenddocumenthook
   \deadcycles}
  {}
  {\let\etb@@end\@@end
   \def\@@end{%
     \let\AfterEndDocument\@firstofone
     \@afterenddocumenthook
     \etb@@end}}

which tries to patch \enddocument, and if it cannot then alters the definition of \@@end. In the standard classes, the patch works (it looks for \deadcycles), and so all is well. On the other hand, this fails for standalone as it redefines \enddocument as

\def\sa@@enddocument{%
  %\let\document\sa@orig@document
  \let\enddocument\sa@orig@enddocument
  \endinput
}

So in this case, etoolbox does it's 'fall-back' patch, and then the tests in bm fail.

If you do not need the end-document hook, the fix is easy

\makeatletter
\let\@@end\etb@end
\makeatother

I have now taken up maintenance of etoolbox and a fix has been applied to the code. The approach now taken is to delay redefining \@@end as late as possible using the \AtEndDocument hook.


The issues is that both the standalone class and the etoolbox package redefine the document environment. This causes a clash. You can avoid this by loading etoolbox before the class using \RequirePackage. I will try to support the normal of this package, but it will take a while until I have to time to release a new version of standalone.

Workaround:

\def\RemoveEToolbox{}% Shows that this works fine without {etoolbox}

\ifdefined\RemoveEToolbox
    \newcommand*{\iftoggle}[3]{#2}% Default to true value of "if"
\else
    \RequirePackage{etoolbox}
    \newtoggle{paper}
    \toggletrue{paper}

    %\renewcommand{\bm}[1]{#1}% Why can't I have a normal \bm with {etoolbox}?
\fi

\documentclass{standalone}

\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{bm}

% \usepackage{hyperref}

\begin{document}

This document is intended for 
\iftoggle{paper}{paper}{electronic}
distribution.

\bm{\textcolor{blue}{Solve $x^2-1=0$}}

\begin{align*}
    a &= b\\
    \iftoggle{paper}{
        c &= d\\
    }{}
    e &= f\\
\end{align*}
\end{document}

Update 2011/12/21

The new version 1.0 of standalone works fine with etoolbox, due to a changed way to patch the document environment. However, the new default settings ignore paragraph breaks, so you will a problem with align*. You can fix easily this by using the varwidth class option.