1

I am trying to check if a variable is defined without using extra packages (since I want ot use it in an .lco file) and if it is defined then check if it is empty.

The definition check works but the check for empty content does not work:

\documentclass{scrlttr2}

\newcommand{\stampfile}{}

\begin{document}

    \ifdefined\stampfile
        \ifx\stampfile\empty
            \textbackslash stampfile is empty
        \else
            \textbackslash stampfile is not empty
        \fi
    \else
        \textbackslash stampfile is not defined
    \fi

\end{document}

I has a look at How to check if a macro value is empty or will not create text with plain TeX conditionals? but the solutions did work either...

E.g. I tried:

\documentclass{scrlttr2}

\newcommand{\stampfile}{}

\newcommand{\checkempty}[1]{ \if\relax\detokenize{#1}\relax EMPTY% \else NON EMPTY% \fi }

\begin{document}

    \ifdefined\stampfile
        \ifx\stampfile\empty
            \textbackslash stampfile is empty
        \else
            \textbackslash stampfile is not empty
        \fi
    \else
        \textbackslash stampfile is not defined
    \fi

    \checkempty{\stampfile}

\end{document}

mrCarnivore
  • 1,505

1 Answers1

2

\empty is not long. If you want to use it as comparision in a \ifx text you should define your stamp command with \newcommand*. It is imho easier to test for blankness:

\documentclass{scrlttr2}

\newcommand*{\stampfile}{} %<-- not long \ExplSyntaxOn \cs_set_eq:NN\tlifblankTF\tl_if_blank:VTF \ExplSyntaxOff

\begin{document}

    \ifdefined\stampfile
        \ifx\stampfile\empty
            \textbackslash stampfile is empty
        \else
            \textbackslash stampfile is not empty
        \fi
    \else
        \textbackslash stampfile is not defined
    \fi


\tlifblankTF\stampfile{empty}{not empty}

\renewcommand\stampfile{ }

\tlifblankTF\stampfile{empty}{not empty}

\renewcommand\stampfile{xxx}

\tlifblankTF\stampfile{empty}{not empty}

\end{document}

Ulrike Fischer
  • 327,261
  • Thank you. Using you \tlifblankTF does work. Also the hint with * is very helpful. Thanks!Can you point me into a direction of good reading material to learn how the parts between the \ExplSyntaxOn works? I have seen these in solutions a few times but never really understood them to use them myself. I do know Python, C++ and a few other languages... – mrCarnivore Apr 21 '23 at 10:08
  • 1
    @mrCarnivore texdoc interface3 – Frank Mittelbach Apr 22 '23 at 07:56