State-tracking first-principles determination of magnetocrystalline anisotropy.