Strike through text without affecting text box size

Without ulem or soul:

\documentclass[12pt]{beamer}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\makeatletter
\newcommand{\tst}[1]{%
  \ifmeasuring@
    % we're in the measuring stage, just use the argument
    #1
  \else
    % we're typesetting, add the strikeout rule
    \sbox0{$\displaystyle#1$}
    \rlap{\color{red}%
          \vrule height \dimexpr.5ex+0.4pt\relax
                 depth -\dimexpr.5ex-0.4pt\relax
                 width \wd0 }
    \box0
  \fi
}
\makeatother

\begin{document}

\begin{frame}
\begin{align*}
P = \{ & \tst{S\rightarrow X_{1,4,2}}\\
       & X_{1,4,1}\rightarrow X_{1,1,1}X_{2,3,1},\\
       & X_{1,4,1}\rightarrow X_{1,1,1}X_{2,3,1}\}
\end{align*}
\begin{align*}
P = \{ & S\rightarrow X_{1,4,2}\\
       & X_{1,4,1}\rightarrow X_{1,1,1}X_{2,3,1},\\
       & X_{1,4,1}\rightarrow X_{1,1,1}X_{2,3,1}\}
\end{align*}
\end{frame}

\end{document}

enter image description here


I used soul to do the strikethrough, but had to define a displayst command to do strikethrough in display math mode. (soul commands only work with math as in \st{$abc$} . . . ) I also used the solution from Why is it that coloring in soul in beamer is not visible to make the color work. Use \setul{⟨underline depth⟩}{⟨underline thickness⟩} to change thickness of underline/strikethrough in soul.–

\documentclass[12pt]{beamer}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{color,soul}
\makeatletter
\newcommand\SoulColor{%
  \let\set@color\beamerorig@set@color
  \let\reset@color\beamerorig@reset@color}
\makeatother
\setstcolor{red}
%\setul{}{1pt} Use this to change weight of underline/strikethrough

\newcommand{\displayst}[1]{\textrm{\SoulColor\st{$\displaystyle#1$}}}

\begin{document}

\begin{frame}
\begin{align*}
P = \{ & \displayst{S\rightarrow X_{1,4,2}}\\
       & X_{1,4,1}\rightarrow X_{1,1,1}X_{2,3,1},\\
       & X_{1,4,1}\rightarrow X_{1,1,1}X_{2,3,1}\}
\end{align*}
\end{frame}

\end{document}

Result:

enter image description here

Edit

I guess making this work nicely with soul required fixing a few things. The original solution I posted strikes behind the text, instead of on top of it, but this was solved in Strikeout in different color appears behind letters, not on top of them

\documentclass[12pt]{beamer}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xparse,xcolor,soul}

%% First define strikethrough command that strikes on top of letters, not behind them 
\makeatletter
\NewDocumentCommand{\sttwo}{O{red}O{black}+m}
    {%
        \begingroup
        \setulcolor{#1}%
        \setul{-.5ex}{1pt}% <---- sets the undline/strikeout weight to 1pt
        \def\SOUL@uleverysyllable{%
            \rlap{%
                \color{#2}\the\SOUL@syllable
                \SOUL@setkern\SOUL@charkern}%
            \SOUL@ulunderline{%
                \phantom{\the\SOUL@syllable}}%
        }%
        \ul{#3}%
        \endgroup
    }
\makeatother

% Now make soul colors work with beamer
\makeatletter
\newcommand\SoulColor{%
  \let\set@color\beamerorig@set@color
  \let\reset@color\beamerorig@reset@color}
\makeatother

%Now make a version of strikethrough that works with display math
\newcommand{\displayst}[1]{\textrm{\SoulColor\sttwo{$\displaystyle#1$}}}


\begin{document}

\begin{frame}
\begin{align*}
P = \{ & \displayst{S\rightarrow X_{1,4,2}}\\
       & X_{1,4,1}\rightarrow X_{1,1,1}X_{2,3,1},\\
       & X_{1,4,1}\rightarrow X_{1,1,1}X_{2,3,1}\}
\end{align*}
\end{frame}

\end{document}

Result:

enter image description here