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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

check if a parameter is empty

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

check if a parameter is empty

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

JakobHeiss
Posts: 1
Joined: Wed Feb 28, 2024 9:54 pm

check if a parameter is empty

Postby JakobHeiss » Wed Feb 28, 2024 10:01 pm

\isempty{#1}


is a good alternative instead of

\equal{#1}{}}


So, in your example, I would use

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


Return to “General”

Who is online

Users browsing this forum: No registered users and 34 guests