如果可能的话,请仅使用普通的 TeX + eTeX 命令(我在 XeLaTeX 中编译但使用 TeX 命令)。
我需要参数值为整数值,而不是小数。在此示例中,参数被赋值给\count255
。但如果参数包含小数点,则显示数字的小数。因此,我需要以某种方式触发错误(或在赋值期间或之前删除小数部分),而不是显示小数。
\documentclass[border=5mm,varwidth]{standalone}
\def\zTest#1{%
\count255=#1 % how to generate error on decimal point?
\the\count255 % displays integer, but decimal has already been displayed as a side effect of assignment
}
\begin{document}
\zTest{3.7} % would assign 3 to \count but display .7 as side effect
\end{document}
答案1
您可以提取传递的数字的小数部分,然后测试该小数部分是否非空:
\documentclass{article}
\makeatletter
\def\zTestdot#1.#2\@nil{#2}
\def\zTest#1{%
\edef\temp{\zTestdot#1.\@nil}% Capture decimal content
\if\relax\temp\relax
\count255=#1 %
\the\count255 %
\else % Decimal content existed
\@latex@error{Don't include decimals to \string\zTest}{}
\fi
}
\makeatother
\begin{document}
\zTest{3.7} % would assign 3 to \count but display .7 as side effect
\end{document}
答案2
您可以使用\afterassignment
:
\def\zTest#1{\afterassignment\zTestAux\count255=#1\relax}
\def\zTestAux#1{\ifx\relax#1\else\ERROR\expandafter\zTestGobble\fi}
\def\zTestGobble#1\relax{}
\zTest{3}\the\count255\ should print 3
\zTest{\count255}\the\count255\ should print 3
\zTest{4.1}\the\count255\ should print 4 and raise error
\bye
定义\ERROR
做你最喜欢的事。