josephwright / etoolbox

Tool-box for LaTeX programmers using e-TeX
LaTeX Project Public License v1.3c
41 stars 7 forks source link

Use `\ifblank` as a test for `\ifstrequal` #44

Closed Bigaluca closed 1 year ago

Bigaluca commented 1 year ago

To carry out checks on some parameters passed to an environment, I would need to:

  1. check if the parameter is blank or not;
  2. pass a default value or the one supplied;
  3. compare the final value with a string and then decide what to do.

Without going into the details of the context, I think the situation can be boiled down to this MWE:

\usepackage{etoolbox}

\newcommand{\foo}[1]{
    \expandafter\ifstrequal\expandafter{#1}{pluto}
        {a}
        {b}%
}

\newcommand{\foofoo}[1][]{\ifblank{#1}{pluto}{#1}}

\begin{document}

\foo{\foofoo}

\foo{\foofoo[]}

\foo{\foofoo[ ]}

\foo{\foofoo[pluto]}

\foo{\foofoo[pippo]}

\end{document}

Running those commands I would expect to get "a" in all but the last case. Instead, "b" is always printed.

What am I doing wrong? Is there a way to get the result I want?

josephwright commented 1 year ago

\expandafter only expands one step, which is rarely enough to produce a result for a an arbitrary command. I strongly suggest you look here at \NewDocumentCommand in the current LaTeX kernel, which has a mechanism to determine if an optional argument is given or not, independent of any value.

Bigaluca commented 1 year ago

Sorry but I can't quite understand what you mean... Could you give me an example of what you mean based on the code I wrote?

josephwright commented 1 year ago

Please post a question at https://tex.stackexchange.com/ about this - this is not etoolbox specific