# How to test if a string is empty

I tried several thing to test if a parameter is empty or equal to a specific carater. How should one proceed ?

By the way, what is the best way to have conditionnal code on a parameter who can have three values : empty, v, h ?

\documentclass{standalone}
\newcommand{\Test}[3]{%
\ifnum #1=1 C'est \'egal à 1 \fi
\if #2v C'est \'egal à v \fi
\ifx #2v C'est \'egal à v \fi
\if #3\empty C'est vide \fi
\ifx #3\empty C'est vide \fi
}

\begin{document}
\Test{1}{v}{}
\end{document}


Never says vide...

• Have you seen \ifblank or \ifstrempty in etoolbox? – egreg May 23 '14 at 13:04
• I use the ifthen-package and use it like: \ifthenelse{\equal{#1}{}}{it's empty}{it's not empty} – musicman May 23 '14 at 13:10
• probably a duplicate of tex.stackexchange.com/questions/53068/… – David Carlisle May 23 '14 at 13:15
• @musicman Yes of course, I just forget about it. – Tarass May 23 '14 at 13:44

If using the primitive TeX if syntax you should always take care to put the test token first before user supplied token, otherwise you have no control over what is being tested:

\documentclass{standalone}
\newcommand{\Test}[3]{%
\ifnum #1=1 C'est \'egal à 1 \fi
\if #2v C'est \'egal à v \fi
\ifx #2v C'est \'egal à v \fi
\if #3\empty C'est vide \fi
\ifx #3\empty C'est vide \fi
}

\begin{document}
\Test{2=2}{aa}{!!}
\end{document}


The 4th test will never test that #3 is empty, if given a single character it tests if it is C.

The exact test you should use depends on your definition of empty. which of these is "empty" where the outer {} are the delimiters of the argument: {} { } {\empty} {\mbox{}}

I normally do something like

 \ifx\som@internal@macro#1\som@internal@macro


where \som@internal@macro is some defined macro in the file that is unlikely to be used in the argument. Then if #1 is empty the test is

 \ifx\som@internal@macro\som@internal@macro


so tests true, If #1 is non empty (and does not start with \som@internal@macro) then it tests false.

• while true, this doesn't show an example of what would work. that would be a kindness. – barbara beeton May 23 '14 at 14:20
• @barbarabeeton perhaps but the "how to do it part" is a duplicate question (and I gave a link to answered in comment on the question) the best way depends on what you want to d really:-) so here I just addressed the code sample. To make you happy I'll add one working example:-) – David Carlisle May 23 '14 at 14:23
• Thank you for your answer, explainations and link. Why \som@internal@macro should the be defined, as in the linked example it is not : \ifx\hfuzz#3\hfuzz ? – Tarass May 23 '14 at 17:05
• @Tarass\hfuzz is defined (it is a TeX primitive) you want something that is unlikely to appear in #1 but any undefined command is \ifx equal so if you use \my@undefined#1\my@undefined but the user mistypes #1 as \sectiom then it would test as true and try to execute the second guard token. – David Carlisle May 23 '14 at 18:41

Here's David Carlisle's solution in a nice macro called \ifempty. Use it in place of an \if as shown. Make sure to wrap your argument to \ifempty in curly brackets.

\def \ifempty#1{\def\temp{#1} \ifx\temp\empty }

\def \mymacro#1{ \ifempty{#1} empty \else not empty \fi }

\mymacro{something} % prints "not empty"
\mymacro{}          % prints "empty"
