[step:Prove the formula when the potential is bounded above]Let
\begin{align*}
f:E\to\mathbb R
\end{align*}
be an $\mathcal E$-measurable function bounded above. Define $f^+:E\to[0,\infty)$ by $f^+(x)=\max\{f(x),0\}$ and $f^-:E\to[0,\infty)$ by $f^-(x)=\max\{-f(x),0\}$. Assume
\begin{align*}
0<Z_f:=\int_E e^f\,d\mu<\infty.
\end{align*}
Define the tilted probability measure $\mu_f$ by
\begin{align*}
\frac{d\mu_f}{d\mu}=\frac{e^f}{Z_f}.
\end{align*}
This is a probability measure because the density
\begin{align*}
E\to[0,\infty),\qquad x\mapsto \frac{e^{f(x)}}{Z_f}
\end{align*}
is non-negative and integrates to $1$ with respect to $\mu$.
Let $\nu\in\mathcal P(E)$. If $\nu\not\ll\mu$, then $D(\nu\|\mu)=+\infty$, and the variational expression is $-\infty$. Suppose $\nu\ll\mu$, and let
\begin{align*}
h:E\to[0,\infty]
\end{align*}
be a [Radon-Nikodym density](/page/Absolutely%20Continuous%20Measures) of $\nu$ with respect to $\mu$. If $D(\nu\|\mu)=+\infty$, then the variational expression is $-\infty$ under the same extended-value convention, so the upper bound is immediate. Hence assume $D(\nu\|\mu)<\infty$. Since the density
\begin{align*}
E\to(0,\infty),\qquad x\mapsto \frac{e^{f(x)}}{Z_f}
\end{align*}
is strictly positive, $\nu\ll\mu_f$, and the [Radon-Nikodym density](/page/Absolutely%20Continuous%20Measures) of $\nu$ with respect to $\mu_f$ is
\begin{align*}
\frac{d\nu}{d\mu_f}=\frac{h}{e^f/Z_f}.
\end{align*}
If
\begin{align*}
\int_E f^-\,d\nu=\infty,
\end{align*}
then
\begin{align*}
\int_E f\,d\nu=-\infty,
\end{align*}
so the desired upper bound is immediate. Otherwise all terms below are well-defined in $(-\infty,\infty]$. Since $D(\nu\|\mu)<\infty$, the positive part of $h\log h$ is $\mu$-integrable, and the negative part of $h\log h$ is integrable with respect to $\mu$ because $a\log a\geq -e^{-1}$ for $a\geq 0$ and $\mu(E)=1$. Since $f$ is bounded above, $f^+$ is bounded, so $\int_E f^+\,d\nu<\infty$; by the present case assumption, $\int_E f^-\,d\nu<\infty$. Hence $\int_E fh\,d\mu=\int_E f\,d\nu$ is finite, and the relative entropy expansion below is not an undefined extended-real subtraction. Direct expansion gives
\begin{align*}
D(\nu\|\mu_f)=\int_E h\log\left(\frac{h}{e^f/Z_f}\right)\,d\mu.
\end{align*}
Therefore
\begin{align*}
D(\nu\|\mu_f)=\int_E h\log h\,d\mu-\int_E fh\,d\mu+\log Z_f\int_E h\,d\mu.
\end{align*}
Since $\int_E h\,d\mu=1$, this rearranges to
\begin{align*}
\int_E f\,d\nu-D(\nu\|\mu)=\log Z_f-D(\nu\|\mu_f).
\end{align*}
The [entropy inequality](/theorems/6729) from the previous step gives $D(\nu\|\mu_f)\geq 0$, hence
\begin{align*}
\int_E f\,d\nu-D(\nu\|\mu)\leq \log Z_f.
\end{align*}
It remains to check that equality is achieved at $\mu_f$. Because $f$ is bounded above, say $f\leq M$, the negative part of $f e^f$ is integrable with respect to $\mu$: on $\{f<0\}$, the function $-f e^f$ is bounded above by $e^{-1}$, and on $\{f\geq 0\}$ there is no negative part. Hence $\int_E f\,d\mu_f$ is finite from below. Also
\begin{align*}
D(\mu_f\|\mu)=\int_E \frac{e^f}{Z_f}\left(f-\log Z_f\right)\,d\mu
\end{align*}
is finite-valued. Substituting $\nu=\mu_f$ in the identity above gives
\begin{align*}
\int_E f\,d\mu_f-D(\mu_f\|\mu)=\log Z_f.
\end{align*}
Thus
\begin{align*}
\log\int_E e^f\,d\mu=\sup_{\nu\in\mathcal P(E)}\left\{\int_E f\,d\nu-D(\nu\|\mu)\right\}
\end{align*}
whenever $f$ is measurable, bounded above, and has $0<\int_E e^f\,d\mu<\infty$.[/step]