0

I am trying to build a command "TEST" which uses IfSubStr from the xstring package. When using the command, I need to pass another command "ARGUMENT" which contains a figure* environment.

When compiling the below code with LaTex (MikTeX), I get the following error:

! Argument of \reserved@a has an extra }.

If there is no figure* environment in the command ARGUMENT, there is no problem. I can't figure out how to fix this. Can anyone help me here?

\documentclass{article}
\usepackage{xstring}
\begin{document}

    \newcommand{\TEST}[1]{
        \IfSubStr{#1}{find me} {
            found it
        }{
            didn't find it
        }
    }

    \newcommand{\ARGUMENT}{
        
        \begin{figure*}
            find me
        \end{figure*}
    }

    \TEST{
        some text
        
        \ARGUMENT       
    }

\end{document}

1 Answer 1

0

By default, xstring performs full expansion of the arguments to its commands, but things such as \begin don't survive full expansion.

In my opinion, xstring should offer a “protected” full expansion mode, but as it was written to work for plain TeX, the author didn't even try it.

You can add it, though.

\documentclass{article}
\usepackage{xstring}

\makeatletter
\newcommand{\protectedIfSubStr}[5][1]{%
  \saveexpandmode\noexpandarg
  \begingroup\protected@edef\x{%
    \endgroup\noexpand\IfSubStr[#1]{#2}{#3}}\x{#4}{#5}%
  \restoreexpandmode
}
\makeatletter

\begin{document}

\newcommand{\TEST}[1]{%
  \protectedIfSubStr{#1}{find me}{%
    found it%
  }{%
    didn't find it%
  }%
}

\newcommand{\ARGUMENT}{%
  \begin{figure*}
    find me
  \end{figure*}
}

\TEST{some text\ARGUMENT}

\end{document}

enter image description here

Beware that TeX is not C or derived language: spaces, blank lines and endlines are significant.

1
  • Wonderful => works like a charm!! THANKS SO MUCH.
    – chris
    Jul 4, 2022 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .