我在其他主题中阅读了代码:定义了newcommand selectTask。
如果 TaskOne/TaskTwo = 空白则可以。
现在我想编辑 IF TaskOne/TaskTwo = 空白或零(0)。
请帮助更新此代码:
\newcommand{\selectTask}[2]{%
\if\relax#1\relax
\if\relax#2\relax
\selectTaskA % 00 TaskOne = TaskTwo=blank => How if blank or zero?
\else
\selectTaskB % 01 TaskOne =blank,TaskTwo=!=blank
\fi
\else
\if\relax#2\relax
\selectTaskC % 10 TaskOne !=blank, TaskTwo=blank
\else
\selectTaskD % 11 TaskOne = TaskTwo!=blank
\fi
\fi
}
如果数据为零或空白,请帮忙。提前谢谢
答案1
\if\relax#1\relax
如果扩展的#1
结果以任何非字符开头,则为真。\if\relax\hbox{abc}\relax
例如,测试结果为真(和类型为abc
)。
这使用\ifx
不扩展测试标记的测试。
\def\tmpempty{}
\def\tmpzero{0}
\newcommand{\selectTask}[2]{%
\def\tmp{#1}\ifx\tmp\tmpempty\let\tmp\tmpzero\fi
\def\tmpb{#2}\ifx\tmpb\tmpempty\let\tmpb\tmpzero\fi
\ifx\tmp\tmpzero
\ifx\tmpb\tmpzero
\show\selectTaskA % 00 TaskOne = TaskTwo=blank => How if blank or zero?
\else
\show\selectTaskB % 01 TaskOne =blank,TaskTwo=!=blank
\fi
\else
\ifx\tmpb\tmpzero
\show\selectTaskC % 10 TaskOne !=blank, TaskTwo=blank
\else
\show\selectTaskD % 11 TaskOne = TaskTwo!=blank
\fi
\fi
}
\selectTask{}{}%A
\selectTask{0}{}%A
\selectTask{}{x}%B
\selectTask{0}{x}%B
\selectTask{x}{}%C
\selectTask{x}{0}%C
\selectTask{x}{y}%D
\stop
答案2
以下是按照您的想法实现的:测试第一个参数是否为空,如果为空则使用参数调用以下宏0
;同样测试第二个参数。然后我们进行字符串大小写选择。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\selectTask}{mm}
{
\latexforti_select_task:nn { #1 } { #2 }
}
\cs_new:Nn \latexforti_select_task:nn
{
\tl_if_blank:nTF { #1 }
{
\__latexforti_select_task_a:nn { 0 } { #2 }
}
{
\__latexforti_select_task_a:nn { #1 } { #2 }
}
}
\cs_new:Nn \__latexforti_select_task_a:nn
{
\tl_if_blank:nTF { #2 }
{
\__latexforti_select_task_b:nn { #1 } { 0 }
}
{
\__latexforti_select_task_b:nn { #1 } { #2 }
}
}
\cs_new:Nn \__latexforti_select_task_b:nn
{
\str_case:nn { #1#2 }
{
{00}{\selectTaskA}
{01}{\selectTaskB}
{10}{\selectTaskC}
{11}{\selectTaskD}
}
}
\ExplSyntaxOff
\newcommand{\selectTaskA}{I'm task A}
\newcommand{\selectTaskB}{I'm task B}
\newcommand{\selectTaskC}{I'm task C}
\newcommand{\selectTaskD}{I'm task D}
\begin{document}
\selectTask{0}{0} --- \selectTask{}{0} -- \selectTask{0}{}
\selectTask{0}{1} --- \selectTask{}{1}
\selectTask{1}{0} --- \selectTask{1}{}
\selectTask{1}{1}
\end{document}
具有更多经典方法的实现(但参数不能{ }
像前一个那样)。
\documentclass{article}
\makeatletter
\newcommand{\selectTask}[2]{%
\expandafter\latexforti@select\expandafter
{\number0#1\expandafter}\expandafter{\number0#2}%
}
\newcommand{\latexforti@select}[2]{%
\ifcase\numexpr#1*2+#2\relax % the arguments are binary digits!
\selectTaskA
\or
\selectTaskB
\or
\selectTaskC
\or
\selectTaskD
\fi
}
\makeatother
\newcommand{\selectTaskA}{I'm task A}
\newcommand{\selectTaskB}{I'm task B}
\newcommand{\selectTaskC}{I'm task C}
\newcommand{\selectTaskD}{I'm task D}
\begin{document}
\selectTask{0}{0} --- \selectTask{}{0} -- \selectTask{0}{}
\selectTask{0}{1} --- \selectTask{}{1}
\selectTask{1}{0} --- \selectTask{1}{}
\selectTask{1}{1}
\end{document}
答案3
如果参数是仅有的允许为空白或数字,则可以使用\ifnum
比较。
\documentclass[]{scrartcl}
\def\selectTaskA{00}
\def\selectTaskB{01}
\def\selectTaskC{10}
\def\selectTaskD{11}
\newcommand{\selectTask}[2]{%
\ifnum9=#19\relax
\ifnum9=#29\relax
\selectTaskA
\else
\selectTaskB
\fi
\else
\ifnum9=#29\relax
\selectTaskC
\else
\selectTaskD
\fi
\fi
}
\begin{document}
\selectTask{}{}\\
\selectTask{0}{}\\
\selectTask{}{0}\\
\selectTask{0}{0}\\
\selectTask{}{1}\\
\selectTask{1}{}\\
\selectTask{1}{1}
\end{document}
答案4
这是一个基于扩展的变体,即,不进行任何临时赋值。除此之外,它不进行任何\if..
-primitives(这可以避免参数本身包含不平衡的\if..
/ \else
/时出现的问题\fi
)。
\documentclass[a4paper]{article}
\usepackage{textcomp}
\makeatletter
%%----------------------------------------------------------------------
%% Check whether argument is empty:
%%......................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is empty>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not empty>}%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter\@secondoftwo\string{\expandafter
\@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
\@secondoftwo\string}\expandafter\expandafter\@firstoftwo{ }{}%
\@secondoftwo}{\expandafter\expandafter\@firstoftwo{ }{}\@firstoftwo}%
}%
%%----------------------------------------------------------------------
%% Check whether argument is blank (empty or only space tokens):
%%......................................................................
%% -- Take advantage of the fact that TeX discards space tokens when
%% "fetching" _un_delimited arguments: --
%% \UD@CheckWhetherBlank{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that
%% argument which is to be checked is blank>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not blank}%
\newcommand\UD@CheckWhetherBlank[1]{%
\romannumeral\expandafter\expandafter\expandafter\@secondoftwo
\expandafter\UD@CheckWhetherNull\expandafter{\@firstoftwo#1{}.}%
}%
%%----------------------------------------------------------------------
%% Check whether argument is blank (empty or only space tokens) or
%% consists only of a _single_ token "0" (charcode 48(dec), catcode 12(dec))
%%......................................................................
\newcommand\UD@GobbleToExclam{}\long\def\UD@GobbleToExclam#1!{}%
\newcommand\UD@forkzero{}\long\def\UD@forkzero#1!!0!#2#3!!!!{#2}%
\newcommand\UD@zerofork[3]{%
\expandafter\UD@CheckWhetherNull\expandafter{\UD@GobbleToExclam#1!}{%
\UD@forkzero!#1!0!{#3}%<-case #1 is empty/has no tokens
!!#1!{#2}%<-case #1 = "0"
!!0!{#3}%<-case #1 something else without exclamation mark
!!!!%
}{#3}%<-case #1 = something else with exclamation-mark.
}%
\newcommand\UD@CheckWhetherBlankOrZero[1]{%
\romannumeral0\UD@CheckWhetherBlank{#1}{%
\@firstoftwo{\expandafter}{} \@firstoftwo
}{%
\UD@zerofork{#1}%
{\@firstoftwo{\expandafter}{} \@firstoftwo}%
{\@firstoftwo{\expandafter}{} \@secondoftwo}%
}%
}%
%%----------------------------------------------------------------------
%% \selectTask
%%......................................................................
\newcommand{\selectTask}[2]{%
\UD@CheckWhetherBlankOrZero{#1}{%
\UD@CheckWhetherBlankOrZero{#2}{% <blank or zero> = either spaces or a single "0" or no tokens at all
\selectTaskA % #1 = #2 = <blank or zero>
}{%
\selectTaskB % #1 = <blank or zero>; #2 =/= <blank or zero>
}%
}{%
\UD@CheckWhetherBlankOrZero{#2}{%
\selectTaskC % #1 =/= <blank or zero>; #2 = <blank or zero>
}{%
\selectTaskD % #1 =/= <blank or zero>; #2 =/= <blank or zero>
}%
}%
}%
\newcommand{\selectTaskA}{%
Task A---%
\string##1 \textit{\textlangle blank or zero\textrangle }
and
\string##2 \textit{\textlangle blank or zero\textrangle }%
.%
}
\newcommand{\selectTaskB}{%
Task B---%
\string##1 \textit{\textlangle blank or zero\textrangle }
and
\string##2 not \textit{\textlangle blank or zero\textrangle }%
.%
}
\newcommand{\selectTaskC}{%
Task C---%
\string##1 not \textit{\textlangle blank or zero\textrangle }
and
\string##2 \textit{\textlangle blank or zero\textrangle }%
.%
}
\newcommand{\selectTaskD}{%
Task D--- %
\string##1 not \textit{\textlangle blank or zero\textrangle }
and
\string##2 not \textit{\textlangle blank or zero\textrangle }%
.%
}
\makeatother
\parindent=0pt
\parskip=\baselineskip
\addtolength\oddsidemargin{-1.5cm}
\addtolength\evensidemargin{-1.5cm}
\addtolength\textwidth{3cm}
\begin{document}
\verb*|\selectTask{0}{0}|: \selectTask{0}{0}\\
\verb*|\selectTask{ }{0}|: \selectTask{ }{0}\\
\verb*|\selectTask{0}{ }|: \selectTask{0}{ }\\
\verb*|\selectTask{ }{ }|: \selectTask{ }{ }\\
\verb*|\selectTask{}{0}|: \selectTask{}{0}\\
\verb*|\selectTask{0}{}|: \selectTask{0}{}\\
\verb*|\selectTask{}{}|: \selectTask{}{}\\
\verb*|\selectTask{}{ }|: \selectTask{}{ }\\
\verb*|\selectTask{ }{}|: \selectTask{ }{}
\verb*|\selectTask{0}{1}|: \selectTask{0}{1}\\
\verb*|\selectTask{ }{1}|: \selectTask{ }{1}\\
\verb*|\selectTask{}{1}|: \selectTask{}{1}
\verb*|\selectTask{1}{0}|: \selectTask{1}{0}\\
\verb*|\selectTask{1}{ }|: \selectTask{1}{ }\\
\verb*|\selectTask{1}{}|: \selectTask{1}{}
\verb*|\selectTask{1}{1}|: \selectTask{1}{1}
\textbf{BUT:}
\verb*|\selectTask{00}{00}|: \selectTask{00}{00}
\verb*|\selectTask{ 0 }{ 0 }|: \selectTask{ 0 }{ 0 }
\let\implicitzero=0%
\def\expandtospace{ }%
\verb*|\let\implicitzero=0|\\
\verb*|\def\myspace{ }|\\
\verb*|\selectTask{\implicitzero}{\implicitzero}|:\\ \selectTask{\implicitzero}{\implicitzero}
\verb*|\selectTask{\expandtospace}{\expandtospace}|:\\ \selectTask{\expandtospace}{\expandtospace}
\end{document}