The MWE below
- makes the
!active in math mode using the solution from Make Characters Active via Macro in Math Mode and - uses the
tikz\hcancelvia Diagonal strikeout starting too low and ending too high.
However, when the ! is active within the \hcancel compilation seems to hang?
The desired output is:
but if I uncomment the code intend to produce the the last canceled term the compilation seems to hang.
Code:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor,etoolbox}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
%% https://tex.stackexchange.com/questions/299798/make-characters-active-via-macro-in-math-mode
\newcommand{\DeclareMathActive}[2]{%
% #1 is the character, #2 is the definition
\expandafter\edef\csname keep@#1@code\endcsname{\mathchar\the\mathcode#1 } \begingroup\lccode~=#1\relax \lowercase{\endgroup\def~}{#2}% \AtBeginDocument{\mathcode#1="8000 }%
}
\newcommand{\std}[1]{\csname keep@#1@code\endcsname}
\patchcmd{\newmcodes@}{\mathcode\-\relax}{\std@minuscode\relax}{}{\ddt} \AtBeginDocument{\edef\std@minuscode{\the\mathcode-}}
\makeatother
\DeclareMathActive{!}{\mathclose{\textcolor{blue}{\std{!}}}}
%% https://tex.stackexchange.com/questions/20643/diagonal-strikeout-starting-too-low-and-ending-too-high
\newcommand{\hcancel}[5]{%
\tikz[baseline=(tocancel.base)]{
\node[inner sep=0pt,outer sep=0pt] (tocancel) {#1};
\draw[red] ($(tocancel.south west)+(#2,#3)$) -- ($(tocancel.north east)+(#4,#5)$);
}%
}%
\begin{document}
Active ! works: $3!$
Cancel with non-active ! works:
\hcancel{$3\std{!}$}{-3pt}{0pt}{3pt}{0pt}
\medskip
Combine cancel with active ! hangs:
%\hcancel{$3!$}{-3pt}{0pt}{3pt}{0pt}% <-- This hangs!!
\end{document}


babel, TikZ changes the meaning of the active!to\tikz@nonactiveexlmark, which expands to a!with category code 12, which then expands to\tikz@nonactiveexlmark, which… – egreg May 12 '16 at 21:30