这是一个困扰了我几天的问题。基本上,我尝试解析字符串并逐个测试它们的符号来确定它们是否是数字(为了实现一些更复杂的处理,超出了这个主题)。
宏\@neByOne
、\aux@neByOne
和\c@untunlessnil
拆分\gobblechar
字符串。对于每个符号,\testSymbol
都会调用宏来判断它是否是数字。
问题:直接调用时,\testSymbol
返回预期结果。但是当通过调用时\c@untunlessnil
,测试失败。实际上,参数#1
在通过时不被视为数字\ifnum
。以下是我的文件和我得到的结果。
感谢您的帮助。
内容myExtension.sty
\def\testSymbol#1{%
\ifnum0<0#1\relax%
<<#1>> is a number\\
\else%
<<#1>> is a NOT number\\
\fi%
}
\def\gobblechar{\let\ch@r= }
\def\c@untunlessnil{%
\ifx\ch@r\nil%
\let\next=\relax%
\else%
\testSymbol\ch@r%
\let\next=\aux@neByOne%
\fi\next
}
\def\aux@neByOne{\afterassignment\c@untunlessnil\gobblechar}
\def\@neByOne#1{\edef\xx{#1}\expandafter\aux@neByOne\xx\nil}
\def\test#1{BEGIN\\\@neByOne{#1}END\\}
内容myMainFile.tex
:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{myExtension}
\begin{document}
\test{123}
\testSymbol{1}
\testSymbol{2}
\testSymbol{3}
我得到的输出:
BEGIN
«1» is a NOT number
«2» is a NOT number
«3» is a NOT number
END
«1» is a number
«2» is a number
«3» is a number
答案1
您可以使用以下测试
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\makeatletter
\def\test#1{\@tfor\tmp:=#1\do{%
<<\tmp>>
\ifcat1\expandafter\noexpand\tmp
\ifnum\expandafter`\tmp>47
\ifnum\expandafter`\tmp<58
IS
\else
IS NOT
\fi
\else
IS NOT
\fi
\else
IS NOT
\fi
a number\par
}}
\begin{document}
\test{123}
\test{1}
\test{2}
\test{3}
\test{4abc5}
\end{document}