Brownian motion

around the essentials…

 

TeX: testing if string is empty

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).

(No Ratings Yet)

2 коментарі to “TeX: testing if string is empty”

  1. Klaas says:

    You could use the following as well.

    \ifx#2\relax%
    \else%
    { \textit{(#2)}}%
    \fi

Leave a Reply

XHTML: Можете використовувати такі теґи: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts

Tags

Archives