Gluing Schemes, Closed Subschemes Let $X$ be a scheme and $Y$ a closed subset. Take a covering of open subsets $U_i$ of $X$ which are affine. Say $U_i\simeq \text{spec } A_i$, choose $\mathfrak{a}_i$ to be the largest ideal with $V(\mathfrak{a}_i)$ corresponding to $U_i\cap Y$. We have a closed immersion $f_i: \text{spec }(A_i/\mathfrak{a_i})\to U_i$ with $f_i$ providing a homeomorphism with $U_i\cap Y$. 
How does one use this information to construct a scheme, call it $Z$, together with a closed immersion $f:Z\to X$ such that $f$ provides a homeomorphism of $Z$ with $Y$? 
Hartshorne says this involves "gluing", and has two exercises on it, but I looked at the exercises and they do not seem to be suitable to the problem at hand. 
My intuition tells me that topology of $Z$ is the topological glue of $\text{spec }(A_i/\mathfrak{a_i})\to U_i$: take the disjoint union of the spaces $\text{spec }(A_i/\mathfrak{a_i})$; then pass to the quotient space by identifying $a_i\in \text{spec }(A_i/\mathfrak{a_i})$ and $a_j\in \text{spec }(A_j/\mathfrak{a_j})$ whenever $f_i(a_i) = f_j(a_j)$. 
Can somebody outline the steps, for the topology of $Z$, and for the structure sheaf on it? Using only the information provided in this post (i.e. no other results from anywhere else). 
 A: I am too lazy to write out every detail. Here is an incomplete proof. I think it has all the relevant steps in it, now it is up to little details. Maybe somebody can help me with completing the rest of it. Here are some questions that come up: 


*

*List item why is $\mathcal{O}_{Y_i}|_{Y_i\cap Y_j} \simeq \mathcal{O}_{Y_i\cap Y_j}$ 

*Where is the cocylce condition used and how does one justify it? 

*How does one make the scheme morphism a closed embedding? 



