  In Green's function theory, the total energy of an interacting many-electron
system can be expressed in a variational form using the Klein or Luttinger-Ward
functionals. Green's function theory also naturally addresses the case where
the interacting system is embedded into a bath. This latter can then act as a
dynamical (i.e., frequency-dependent) potential, providing a more general
framework than that of conventional static external potentials. Notably, the
Klein functional includes a term of the form $\text{Tr}_\omega
\text{Ln}\left\{G_0^{-1}G\right\}$, where $\text{Tr}_\omega$ is the frequency
integration of the trace operator. Here, we show that using a sum-over-pole
representation for the Green's functions and the algorithmic-inversion method
one can obtain in full generality an explicit analytical expression for
$\text{Tr}_\omega \text{Ln}\left\{G_0^{-1}G\right\}$. This allows one, e.g., to
derive a variational expression for the Klein functional in the presence of an
embedding bath, or to provide an explicit expression of the RPA correlation
energy in the framework of the optimized effective potential.
