1

My MWE was created with the help of these two Q&As: 1 and 2.

MWE:

\documentclass{article}
\usepackage{xparse}
\usepackage{xstring}
\usepackage{pdftexcmds}

% Command declaring a global definition if argument not empty, and using a pre-defined empty definition if argument empty
\ExplSyntaxOn%
\NewDocumentCommand{\Client}{g}%
  {%
     \IfNoValueTF{#1}%
      {%
        \tl_use:N \g_jalep_client_tl%
      }%
      {%
        \tl_gset:Nn \g_jalep_client_tl {#1}%
      }%
  }%
\tl_new:N \g_jalep_client_tl%
\ExplSyntaxOff%

% Attempt using \pdfstrcmp 
\makeatletter
\newcommand\foo[1]{%
  \ifnum\pdf@strcmp{\unexpanded{#1}}{Adam}=0 %
     \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
    {Adam}
    {Eve}%
}
\makeatother

\begin{document}
\Client{Adam}
% Attempt using xstring
\par My client should be Adam: \IfSubStr{Adam}{\Client}{Adam}{Eve}
% Attempt using \pdfstrcmp
\par My client should be Adam: \foo{\Client}
\end{document}

Output:

enter image description here

Notes:

  1. The \IfSubStr and \foo line work when there exists something like: \newcommand{\Client}{Adam} or \newcommand{\Client}{Eve} or \newcommand{\Client}{Anything}.
  2. From this, I'm inclined to believe \Client produces something other than a string? How do I change this/convert this/work with this when I'm trying to create an if-else statement?

Question:

How do I make a string comparison with the \NewDocumentCommand shown in my MWE? Something like: If \Client = Adam, then print: Adam, else print: Eve

  • 2
    Are you open to alterations to the definition of \Client? – Werner Jan 16 at 1:01
  • @Werner Yes, though in my document: 1. \Client is always called either empty like this: \Client{} or with any argument like this: \Client{Argument}. 2. \Client{} or \Client{Argument} should print nothing, and only the command \Client should print whatever the global definition defined with \Client is – jed Jan 16 at 2:05
  • 2
    So, a first call to \Client{<name>} destroys its and just stores the <name>. – Werner Jan 16 at 2:35
  • @Werner Yes! Correct – jed Jan 16 at 2:46
  • 1
    What about this? – Werner Jan 16 at 3:03
1

The following doesn't use LaTeX3, but works the way you want it, both using xstring and \pdfstrcmp:

enter image description here

\documentclass{article}

\usepackage{xstring}

\newcommand{\Client}[1]{\renewcommand{\Client}{#1}}

\makeatletter
\newcommand\foo[1]{%
  \ifnum\pdfstrcmp{#1}{Adam}=0 %
     \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {Adam}
  {Eve}%
}
\makeatother

\begin{document}

\Client{Adam}

% Attempt using xstring
My client should be Adam: \IfSubStr{Adam}{\Client}{Adam}{Eve}

% Attempt using \pdfstrcmp
My client should be Adam: \foo{\Client}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.