Let $X$ be a scheme and $U_i$ an open cover. Thus, $U_i \simeq \text{spec}(A_i)$ for some ring $A_i$. Fix a closed subset $Y$ of $X$, so that $Y_i = Y\cap U_i$ are closed subsets of $U_i$. As $Y_i$ is closed in $U_i$, there exists a largest possible ideal $\mathfrak{a}_i$ such that $V(\mathfrak{a}_i)$ is the closed subset of $\text{spec}(A_i)$ corresponding to $Y_i$. Thus, there exists a morphism of schemes $f_i: Z_i \to U_i$ is a closed immersion of $Z_i$ onto $Y_i$ (here $Z_i = \text{spec}(A/\mathfrak{a}_i)$). We can push the sheaf onto $Y_i$, and denote it $\mathcal{O}_{Y_i}$ instead of $f_{i*}\mathcal{O}_{Z_i}$, and obtain a ringed space $(Y_i,\mathcal{O}_{Y_i})$ isomorphic to $(Z_i,\mathcal{O}_{Z_i})$.\
Construct $W = \coprod_i Z_i$ to be the disjoint union (coproduct in the category of topological spaces) of the $Z_i$. Let $d_i:Z_i\to W$ be the universal maps for the disjoint union. Equip $W$ with the equivalence relation $\sim$ generated by $d_i(z_i) \sim d_j(z_j)$ whenever $f_i(z_i) = f_j(z_j)$ (where, of course, $z_i\in Z_i$ and $z_j\in Z_j$). Construct $Z = W/\sim$ to be the quotient topology, so we have a map $W\to Z$ which satisfies the universal property for the quotient topology. \ 
The maps $Z_i\to W$ are open, and $W\to Z$ is open, thus we have an open map $\lambda_i: Z_i\to Z$. The image $\lambda_i(Z_i)$ is open in $Z$. The maps $\lambda_i: Z_i\to Z$ are homeomorphisms onto its image. \
We can push the sheaf $\mathcal{O}_{Z_i}$ and construct $\lambda_{i*}\mathcal{O}_{Z_i}$ as a sheaf over $\lambda_i(Z_i)$, to ease notation call it $F_i$. We have the isomorphisms $(Y_i,\mathcal{O}_{Y_i})\simeq (Z_i,\mathcal{O}_{Z_i})\simeq (\lambda_i Z_i, F_i)$. Now the idea is that $Z$ is covered by the open sets $\lambda_i(Z_i)$ and on each of these open sets we have a sheaf, so we can under proper circumstances, glue the sheaves together and construct a sheaf $F$ on the whole space $Z$. This sheaf will come equipped with morphisms $\psi_i: F|_{\lambda_i Z_i} \to F_i$. \ 
As will soon be shown, there exist isomorphisms $\varphi_{ij}: F_i|_{\lambda_i Z_i\cap \lambda_j Z_j} \to F_j|_{\lambda_i Z_i\cap \lambda_j Z_j}$, such that $\varphi_{jk} \circ \varphi_{ij}  = \varphi_{ik}$ on the restriction $\lambda_i Z_i\cap \lambda_j Z_j\cap \lambda_k Z_k$. Thus, as we will also afterwards, the morphisms $\psi_i$ will be isomorphisms with the property that $\varphi_{ij} \circ \psi_i  = \psi_j$ on the restriction $\lambda_i Z_i\cap \lambda_j Z_j$.\  
In order to create such isomorphisms $\varphi_{ij}$, it suffices to construct isomorphisms $\mathcal{O}_{Y_i}|_{Y_i\cap Y_j} \simeq \mathcal{O}_{Y_j}|_{Y_i\cap Y_j}$ because $(Y_i,\mathcal{O}_{Y_i}) \simeq (\lambda_i Z_i, F_i)$ and the fact that, 
$$ \lambda_i^{-1} \left( \lambda_i Z_i \cap \lambda_j Z_j\right) = f_i^{-1} \left( Y_i\cap Y_j\right) $$
This is easy to check, because $\lambda_i^{-1} \left( \lambda_i Z_i \cap \lambda_j Z_j\right) = \lambda_i^{-1} \left( \lambda_j Z_j\right)$, and 
$$ x\in \lambda_i^{-1} \left( \lambda_j Z_j\right) \iff \exists y\in Z_j, ~ \lambda_i(x) = \lambda_j(y) \iff f_i(x) = f_j(y) \iff x\in f_i^{-1} (Y_j)  $$
but $f_i^{-1}(Y_j) = f_i^{-1}(Y_i\cap Y_j)$. (The statement $\lambda_i(x) = \lambda_j(y) \iff f_i(x) = f_j(y)$ is precisely the defining property of the quotient space $Z$.) \ 
Next we take a slight detour and discuss a lemma that we need. \ 
Let $U$ be an affine scheme, say $\varphi:\text{spec}(A)\to U$ is an isomorphism for some ring $A$. For a closed subset $Y$ in $U$, $\varphi^{-1}(Y) = V(\mathfrak{a})$, where $\mathfrak{a}$ is the intersection of all prime ideals in $\varphi^{-1}(Y)$. We have a map $g:\text{spec}(A/\mathfrak{a})\to U$ by composition $\text{spec}(A/\mathfrak{a})\to \text{spec}(A) \to U$. This map is a closed embedding onto $Y$. The push-forward sheaf $g_*\mathcal{O}_{A/\mathfrak{a}}$ is denoted by $\mathcal{O}_Y$. 
Fix some $f\in A$. The open subset $\varphi D(f)$ of $U$ is affine, $D(f)\simeq \text{spec}(A_f)$. We have an isomorphism $\varphi':\text{spec}(A_f)\to \varphi D(f)$. The set $Y\cap \varphi D(f)$ is closed in $\varphi D(f)$, $\varphi'^{-1}$ of this set is $= V(\mathfrak{a}')$, where $\mathfrak{a}'=$ the intersection of all prime ideals in the pre-image. As before, we have a closed embedding $h:\text{spec}(A_f/\mathfrak{a}') \to \varphi D(f)$ onto $Y\cap \varphi D(f)$. The push-forward sheaf $h_*\mathcal{O}_{A_f/\mathfrak{a}'}$ is denoted by $\mathcal{O}_{\varphi D(f)\cap Y}$. \ 
\texttt{Lemma.} The following two are sheaves on $Y\cap \varphi D(f)$ and, 
{\LARGE $$ \mathcal{O}_Y|_{Y\cap \varphi D(f)} \simeq \mathcal{O}_{Y\cap \varphi D(f)} $$}
To construct this isomorphism, pick an open set in $Y\cap \varphi D(f)$ of the form $Y\cap \varphi D(f)\cap \varphi D(f')$. Then, 
$$ \mathcal{O}_Y|_{Y\cap \varphi D(f)}( Y\cap \varphi D(f)\cap \varphi D(f') ) = \mathcal{O}_{A/\mathfrak{a}} g^{-1}(Y\cap \varphi D(f)\cap \varphi D(f'))  $$
Which is more simply, 
$$ \mathcal{O}_{A/\mathfrak{a}} ( D(\overline{f})\cap D(\overline{f'})) = \mathcal{O}_{A/\mathfrak{a}}  D(\overline{ff'}) = (A/\mathfrak{a})_{ \overline{ff'} }$$
We also have that, 
$$ \mathcal{O}_{Y\cap \varphi D(f)} ( Y\cap \varphi D(f)\cap \varphi D(f') ) = \mathcal{O}_{A_f/\mathfrak{a}'}h^{-1}(( Y\cap \varphi D(f)\cap \varphi D(f') ))$$
Which is more simply, 
$$  \mathcal{O}_{A_f/\mathfrak{a}'} D(\overline{f'/1}) = (A_f/\mathfrak{a}')_{\overline{f'/1}}$$
Now we use the basic algebraic fact that $\mathfrak{a}' = $ the intersection of all prime ideals (of $A_f$) which are contained in $g^{-1} Y = \mathfrak{a}A_f$. And we have a natural isomorphism, 
$$   (A/\mathfrak{a})_{ \overline{ff'} } \simeq (A_f/\mathfrak{a}A_f)_{\overline{f'/1}} $$
Therefore, we have an isomorphism of the two sheaves $\mathcal{O}_Y|_{Y\cap \varphi D(f)}$ and $\mathcal{O}_{Y\cap \varphi D(f)}$ when we restrict it to the basic open sets within the topological space $Y\cap \varphi D(f)$. For any arbitrary open set in $Y\cap \varphi D(f)$ we use projective limits and extend to an isomorphism to any open set. These isomorphisms will also be natural. \ 
This lemma, applied to our problem of interest, implies that $\mathcal{O}_{Y_i}|_{Y_i\cap Y_j}\simeq \mathcal{O}_{Y_i\cap Y_j}$. Thus, we have isomorphisms $\mu_{ij}: \mathcal{O}_{Y_i}|_{Y_i\cap Y_j} \to \mathcal{O}_{Y_j}|_{Y_i\cap Y_j}$. Furthermore, these isomorphisms are compatible in the sense that $\mu_{jk}\circ \mu_{ij} = \mu_{ik}$ when restricted to $Y_i\cap Y_j\cap Y_k$. Which in turn makes it possible to define isomorphisms $\varphi_{ij}: F_i|_{\lambda_i Z_i\cap \lambda_j Z_j} \to F_j|_{\lambda_i Z_i\cap \lambda_j Z_j}$ with the same compatibility condition. \ 
Thus, there exists a sheaf $F$ on $Z$ together with isomorphisms $\psi_i: F|_{\lambda_i Z_i} \to F_i$, with the property that $\varphi_{ij} \circ \psi_i = \psi_j$ when restricted to $\lambda_i Z_i\cap \lambda_j Z_j$. \
The justification that $(Z,F)$ is a scheme is easy. The sets $\lambda_i Z_i$ form an open cover for $Z$, and $(\lambda_i Z_i, F_i|_{\lambda_i Z_i}) \simeq (\lambda_i Z_i, F_i) \simeq (Z_i,\mathcal{O}_{Z_i}) = (\text{spec}(A_i), \mathcal{O}_{A_i})$. Thus, we see that the $\lambda_i Z_i$ is an affine open cover for $Z$, and so $Z$ is a scheme. \ 
The topological functions $f_i: Z_i \to X$ can be glued together to obtain $f:Z\to X$ such that $f\circ \lambda_i = f_i$ (because this is the categorical property of the quotient space that we constructed for $Z$). This is clearly a homeomorphism because each $f_i$ is, and since the image of each $f_i$ is $Y_i$, the image of $f$ will be $\bigcup_i Y_i = Y$. \   
It only remains to define a scheme morphism $f:Z\to X$ such that $\mathcal{O}_X \to f_*F$ is surjective.
