1

I want to store the result of IfSubStr (from package xstring) for using it later. So I define a command and I redefined with the result of IfSubStr. So far, so good. But now I want also to use StrCount on the result. The folllowing error shows up:

Argument of \@xs@StrCount has an extra }

I think it is related to the expansion of the arguments, but I do not know much about it, so I was not able to solve the problem. Any help? Here is a MWE:

\documentclass{article}
\usepackage{xstring}
\newcommand{\XXX}{}
\newcommand{\DoSomethingWith}[1]{String is #1.}
\begin{document}
    \renewcommand{\XXX}{\IfSubStr{AC}{A}{ax}{xbx}}  
    \DoSomethingWith{\XXX}

    \StrCount{ax}{x}\ x.

    \StrCount{\XXX}{x}\ x. % Error
\end{document}

1 Answer 1

1

You have to do it in a different way:

\documentclass{article}
\usepackage{xstring}
\newcommand{\XXX}{}
\newcommand{\DoSomethingWith}[1]{String is #1.}
\begin{document}
\IfSubStr{AC}{A}{\renewcommand{\XXX}{ax}}{\renewcommand{\XXX}{xbx}}

\DoSomethingWith{\XXX}

\StrCount{ax}{x}\ x.

\StrCount{\XXX}{x}\ x. % Error
\end{document}

enter image description here

2
  • Why does this work and the former does not? Originally, I wrote what you suggest. However, actually I have the concatenation of several IfSubStr. This forced me to redefine several commands (one additional command for each IfSubStr), and then I switched from several IfSubStr containing one renewcommand each, to one renewcommand containing several IfSubStr.
    – ASdeL
    Commented Aug 12, 2015 at 17:38
  • @ASdeL It's the usual problem: \IfSubStr doesn't produce the string directly, but the instructions to produce it.
    – egreg
    Commented Aug 12, 2015 at 17:57

You must log in to answer this question.

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