我正在开发一个文档命令,它有各种可选参数,用于不同的结果。我需要添加一个选项,使可选参数在数字范围内(例如 100-200)产生“true”。命令如下(MWE):
\documentclass{article}
\usepackage{xparse,xstring,xspace}
\NewDocumentCommand{\Foo}{ o }{%
\IfNoValueTF{#1} {%
\emph{Foo}\xspace}{%
\IfBeginWith{#1}%
{b}{\emph{Foo} Bar\xspace}{%
\IfStrEqCase{#1}{%
{f}{\emph{Foo}\xspace}%
}%
[\emph{Foo}~#1\xspace]%
}%
}%
}
\begin{document}
\Foo
\Foo[b]
\Foo[3]
\end{document}
xstring 似乎没有范围选项。
答案1
这是这xstring
版本:
笔记:
- 我使用
pgf
数学测试来测试数字的符号,但是如果您愿意,也可以通过采用以下解决方案来做到这一点如何测试一个数字是否为负数。 - 我
\IsInteger
用过如何检查参数的值是否为数字?因为这处理空字符串的情况。如果不需要,可以使用xstring's
\IfInteger
直接地。
代码:
\documentclass{article}
\usepackage{xstring}
\usepackage{pgf}
% https://tex.stackexchange.com/questions/50111/how-to-check-if-the-value-of-a-parameter-is-a-number
\newcommand*{\IsInteger}[3]{%
\IfStrEq{#1}{ }{%
#3% is a blank string
}{%
\IfInteger{#1}{#2}{#3}%
}%
}%
% https://tex.stackexchange.com/questions/15297/how-to-test-if-a-number-is-negative
\newcommand{\IsZeroOrPositive}[3]{%
% #1 = number to test
% #2 = code to executer if positive
% #3 = code to execute if non-postiive
\pgfmathsetmacro{\varX}{#1}%
\pgfmathparse{ifthenelse(\varX>=0,1,0)}%
\if\pgfmathresult 1 \relax%
#2%
\else%
#3%
\fi%
}%
\newcommand*{\RangeMin}{100}%
\newcommand*{\RangeMax}{200}%
\newcommand*{\IsInRange}[3]{%
% #1 = number to test
% #2 = code to execute if in range
% #3 = code to execute if NOT in range, or #1 NOT an integer
\IsInteger{#1}{%
\IsZeroOrPositive{#1-\RangeMin}{%
\IsZeroOrPositive{\RangeMax-#1}{%
#2% Number is in range
}{%
#3% Number not in range
}%
}{%
#3% Number not in range
}%
}{%
#3% Not a number
}%
}
\begin{document}
Character ``a" is \IsInRange{a}{in range}{not in range}.\par
Empty string is \IsInRange{}{in range}{not in range}.\par
Blank string is \IsInRange{ }{in range}{not in range}.\par
\medskip
2 is \IsInRange{2}{in range}{not in range}.\par
99 is \IsInRange{99}{in range}{not in range}.\par
100 is \IsInRange{100}{in range}{not in range}.\par
111 is \IsInRange{111}{in range}{not in range}.\par
200 is \IsInRange{200}{in range}{not in range}.\par
201 is \IsInRange{201}{in range}{not in range}.
\end{document}
答案2
我的建议是测试可选参数中的第一个标记;如果它是一个数字,我们假设它是一个数字:
\documentclass{article}
\usepackage{xparse,xspace}
\ExplSyntaxOn
\NewDocumentCommand{\foo}{ o }
{
\IfNoValueTF{#1}
{
\emph{Foo}\xspace
}
{
\rowthorn_check_arg:n { #1 }
}
}
\cs_new_protected:Npn \rowthorn_check_arg:n #1
{
\tl_if_in:nfTF {0123456789} { \tl_head:n { #1 } }
{
\rowthorn_numeric_arg:n { #1 }
}
{
\rowthorn_string_arg:n { #1 }
}
}
\cs_generate_variant:Nn \tl_if_in:nnTF { nf }
\cs_new_protected:Npn \rowthorn_numeric_arg:n #1
{
\int_compare:nTF { #1 > 343 }
{
\emph{Foo}~(#1)
}
{
\emph{Foo}
}
}
\cs_new_protected:Npn \rowthorn_string_arg:n #1
{
\emph{Foo}~[#1]
}
\ExplSyntaxOff
\begin{document}
\foo test
\foo[bxx] test
\foo[f] test
\foo[fx] test
\foo[3] test
\foo[400] test
\end{document}
是否完成规范取决于你。
答案3
\documentclass{minimal}
\usepackage{xspace}
\newcommand\isnum[3]{%
\if!\ifnum9<1#1!\else_\fi % a number or something else?
\ifnum#1>99\relax % we have a number > 99
\ifnum#1<201\relax #2 % it is in [100;200]
\else #3\fi % not a number or not in [100;200]
\else #3\fi
\else #3\fi\xspace}
\newcommand\Foo[1][a]{\isnum{#1}{true}{false}}
\begin{document}
\Foo, \Foo[b], \Foo[3], \Foo[155]
\end{document}