  We calculate the second-order corrections to the atomic energy level shifts
in ordinary and muonic deuterium due to virtual excitations of the deuteron
which are important for ongoing and planned precise experiments in these
systems. For light atoms a method can be used in which the shift is expressed
as integrals over the longitudinal and transverse inelastic structure functions
of the nucleus. We employ the structure functions arising from separable NN
potentials of the Yamaguchi and Tabakin form which can be evaluated
analytically. Special emphasis is put on gauge invariance which requires a
consistent inclusion of interaction currents and seagull terms. The effect of
the D-wave component of the deuteron is investigated for the leading
longitudinal contribution. We also estimate the shift for pionic deuterium.
