How to make text appear in the middle of a phantom in math mode?
You don't really need to use \mathclap
, you can just place what you want to center in a box of the appropriate width.
\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}
\newcommand*{\mask}[2]{%
\mathord{\makebox[\widthof{\(#1\)}]{\(#2\)}}%
}
\begin{document}
\begin{gather*}
1 + \mask{ABC}{B} + 2 \\
1 + ABC + 2
\end{gather*}
\end{document}
A version that works in all styles.
\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand{\mask}[2]{{\mathpalette\mask@{{#1}{#2}}}}
\newcommand{\mask@}[2]{\mask@@{#1}#2}
\newcommand{\mask@@}[3]{%
\settowidth{\dimen@}{$\m@th#1#2$}%
\makebox[\dimen@]{$\m@th#1#3$}%
}
\makeatother
\begin{document}
\begin{gather*}
1 + \mask{ABC}{B} + 2 \\
1 + ABC + 2 \\
1 + \mask{\int_0^1 f(x)\,dx}{y}+2 \\
1 + \int_0^1 f(x)\,dx+2
\end{gather*}
\[
\begin{array}{c}
1 + \mask{\int_0^1 f(x)\,dx}{y}+2 \\
1 + \int_0^1 f(x)\,dx+2
\end{array}
\]
\[
\sum_{\substack{1\le \mask{w}{i}\le n \\ 1\le w\le n}} a_{iw}
\]
\end{document}