Context. Understanding the relation between the attenuation and the gas is fundamental for interpreting the appearance of galaxies. This can now be done down to a local scale in the local Universe, thanks to the high spatial resolution achievable from the far-ultraviolet (FUV) to the far-infrared (FIR). More importantly, this relation is also crucial for predicting the emission of galaxies. This is essential in semi-analytic models that link dark matter from large cosmological simulations, to the baryonic matter which we can observe directly. Aims. The aim of the present paper is to provide new and more detailed relations at the kpc scale between the gas surface density and the face-on optical depth directly calibrated on galaxies, in order to compute the attenuation not only for semi-analytic models, but also for observational models as new and upcoming radio observatories are able to trace gas ever farther away in the Universe. Methods. We have selected a sample of 4 nearby resolved galaxies and a sample of 27 unresolved galaxies from the Herschel Reference Survey and the Very Nearby Galaxies Survey, for which we have a large set of multi-wavelength data from the FUV to the FIR including metallicity gradients for resolved galaxies, along with radio HI and CO observations. For each pixel in resolved galaxies and for each galaxy in the unresolved sample, we compute the face-on optical depth from the attenuation determined with the CIGALE spectral energy distribution fitting code and an assumed geometry. We determine the gas surface density from HI and CO observations with a metallicity-dependent XCO factor. Results. We provide new, simple-to-use relations to determine the face-on optical depth from the gas surface density, taking the metallicity into account, which proves to be crucial for a proper estimate. The method used to determine the gas surface density or the face-on optical depth has little impact on the relations except for galaxies that have an inclination over 50. Finally, we provide detailed instructions on how to efficiently compute the attenuation from the gas surface density taking into account possible information on the metallicity. Conclusions. Examination of the influence of these new relations on simulated FUV and IR luminosity functions shows a clear impact compared to older, more-frequently used relations, which in turn could affect the conclusions drawn from studies based on large-scale cosmological simulations. © 2013 ESO.