Characterizing pairs of affine schemes $X$ and closed subschemes $Y$ so that $X \setminus Y$ is affine Let $R$ be a ring and let $X = \text{Spec}(A)$ be an affine $R$-scheme. I will from now on not mention the base $R$ anymore and just say affine scheme or algebra instead (it is fine for me to assume that $R = k$ is a field if that helps later on). It is a basic fact that closed subschemes of $X$ correspond to ideals in $A$ and that via this correspondence every closed subscheme of $X$ is of the form $V(I) = \text{Spec}(A/I)$ for some ideal $I \subset A$. In particular, closed subschemes of affine schemes are again affine.
Background/Motivation:
For a project that I am working on it is essential that I am able to use the anti-equivalence between affine schemes and algebras, but also that I am allowed to take complements of closed subschemes. The problem that arises is that complements of affine schemes need not be affine again, as for example $X = \mathbb{A}_k^n$ for $n \geq 2$ shows by taking the complement at the origin. Therefore I would like to have a characterization of pairs $(X,V(I))$, where $X = \text{Spec}(A)$ is an affine scheme and $I \subset A$ is an ideal, so that $D(I) = X \setminus V(I)$ is affine again. This leads to the first question.
Question 1 (maybe too difficult, therefore more below):
Is there a characterization of these pairs? (maybe in terms of the quotient ring $A/I$, or in terms of topological properties of its spectrum, etc.)
My thoughts:
My first idea was to start by seeing what happens for complements associated to closed subschemes defined by finitely generated ideals. For instance, if $I = (f)$ is a principal ideal, it is once again a basic fact that the complement $D(I)$ of $V(I)$ can be identified with $\text{Spec}(A_f)$, where $A_f = A[f^{-1}] \cong A[x]/(xf-1)$ is the localization at the multiplicative set of powers of $f$. This tells us that affine schemes given by spectra of pid's have the wanted property for all closed subschemes or that pairs of affines schemes $X$ and a hypersurface $Y \subset X$ work. This leads to:
Question 2:
What about ideals whose minimal number of generators is two?
Here I am actually stuck already. Therefore I started to change my approach:
Another idea was to see which algebra would correspond to $D(I)$ if it was affine. I have not been able to answer that though. My first idea was to localize at all generators, which also fits the case of principal ideals, but which does not work as Zhen Lin's comment also explains. This leads to my final question:
Question 3:
If $D(I)$ is affine, what is corresponding algebra? (here I edited the question since I remembered an answer to the previous one due to Zhen Lin's comment)
I am happy for both references and answers or helpful comments to either of the questions or regarding related things.
 A: First, we need to recognise the $A$-algebras that arise as the affine open subschemes of $\operatorname{Spec} A$. Here is a criterion:
Proposition 1. An $A$-algebra $B$ corresponds to an open subscheme of $\operatorname{Spec} A$ if and only if there is a (finite) set $S \subseteq A$ with the following properties:

*

*For every $a \in S$, the induced homomorphism $A [a^{-1}] \to B [a^{-1}]$ is an isomorphism.


*The image of $S$ generates the unit ideal of $B$.
(This is easy to understand in the geometric picture: all it is saying is that there are principal open subschemes of $\operatorname{Spec} A$ that pull back isomorphically to $\operatorname{Spec} B$ and cover it.)
Let $I$ be an ideal of $A$. There is only one possibility for $B$ if $D (I) \cong \operatorname{Spec} B$, namely $B \cong \varprojlim_{a \in I} A [a^{-1}]$. Here, we order $I$ by divisibility and recall that if $a$ divides $b$ then there is a unique $A$-algebra homomorphism $A [a^{-1}] \to A [b^{-1}]$. Note that we take the inverse limit here, not the direct limit, so $B$ is not $A [I^{-1}]$ (which, in any case, is $\{ 0 \}$) – this comes from the construction of the structure sheaf of $\operatorname{Spec} A$. Therefore:
Proposition 2. $D (I)$ is an affine scheme if and only if $\varprojlim_{a \in I} A [a^{-1}]$ satisfies the conditions of proposition 1.
The next step is to get a grip on $\varprojlim_{a \in I} A [a^{-1}]$. Suppose $T \subseteq I$ satisfies the following conditions:

*

*For each $a \in I$, there is $b \in T$ such that $b$ divides $a$.

*For each $a \in I$, $b \in T$, $c \in T$, if both $b$ and $c$ divide $a$, then there is $d \in T$ such that both $b$ and $c$ divide $d$ and $d$ divides $a$.

Then the canonical comparison $\varprojlim_{a \in T} A [a^{-1}] \to \varprojlim_{a \in I} A [a^{-1}]$ is an isomorphism. For instance, we might take $T$ to be a generating set of $I$, together with all their finite products. In particular, if $I$ is a finitely generated ideal, we can have $T$ be a finite set. This is extremely helpful because localisation preserves finite limits. Thus:
Proposition 3. Assuming $I$ is finitely generated, $D (I)$ is affine if and only if $I$ generates the unit ideal of $B$.
(If $I$ is finitely generated and $a \in I$, then $A [a^{-1}] \to B [a^{-1}]$ is an isomorphism; so only the second condition of proposition 1 has to be checked for the "if" direction. The "only if" direction is true without assuming $I$ is finitely generated.)
For example, if $A = k [x, y, u, v] / (x y + u x^2 + v y^2)$ and $I = (x, y)$, then we have the following elements of $B$:
$$\begin{aligned}
f & = - \frac{v}{x} = \frac{y + u x}{y^2} \\
g & = - \frac{u}{y} = \frac{x + v y}{x^2}
\end{aligned}$$
But $y f + x g = 1$ in $B$, thus by proposition 3, $D (I)$ is affine.
