  We determine the equation of state of 2+1-flavor QCD with physical quark
masses, in the presence of a constant (electro)magnetic background field on the
lattice. To determine the free energy at nonzero magnetic fields we develop a
new method, which is based on an integral over the quark masses up to
asymptotically large values where the effect of the magnetic field can be
neglected. The method is compared to other approaches in the literature and
found to be advantageous for the determination of the equation of state up to
large magnetic fields. Thermodynamic observables including the longitudinal and
transverse pressure, magnetization, energy density, entropy density and
interaction measure are presented for a wide range of temperatures and magnetic
fields, and provided in ancillary files. The behavior of these observables
confirms our previous result that the transition temperature is reduced by the
magnetic field. We calculate the magnetic susceptibility and permeability,
verifying that the thermal QCD medium is paramagnetic around and above the
transition temperature, while we also find evidence for weak diamagnetism at
low temperatures.
