It seem that if macro \IfSubStr
from package xstring
is used as parameter it is not expanded like other macros are.
Here is minimal demonstration:
\usepackage{xstring}
\newcommand{\copyArg}[1]{#1}
\newcommand{\test}[1]{%
\if#1W
in then%
\else
in else%
\fi
}
\begin{document}
\test{\copyArg{W}}
\test{\IfSubStr{WKS}{W}{W}{N}}
\end{document}
I would expect this to print in then in then
. Instead \test{\IfSubStr{WKS}{W}{W}{N}}
behaves differently to previous macro and the output is in then in else
. Why is that? How can I tweak my code or replace \IfSubStr
by another macro so that I get expected result?
Edit
Here I should explain what I need do. There is a macro like the \test
macro shown in minimal example above. This macro has optional argument that works like switch - in very similar way it is shown in example above. If optional argument is W
some actions are taken, it is unset or anything else then W
some other actions are taken. It uses the same \if
\else
condition that I have used in demo.
I am creating new macro that uses this existing macro. My macro has also first argument optional that may contain number of switches. One of them is that W
switch. This switch is to be delegated existing macro. I thought I might code like this in my macro would do the trick \theExistingMacro[\IfSubStr{#1}{W}{W}{}]{...}{...}
but it does not work. So how do I make it work?
Edit 2
To clarify here is real life example.
There is a macro like this (simplified code)
\newcommand{\addLabelAbove}[4][]{%
\if#1W%
\saveToNewBox{#2}{\vbox{\noindent#4\\*\noindent#3}}%
\else%
\saveToNewBox{#2}{\pbox{\textwidth}{\noindent#4\\*\noindent#3}}%
\fi%
}%
I am creating new macro that calls different other macros one of which is \addLabelAbove
\newcommand{\block}[6][]{
\uppercase{\IfSubStr{#1}}{A}{%then
\uppercase{\IfSubStr{#1}}{L}{%then
\doSomething{#2}{#3}{#4}{#5}{#6}
}{%else
\stepcounter{BoxCount}%
\addLabelAbove[\IfSubStr{#1}{W}{W}{}]{\theBoxCount}{#2}{#3}%
\someMoreMacrosSkiped
}%
}{%else
\someOtherUnimportantCommands
}%
}
The line \addLabelAbove[\IfSubStr{#1}{W}{W}{}]{\theBoxCount}{#2}{#3}%
intends to call macro \addLabelAbove
with switch W
if there is W
among switches given to macro \block
or with without that switch if there is no W
switch among switches given to \block
. It does not work this way and I'd like to know how to make it work.
\ifx
would compare\IfSubStr
to{
, which is surely not what's wanted. ;-)AWK
? And in this case, should the macro take theW
branch?AWK
(multiple swithes). But the existing macro that I cannot alter is only expecting one switch - that isW
. I'm not sure I understand your second question... The predefined macro (that I cannot alter) should get theW
switch if and only ifW
was one of switches given to macro that I am creating.\addLabelAbove
is definitely wrong. If you call\addLabelAbove[aa]{X}{Y}{Z}
it will follow the true branch and you'll have aW
in the output. And if you call it with an empty optional argument you'll be in big trouble, because the first token of the expansion of\saveToNewBox
will be eaten up.\addLabelAbove
command. What should the correct definition look like?