If $f \in \mathcal{C}[a,b]$ and $|f| \in \mathrm{BV}[a,b]$, then how may we show $f \in \mathrm{BV}[a,b]$? Problem Statement: Given $f \in \mathcal{C}[a,b]$ and $|f| \in \mathrm{BV}[a,b]$, then show $f \in \mathrm{BV}[a,b]$.
(Here, $f : [a,b] \to \mathbb{R}$, i.e. we consider $f$ real-valued.)

Relevant Definitions:

*

*Given an interval $[a,b]$, we consider an ordered partition $P := \{x_i\}_{i=0}^{n_P}$ of it to be a finite collection of points such that
$$
a = x_0 < x_1 < \cdots < x_{n_P} = b
$$


*The class of all such partitions of $[a,b]$ is denoted $\mathcal{P}_{a,b}$.


*For $\newcommand{\nc}{\newcommand}
\nc{\R}{\mathbb{R}}
\nc{\BV}{\mathrm{BV}}
\nc{\CC}{\mathcal{C}}
\nc{\PP}{\mathcal{P}}
\nc{\abs}[1]{\left|#1\right|}
\nc{\ve}{\varepsilon}
f : [a,b] \to \R$, we define its variation by
$$
V_a^b(f) := \sup_{P \in \PP_{a,b}} \sum_i \abs{ f(x_i) - f(x_{i-1}) }
$$
We may associate a variation to a given partition by
$$
V(f,P) := \sum_i \abs{ f(x_i) - f(x_{i-1}) }
$$
If $V_a^b(f) < \infty$, then $f$ is of bounded variation and we say $f \in \BV[a,b]$.


*Continuity is defined in the usual way: $f$ is continuous at $\xi$ if
$$
(\forall \ve >0)(\exists \delta > 0)\Big( 0 < \abs{x-\xi} < \delta \implies \abs{ f(x) - f(\xi) } < \ve \Big)
$$
If $f$ is continuous at all points in $[a,b]$, then $f \in \CC[a,b]$.

Thoughts:
For what it's worth, pretty much all of the "basic" and "standard" results (e.g. Jordan's decomposition theorem) are fair game, but I feel this can be justified from first principles. That said, one potentially useful one is that, for $f$ continuous,
$$
V_a^b(f) = \lim_{\|P\| \to 0} \sum_i \abs{ f(x_i) - f(x_{i-1}) }
$$
It's easy to derive some ideas from continuity or bounded variation individually, but interleaving the two is proving difficult.

*

*For instance, one can see that, given $\ve > 0$ and a partition $P := \{x_i\}_{i=0}^{n_P}$, then $f$ is continuous at each $x_i$, with an associated $\delta_i$ for each. In particular, if $\|P\| < \min \delta_i$, then
$$V(f,P) = \sum_i \abs{ f(x_i) - f(x_{i-1}) } < \ve \cdot n_P$$
but in the supremum this isn't a tight enough bound.


*Of course, since $[a,b]$ is compact, its supremum is achieved by continuity (say it's $M$); then
$$V(f,P) = \sum_i \abs{f(x_i) - f(x_{i-1})} \le \sum_i \abs{f(x_i)} + \abs{f(x_{i-1})} \le 2M n_P$$
which is likewise not good enough.


*When one looks at the variation for $\abs{f}$, we see that it begs the use of the reverse triangle inequality:
$$V(\abs{f},P) = \sum_i \Big| \abs{f(x_i)} - \abs{f(x_{i-1})} \Big|
\le \sum_i \abs{ f(x_i) - f(x_{i-1}) } = V(f,P)$$
Of course, this actually proves unhelpful in the end.
Obviously, continuity alone isn't enough (the usual example being $\sin(1/x)$ on $(0,1]$ extended to $[0,1]$ with any value at $x=0$), nor is $\abs{f} \in \BV[a,b]$ (think of $f = \chi_{\mathbb{Q}} - \chi_{\mathbb{R}\setminus \mathbb{Q}}$), but the question I'm struggling with is how to apply both of these conditions at once, especially with the latter condition seems so unhelpful at first glance.
Any hints or nudges in the right direction?
 A: Let $f : [a, b] \to \mathbb{R}$ be a continuous function. We show that
$$ V(f) = V(|f|). $$
To this end, it suffices to show the next result.

Lemma. Let $P$ be any partition of $[a, b]$. Then there exists a refinement $Q$ of $P$ such that
$$ V(f, Q) = V(|f|, Q). $$

Once this is established, we can complete the proof via a standard argument: Choose a sequence of partition $(P_n)$ such that $V(f, P_n) \nearrow V(f)$. Also, for each $P_n$ we apply the above lemma to find a refinement $Q_n$ such that $V(f, Q_n) = V(|f|, Q_n)$. Then
$$ V(f, P_n) \leq V(f, Q_n) = V(|f|, Q_n) \leq V(|f|). $$
Now letting $n\to\infty$ shows that $V(f) \leq V(|f|)$. The inequality $V(|f|) \leq V(f)$ can be proved in an analogue way, hence the equality follows.

Proof of Lemma. Write $P=\{x_i\}_{i=0}^{n_P}$, and construct a refinement $Q$ of $P$ via the following algorithm:
\begin{align*}
& \texttt{let $Q = P$} \\
& \texttt{for $i$ in $\{ 1,\ldots,n_P \}$}\\
& \qquad \texttt{if $f(x_{i-1})f(x_i) \leq 0$ then}\\
& \qquad\qquad \texttt{use IVT to find $\xi \in [x_{i-1}, x_i]$ such that $f(\xi) = 0$} \\
& \qquad\qquad \texttt{and then add $\xi$ to $Q$}\\
& \qquad \texttt{end}\\
& \texttt{end}\\
& \texttt{yield $Q$}
\end{align*}
Here, IVT stands for the intermediate value theorem. Now write $Q = \{y_i\}_{i=1}^{n_Q}$ and note that we can find $ 0 = i_0 < i_1 < \ldots < i_m = n_Q $ such that
$$ \begin{cases}
f(y_i) \geq 0 & \text{for all $i \in \{i_{k-1}, \ldots, i_k \}$, or } \\[0.25em]
f(y_i) \leq 0 & \text{for all $i \in \{i_{k-1}, \ldots, i_k \}$}
\end{cases} $$
for each $k = 1, \ldots, m$. Consequently,
\begin{align*}
V(|f|, Q)
&= \sum_{k=1}^{m} \Biggl( \sum_{i = i_{k-1} + 1}^{i_k} \bigl| |f(y_{i})| - |f(y_{i-1})| \bigr| \Biggr) \\
&= \sum_{k=1}^{m} \Biggl( \sum_{i = i_{k-1} + 1}^{i_k} |f(y_{i}) - f(y_{i-1})| \Biggr) \\
&= V(f, Q).
\end{align*}
