[Tex/LaTex] cancel package: combine bcancel and cancelto (bcancelto?)

cancel

Is there a way to create a negative slope with the cancel package, but using \cancelto? i.e. I would like to combine the two commands so that I have a negative slope but with an arrow and number.

Thanks!

MWE:

\documentclass{journal}
\usepackage{cancel}

\begin{document}
\[
\cancelto{0}{X^2}
\]
\[
\bcancel{X^2}
\]
%$\bcancelto{0}{X^2}$
\end{document}

enter image description here

Best Answer

I assume this is what you are after:

cancelto lower right example

This was achieved by first patching the \canto@vector command to change modify the script placement based on the sign of the y component of the vector (given by #4). Then, \@cancelto was renewed with an optional parameter. If blank, the standard behavior would be achieved. If a - is passed, then the vector is pointed downward. Finally, the \bcancelto command was created, assuming the default smaller option is used (Smaller or samesize would require an alternative definition to modify the script size used).

Your modified MWE:

\documentclass{article}
\usepackage{cancel}

\usepackage{etoolbox}
\makeatletter

    %patch \canto@vector to correctly place script.
    \pretocmd{\canto@vector}{\edef\testneg{#4<0}}{}{\@latex@error{Patch fail}}
    \patchcmd{\canto@vector}{^}{\ifnum\testneg_\else^\fi}{}{\@latex@error{Patch fail}}
    \patchcmd{\canto@vector}{\raise#2\dimen@}{\raise\dimexpr\ifnum\testneg-\ht\z@+\fi#2\dimen@}{}{\@latex@error{Patch fail}}

    \renewcommand\@cancelto[5][]{%
        \OriginalPictureCmds\@begin@tempboxa\hbox{\m@th$#4{#5}$}%
        \dimen@\width % wide
        \@min@pt\dimen@ 2\@min@pt\totalheight4
        \ifdim\totalheight<\dimen@
         \@tempcnta\totalheight \multiply\@tempcnta 5 \divide\@tempcnta\dimen@
         \@tempdimb 3\p@ % extra width for arrowhead ("+2")
         \advance\dimen@ \ifcase\@tempcnta 5\or 5\or 4\or 3\else 2\fi \p@
         \@min@pt\dimen@9\advance\dimen@\p@ 
         \edef\@tempa{\ifcase\@tempcnta 544{#1 1}\or 544{#1 1}\or 542{#1 1}\or 444{#1 3}\else 361{#1 1}\fi
              {\strip@pt\dimen@}{\strip@pt\@tempdimb}}%
         \def\@tempb{Cancel #4 to #2; case wide }%
        \else % tall
         \advance\totalheight3\p@ % "+2"
         \@tempcnta\dimen@ \multiply\@tempcnta 5 \divide\@tempcnta\totalheight
         \advance\totalheight3\p@ % "+2"
         \dimen@ \ifcase\@tempcnta .25\or .25\or .5\or .75\else 1\fi \totalheight
         \@tempdimb \ifcase\@tempcnta .8\or .8\or 1.2\or 1.5\else 2\fi \p@
         \edef\@tempa{\ifcase\@tempcnta 081{#1 4}\or 081{#1 4}\or 181{#1 2}\or 273{#1 4}\else 361{#1 1}\fi
              {\strip@pt\dimen@}{\strip@pt\@tempdimb}}%
        \fi
        \dimen@\height
        \setbox\@tempboxa\hbox{$\m@th\vcenter{\box\@tempboxa}$}%
        \advance\dimen@-\height % the difference in height
        \unitlength\p@ \canc@thinlines
        {\/\raise\dimen@\hbox{\expandafter\canto@vector\@tempa{#2}{#3}}}%
        \@end@tempboxa
    }

    \def\bcancelto#1#2{\mathchoice    %  smaller option (default)
      {\@cancelto[-]\textstyle{#1}\displaystyle{#2}}%
      {\@cancelto[-]\scriptstyle{#1}\textstyle{#2}}%
      {\@cancelto[-]\scriptscriptstyle{#1}\scriptstyle{#2}}%
      {\@cancelto[-]\scriptscriptstyle{#1}\scriptscriptstyle{#2}}%
      }
\makeatother
\begin{document}
\[
\cancelto{0}{X^2}
\]
\[
\bcancel{X^2}
\]
\[
\bcancelto{0}{X^2}
\]
\end{document}

As a note, I'd recommend using the makeroom option to accommodate the arrow head for the \bcancelto.

Related Question