I am using the changes package to highlight changes in my document. But changes inside math mode throw errors when listing the changes, so I tried wrapping them in \ensuremath{}. I thought it would be convenient to redefine the macros to automate this. The issue is that it does not always work in both draft and final modes (see MWE below).
\documentclass{article}
\usepackage{mathtools}
\usepackage[colorlinks]{hyperref}
\def\mode{draft}
%\def\mode{final}
\usepackage[\mode]{changes}
% My attempt
\ifthenelse{\equal{\mode}{draft}}{
\let\origreplaced\replaced
\renewcommand{\replaced}[2]{\ifmmode{\origreplaced{\ensuremath{#1}}{\ensuremath{#2}}}
\else{\origreplaced{#1}{#2}}\fi}
}{}
\begin{document}
Deleting \deleted{some undesirable} text
% Worked originally - inspiration for redefinition
% \[ K = \alpha^2\replaced{\ensuremath{\phi_\text{in}}}{\ensuremath{\phi_\text{out}}} \]
% Works
\[ K = \alpha^2\replaced{\phi_{\text{in}}}{\phi_{\text{out}}} \]
\[
\begin{aligned}
% Does not work
\replaced{\phi_{\text{in}}}{\phi_{\text{out}}}
&= \frac{1}{A} \int T(\vec{x})\,\mathrm{d}\vec{x} \\
&= \frac{1}{\pi R_2^2} \int_{R_1}^{R_2}T(r) 2\pi r\,\mathrm{d}r
\end{aligned}
\]
\listofchanges
\end{document}
Is there a way to redefine the macros so that I do not need to use \ensuremath{} each time? I am not very proficient in LaTeX, but I tried playing with \protect, \expandafter etc., but could not manage to make it work. Of course, solutions without \ensuremath{} are definitely welcome!

\relax,\protectetc. work and implement this myself. Accepting the answer. Thanks! – Krishnadev N Jun 28 '23 at 14:16