Out-of-Distribution Generalization in Transformers via Latent Space Reasoning