在我的工作中,我需要一个宏来区分number
和length
。这里的长度意味着,例如:
(1)\textwidth
、\parindent
、 等
(2)\newdimen\mylena \mylena=10cm
(3)\def\mylenb{2in}
(4)5.2in
我希望宏像这样工作:
\documentclass{article}
\begin{document}
\newdimen\mylena \mylena=10cm
\def\mylenb{2in}
\def\mynum{5.2}
\newcommand{\numorlen}[1]{
% if #1 is a number, do something, here, to be simple, just typeset "It's a number".
% if #1 is a length, do something, here, to be simple, just typeset "It's a length".
}
\numorlen{\textwidth} % typeset It's a length
\numorlen{\mylena} % typeset It's a length
\numorlen{\mylenb} % typeset It's a length
\numorlen{5.2in} % typeset It's a length
\numorlen{5.2} % typeset It's a number
\numorlen{\mynum} % typeset It's a number
\end{document}
答案1
这是一种简单的方法。它实际上并不检查输入是数字还是长度,而只检查其扩展参数的最右边的字符是否是整数。假设唯一的可能性是数字和长度,它就可以工作。但是,\numorlen{m2}
会被错误地识别为数字。同样,\numorlen{2m}
会被错误地识别为长度。
\documentclass{article}
\usepackage{xstring}
\newdimen\mylena \mylena=10cm
\def\mylenb{2in}
\def\mynum{5.2}
\newcommand{\numorlen}[1]{\StrRight{#1}{1}[\lastchr]\IfInteger{\lastchr}{It's a number}{It's a length}}
\begin{document}
\numorlen{\textwidth} % typeset It's a length
\numorlen{\mylena} % typeset It's a length
\numorlen{\mylenb} % typeset It's a length
\numorlen{5.2in} % typeset It's a length
\numorlen{5.2} % typeset It's a number
\numorlen{\mynum} % typeset It's a number
\end{document}