Heavy quarks in a magnetic field