Suppose $\pi:\mathcal{X}\rightarrow S$ is a smooth family of complex affine varieties (to make things simpler, we can actually assume it is locally trivial). Let $P$ be a smooth projective variety, and assume there is an embedding $\mathcal{X}\hookrightarrow P\times S$ over $S$. Let $\overline{\pi}: \overline{\mathcal{X}}\rightarrow S$ be the projection for the closure of $\mathcal{X}$ in $P\times S$. Is it always true that for every (closed) point $s\in S$, $\overline{\pi^{-1}(s)}=\overline{\pi}^{-1}(s)$? Any references will be greatly appreciated.



When is the fiberwise compactification (not) equal to the compactification of the family?
No.
Let $S = \mathbb{A}^1\_{\mathbb{C}}$ and $\mathcal{X} = \mathbb{A}^1\_S \rightarrow S$ be the constant family. Let $\mathcal{Y}$ be the blow-up of the surface $\mathbb{P}^1\_S$ at the closed point over $0\in S$ lying at infinity. In other words: $\mathbb{P}^1\_S = \mathbb{P}^1\times \mathbb{A}^1$ and $\mathcal{Y}$ is the blow-up at the point $(\infty,0)$.
Since blowing-up is an isomorphism away from the exceptional locus, there is an open immersion $\mathcal{X} \hookrightarrow \mathcal{Y}$ of $S$-schemes. Since $\mathcal{Y}$ is irreducible, $\mathcal{X}$ is dense in $\mathcal{Y}$.


Since the morphisms $\mathcal{Y} \rightarrow \mathbb{P}^1\_S$ and $\mathbb{P}^1\_S \rightarrow S$ are projective, the composite $\mathcal{Y} \rightarrow S$ is projective as well. Choose $n \geq 0$ large enough such that there exists a closed embedding $\mathcal{Y} \hookrightarrow P\_S$, where $P = \mathbb{P}^n\_{\mathbb{C}}$. The closure $\bar{\mathcal{X}}$ of $\mathcal{X}$ in $P\_S$ is exactly $\mathcal{Y}$.


The fibre of $\mathcal{Y} \rightarrow S$ above $0$ is a union of two projective lines. However, the fibre of $\mathcal{X} \rightarrow S$ above $0$ is just $\mathbb{A}^1$. Therefore $\mathcal{X}\_0$ is not dense in $(\bar{\mathcal{X}})\_0$, as required.


