1

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!

1 Answers1

1

With this post Correct usage of \ifmmode, it requires a \relax before \ifmmode

\renewcommand{\replaced}[2]{\relax\ifmmode{\origreplaced{\ensuremath{#1}}

With this other post Wrong to delete/replace equation by using changes package, we can steer in math mode

The code

\documentclass{article}
\usepackage{mathtools}
\usepackage[colorlinks]{hyperref}

\def\mode{draft} %\def\mode{final}

\usepackage[\mode]{changes} %https://tex.stackexchange.com/questions/297111/wrong-to-delete-replace-equation-by-using-changes-package/352748#352748 % My attempt \newcommand{\stkout}[1]{\ifmmode\text{\sout{\ensuremath{#1}}}\else\sout{#1}\fi} \setdeletedmarkup{\stkout{#1}}

% https://tex.stackexchange.com/questions/82653/correct-usage-of-ifmmode

\ifthenelse{\equal{\mode}{draft}}{ \let\origreplaced\replaced \renewcommand{\replaced}[2]{\relax\ifmmode{\origreplaced{\ensuremath{#1}}{\ensuremath{#2}}}\else{\origreplaced{#1}{#2}}\fi} }{} \begin{document} Deleting \deleted{some undesirable} text

\[ K = \alpha^2\replaced{\phi_{\text{in}}}{\phi_{\text{out}}}\]

\[
\begin{aligned}
    \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}

enter image description here

pascal974
  • 4,652
  • Nice! I had already stumbled upon both the questions linked before asking the question myself, and I was also using the hack to get strikeout working in both math mode and text mode, but was not TeX-savvy ;) enough to understand how \relax, \protect etc. work and implement this myself. Accepting the answer. Thanks! – Krishnadev N Jun 28 '23 at 14:16