TITLE: closure of inverse image is subset of inverse image of closure, given that $f$ is continuous
QUESTION [6 upvotes]: Let $f : \mathbb{R} \rightarrow \mathbb{R}$ be a continuous function. Prove then that $$ \overline{f^{-1}(X)} \subset f^{-1} (\overline{X}) $$  for every $X \subset \mathbb{R}$.
Attempt at proof: Let $a \in \overline{f^{-1}(X)}$ be arbitrary. Then by definition we have $\forall \delta > 0$ that $$ ] a - \delta, a + \delta [ \cap f^{-1}(X) \neq \emptyset. $$ Let $x$ be an element in this intersection. Thus $x \in ]a - \delta, a + \delta [ $ and $x \in f^{-1}(X)$. It follows that $f(x) \in X$. Because $f: \mathbb{R} \rightarrow \mathbb{R}$ is continuous $a$, we can find $\forall \epsilon > 0$ a $\delta > 0$ such that $\forall x \in \mathbb{R}$  it holds that $$ | f(x) - f(a) | < \epsilon $$ if $| x - a | < \delta$. Now we have $$f^{-1} (\overline{X})  = \left\{a \in \overline{X} \mid f(a) \in f(\overline{X}) \right\}. $$ This means I have to show that $a \in \overline{X}$ and then show that $f(a) \in f(\overline{X})$. This is the part where I'm stuck.
Help would be appreciated.

REPLY [2 votes]: I finish your idea:
Let $a \in \overline{f^{-1}(X)}$ be arbitrary. Then by definition we have $\forall \delta > 0$ that $$ ] a - \delta, a + \delta [ \cap f^{-1}(X) \neq \emptyset. $$ Let $\delta_n=\frac{1}{n}$ and $x_n$ be an element in the intersection $]a-\delta_n,a+\delta_n[\cap f^{-1}(X)$. Thus $x_n \in ]a - \delta_n, a + \delta_n [ $ and $x_n \in f^{-1}(X)$. It follows that $f(x_n) \in X\subset \overline X$. Because $f: \mathbb{R} \rightarrow \mathbb{R}$ is continuous at $a$ and $x_n\to a\Rightarrow f(x_n)\to f(a)$. But as $\{f(x_n)\}\subset \overline X$ then its limit is also in $\overline X$, i.e $f(a)\in\overline X$ which means that $a\in f^{-1}(\overline X)$.