TeX: testing if string is empty

Posted on

TeX has several «functions» for «conditional compilation», but there is no built-in for checking if «this string is equal to that». For example, now i need to check if some string (second parameter to my macro) is empty.

But this can be achieved by making two macros — «needed string» and «given string» — and comparing these:

\def\myMacro#1#2{%
\def\reallyempty{}%
\def\secondarg{#2}%
#1%
\ifx\secondarg\reallyempty
  {}
\else
  { \textit{(#2)}}
\fi%
}

Why one may need it? Let’s say i need to format some songbook, and want to format «author» and «arranger» (if any) in some specific way. Example above will print «author» and — if any — «arranger» in parentheses and italicized.

Using such a macro is very useful if you may decide to change this «specific format» — you need to change only this macro (throw out parentheses, insert blank line between fields etc).

2 Replies to “TeX: testing if string is empty”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.