[step:Compute the first variation of $\mathcal F$ for an arbitrary variation of $(g,f)$]Let $s \mapsto (g_s,f_s)$ be a smooth variation of a Riemannian metric and a smooth function on $M$. At a fixed parameter value $s=0$, set
\begin{align*}
g:=g_0, \qquad f:=f_0, \qquad h:=\left.\partial_s g_s\right|_{s=0}, \qquad \phi:=\left.\partial_s f_s\right|_{s=0}.
\end{align*}
Let $d\mu := d\mu_g$, let $S := S_g$, and let all contractions, gradients, Laplacians, Hessians, and divergences be taken with respect to $g$.
[claim:First variation formula for $\mathcal F$]
With the above notation,
\begin{align*}
\left.\frac{d}{ds}\right|_{s=0}\mathcal F[g_s,f_s] = \int_M \left[-\langle h,\operatorname{Ric}_g+\operatorname{Hess}_g f\rangle_g + \left(\frac{1}{2}\operatorname{tr}_g h-\phi\right)\left(2\Delta_g f-|\nabla^g f|_g^2+S\right)\right]e^{-f}\,d\mu.
\end{align*}
[/claim]
[proof]
We use the standard Riemannian volume variation formula, scalar-curvature variation formula, and the elementary metric-gradient variation identity obtained by differentiating $g_s^{-1}(df_s,df_s)$ at $s=0$. The smoothness of $s \mapsto (g_s,f_s)$ verifies the differentiability hypotheses of these variation identities, and closedness of $M$ will verify the boundary-free [integration by parts](/theorems/2098) used below. Explicitly,
\begin{align*}
\left.\partial_s d\mu_{g_s}\right|_{s=0}=\frac{1}{2}\operatorname{tr}_g h\,d\mu.
\end{align*}
Also,
\begin{align*}
\left.\partial_s S_{g_s}\right|_{s=0}=-\langle h,\operatorname{Ric}_g\rangle_g+\operatorname{div}_g(\operatorname{div}_g h)-\Delta_g(\operatorname{tr}_g h).
\end{align*}
Finally,
\begin{align*}
\left.\partial_s|\nabla^{g_s}f_s|_{g_s}^2\right|_{s=0}=-h(\nabla^g f,\nabla^g f)+2\langle\nabla^g f,\nabla^g\phi\rangle_g.
\end{align*}
Differentiating the definition of $\mathcal F$ gives
\begin{align*}
\left.\frac{d}{ds}\right|_{s=0}\mathcal F[g_s,f_s] = \int_M \left[-\langle h,\operatorname{Ric}_g\rangle_g + \operatorname{div}_g(\operatorname{div}_g h) - \Delta_g(\operatorname{tr}_g h) - h(\nabla^g f,\nabla^g f) + 2\langle\nabla^g f,\nabla^g\phi\rangle_g\right]e^{-f}\,d\mu + \int_M (S+|\nabla^g f|_g^2)\left(\frac{1}{2}\operatorname{tr}_g h-\phi\right)e^{-f}\,d\mu.
\end{align*}
Because $M$ is closed, [integration by parts](/theorems/210) against the weight $e^{-f}$, equivalently the [divergence theorem](/theorems/3614), gives
\begin{align*}
\int_M
\left[
\operatorname{div}_g(\operatorname{div}_g h)
-
\Delta_g(\operatorname{tr}_g h)
\right]e^{-f}\,d\mu
&=
\int_M
\left[
h(\nabla^g f,\nabla^g f)
-
\langle h,\operatorname{Hess}_g f\rangle_g
-
(\operatorname{tr}_g h)(|\nabla^g f|_g^2-\Delta_g f)
\right]e^{-f}\,d\mu.
\end{align*}
Also, weighted integration by parts first gives
\begin{align*}
\int_M 2\langle\nabla^g f,\nabla^g\phi\rangle_g e^{-f}\,d\mu = -2\int_M \phi\,\operatorname{div}_g(e^{-f}\nabla^g f)\,d\mu.
\end{align*}
Using $\operatorname{div}_g(e^{-f}\nabla^g f)=e^{-f}(\Delta_g f-|\nabla^g f|_g^2)$, this becomes
\begin{align*}
\int_M 2\langle\nabla^g f,\nabla^g\phi\rangle_g e^{-f}\,d\mu = -2\int_M \phi(\Delta_g f-|\nabla^g f|_g^2)e^{-f}\,d\mu.
\end{align*}
Substituting these two identities cancels the terms $h(\nabla^g f,\nabla^g f)$ and yields
\begin{align*}
\left.\frac{d}{ds}\right|_{s=0}\mathcal F[g_s,f_s] = \int_M \left[-\langle h,\operatorname{Ric}_g+\operatorname{Hess}_g f\rangle_g + \left(\frac{1}{2}\operatorname{tr}_g h-\phi\right)\left(2\Delta_g f-|\nabla^g f|_g^2+S\right)\right]e^{-f}\,d\mu.
\end{align*}
[/proof][/step]