3

i read code in other topic: definited newcommand selectTask.

IF TaskOne/TaskTwo = blank is ok.

Now i want to edit IF TaskOne/TaskTwo = blank or zero (0).

Please help update this code:

\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
}

Please help if data = zero or blank. Thank in advance

  • 3
    \if\relax#1\relax is not a good test – David Carlisle Jun 13 '19 at 20:22
  • 2
    Adding to @DavidCarlisle's first comment: A better test would be \if\relax\detokenize{#1}\relax. – Skillmon Jun 13 '19 at 20:29
  • 1
    There is also \if\empty#1\relax, but for best results use something like \def\@tempa{#1} to do comparisons. – John Kormylo Jun 13 '19 at 20:34
  • 1
    If the arguments will always be numeric (or blank), \newcommand{\selectTask}[2]{\ifnum9=#19\relax \ifnum9=#29\relax \selectTaskA \else \selectTaskB \fi \else \ifnum9=#29\relax \selectTaskC \else \selectTaskD \fi \fi } – Steven B. Segletes Jun 13 '19 at 20:38
  • 1
    A test for "blankness", i.e., only space tokens or no tokens at all, could be: \long\def\firstoftwo#1#2{#1}\long\def\secondoftwo#1#2{#2}...\if\relax\detokenize\expandafter{\firstoftwo#1{}.}\relax\expandafter\firstoftwo\else\expandafter\secondoftwo\fi{<blank>}{<not blank>} – Ulrich Diez Jun 14 '19 at 12:24
5

\if\relax#1\relax would be true if the expansion of #1 results in something that starts with any non-character. \if\relax\hbox{abc}\relax for example would test as true (and typeset abc).

this uses \ifx tests which do not expand the tested tokens.

\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
| improve this answer | |
4

Here's an implementation according to your idea: the first argument is tested if blank, if it is the following macro is called with 0 argument; similarly the second argument is tested. Then we do a string case choice.

\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}

enter image description here

An implementation with more classical methods (but arguments cannot be { } as in the previous one).

\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}
| improve this answer | |
2

If the arguments are only allowed to be blank or numeric, you can use an \ifnum comparison.

\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}
| improve this answer | |
1

This is a variant which is expansion-based, i.e., does without whatsoever temporary assignments. Besides this it does without any \if..-primitives (which avoids problems in case the arguments themselves contain unbalanced \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}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.