  Inversion codes for the polarized radiative transfer equation can be used to
infer the temperature $T$, line-of-sight velocity $v_{\rm los}$, and magnetic
field $\rm{\bf B}$ as a function of the continuum optical-depth $\tau_{\rm c}$.
However, they do not directly provide the gas pressure $P_{\rm g}$ or density
$\rho$. In order to obtain these latter parameters, inversion codes rely
instead on the assumption of hydrostatic equilibrium (HE) in addition to the
equation of state (EOS). Unfortunately, the assumption of HE is rather
unrealistic across magnetic field lines. This is because the role of the
Lorentz force, among other factors, is neglected. This translates into an
inaccurate conversion from optical depth $\tau_{\rm c}$ to geometrical height
$z$. We aim at improving this conversion via the application of
magneto-hydrostatic (MHS) equilibrium instead of HE. We develop a method to
solve the momentum equation under MHS equilibrium (i.e., taking the Lorentz
force into account) in three dimensions. The method is based on the solution of
a Poisson-like equation. Considering the gas pressure $P_{\rm g}$ and density
$\rho$ from three-dimensional magneto-hydrodynamic (MHD) simulations of
sunspots as a benchmark, we compare the results from the application of HE and
MHS equilibrium. We find that HE retrieves the gas pressure and density within
an order of magnitude of the MHD values in only about 47 \% of the domain. This
translates into an error of about $160-200$ km in the determination of the
$z-\tau_{\rm c}$ conversion. On the other hand, the application of MHS
equilibrium allows determination of $P_{\rm g}$ and $\rho$ within an order of
magnitude in 84 \% of the domain. In this latter case, the $z-\tau_{\rm c}$
conversion is obtained with an accuracy of $30-70$ km.

Solar and Stellar Astrophysics