LaTeX forum ⇒ Generalcheck if a parameter is empty

LaTeX specific issues not fitting into one of the other forums of this category.
kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am

check if a parameter is empty

Postby kostka » Tue Sep 15, 2009 4:03 am

I want to have a command that does one thing if the parameter is empty, and another if it is nonempty. In pseudocode, something like the following:

\newcommand{\card}[1]{
if #1 is empty:
Empty argument.
else:
The argument is #1.
end if
}


I could find information on checking if a macro is empty, but it didn't work for an argument. Is there a solution for this? Many thanks.

Tim
Last edited by kostka on Tue Sep 15, 2009 9:28 am, edited 1 time in total.

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Postby phi » Tue Sep 15, 2009 9:00 am

\ifx&#1&%
  % #1 is empty
\else
  % #1 is nonempty
\fi
The etoolbox package offers \ifstrempty, a more robust and “LaTeX-like” solution.

kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am

Postby kostka » Tue Sep 15, 2009 9:28 am

Thanks very much, I like that format the best. I also found the following way to do it:

\usepackage{ifthen}

\newcommand{\card}[1]{
\ifthenelse{\equal{#1}{}}{Empty.}{Nonempty.}
}


Tim


Return to “General”

Who is online

Users browsing this forum: No registered users and 8 guests