我正在使用changes
包来突出显示文档中的更改。但是数学模式中的更改在列出更改时会引发错误,因此我尝试将它们包装在中\ensuremath{}
。我认为重新定义宏来自动化此操作会很方便。问题是它并不总是在draft
和final
模式下都有效(请参阅下面的 MWE)。
\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}
有没有办法重新定义宏,这样我就不需要\ensuremath{}
每次都使用了?我对 LaTeX 不是很熟练,但我尝试过\protect
,\expandafter
等等,但无法让它工作。当然,没有的解决方案\ensuremath{}
绝对受欢迎!
答案1
通过这篇文章 \ifmmode 的正确用法,它需要一个\relax
之前\ifmmode
\renewcommand{\replaced}[2]{\relax\ifmmode{\origreplaced{\ensuremath{#1}}
与此其他帖子使用变更包删除/替换方程式是错误的,我们可以在数学模式下驾驶
代码
\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}