Nuclear magnetic resonance (NMR) experiments are usually carried out in homogeneous magnetic fields. In many cases, however, high-resolution spectra are virtually impossible to obtain because of the inherent heterogeneity of the samples or living organisms under investigation, as well as the poor homogeneity of the magnets (particularly when bulky samples must be placed outside their bores). Unstable power supplies and vibrations arising from cooling can lead to field fluctuations in time as well as space. We show how high-resolution NMR spectra can be obtained in inhomogeneous fields with unknown spatiotemporal variations. Our method, based on coherence transfer between spins, can accommodate spatial inhomogeneities of at least 11 gauss per centimeter and temporal fluctuations slower than 2 hertz.