Flexible transition state theory for a variable reaction coordinate: Derivation of canonical and microcanonical forms