2

I have this MWE

\documentclass{article}

\def\posa(#1) {%
    \ifx #1+ Plus \else \ifx #1- Minus\else Retry \fi \fi
}   
\begin{document}

    \posa(+)

\end{document}

Which compare argument of \posa command with + and -, I need to remove space before and after argument of \posa so that even if we wrote:

\posa( +) or \posa(+ ) or \posa( + ) we get Plus as result (same with -)

Salim Bou
  • 17,021
  • 2
  • 31
  • 76

4 Answers4

2

None of this is really latex, but since you are using def

enter image description here

\documentclass{article}

\def\posa#1#2#3{%
    \ifx +#2Plus\else \ifx-#2Minus\else Retry \fi\fi
}   
\begin{document}

    \posa(+)

    \posa( -)

    \posa(?)

    \posa( + )

\end{document}

Your original had a required space after the ) but I removed that requirement here.

Note that it is safer to use \ifx+#1 than \ifx#1+ for example in your original if you use \posa(zz) the first test \ifx #1+ would be true as it would test z and z.

David Carlisle
  • 757,742
2

Here, I use a listofitems package \readlist* to read the argument as a 1-item list, with leading/trailing spaces stripped. Then I can do an \if (expanded) comparison on whether the list item is a + or -.

\documentclass{article}
\usepackage{listofitems}
\def\posa(#1){%
    \readlist*\myarg{#1}
    \if+\myarg[1]Plus\else \if-\myarg[1]Minus\else Retry \fi\fi
}   
\begin{document}

    \posa(+)

    \posa( -)

    \posa(?)

    \posa( + )

\end{document}

enter image description here

If one needs the test to be done with the non-expanded argument of \posa, then this would suffice, since \myarg[1], when twice expanded, yields the token(s) of the argument (leading/trailing spaces removed).

\documentclass{article}
\usepackage{listofitems}
\def\posa(#1){%
    \readlist*\myarg{#1}
    \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tmp%
      \expandafter\expandafter\expandafter{\myarg[1]}%
    \expandafter\ifx\expandafter+\tmp Plus\else%
      \expandafter\ifx\expandafter-\tmp Minus\else Retry \fi\fi%
}   
\begin{document}

    \posa(+)

    \posa( -)

    \posa(?)

    \posa( + )

\end{document}

In plain TeX, use \input{listofitems} instead of \usepackage{listofitems}.

2

I am not sure I can explain why, but adding a second argument will suppress the spaces.

\documentclass{article}
\def\posa(#1#2){%
  \ifx+#1Plus\else\ifx-#1Minus\else Retry\fi\fi
}   
\begin{document}
    \posa(+)

    \posa( -)

    \posa(?)

    \posa( + )

    \posa(- )

    \posa(zz)
\end{document}

enter image description here

StefanH
  • 13,823
2

With xparse we have easier syntax:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\posa}{ r() }
 {
  \str_case_x:nnF { \tl_trim_spaces:n { #1 } }
   {
    {+}{Plus}
    {-}{Minus}
   }
   {Retry}
 }
\ExplSyntaxOff

\begin{document}

X\posa(+)X\posa( +)X\posa(+ )X\posa( + )X

X\posa(-)X\posa( -)X\posa(- )X\posa( - )X

X\posa(=)X\posa( =)X\posa(= )X\posa( = )X

\end{document}

enter image description here

egreg
  • 1,121,712