答案1
我认为这就是你所追求的:
这是通过首先修补\canto@vector
命令来更改脚本位置来实现的,该命令基于向量 y 分量的符号(由 给出#4
)。然后,\@cancelto
使用可选参数更新。如果为空,则将实现标准行为。如果-
传递了 a,则向量指向下方。最后,\bcancelto
创建命令,假设smaller
使用默认选项(Smaller
或samesize
需要替代定义来修改使用的脚本大小)。
您修改后的 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}
需要注意的是,我建议使用makeroom
选项来容纳箭头\bcancelto
。