I am trying to use xstring's \IfStrEqCase to switch between cases. It seems to work fine except for the case where the parameter for the switch is a macro with an optional parameter:
\MySwitch{$\MyMacro[optional]{a - b}$}
The problematic line is commented out in the MWE. Once this works as desired, the last line in the output should be duplicated:
The code within the \MySwitch is used often for different cases.
Code:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xstring}
\usepackage{xparse}
\NewDocumentCommand{\MyMacro}{o m}{\IfNoValueTF{#1}{#2}{\text{#1: } #2}}%
\newcommand*{\MySwitch}[1]{%
\IfStrEqCase{#1}{%
{plain text}{text: #1}%
{$x^2$}{math a: #1}%
{$\MyMacro{a - b}$}{math b: #1}%
{$\MyMacro[optional]{a - b}$}{math c: #1}%
}[Error: Invalid input: #1]%
}
\begin{document}
\MySwitch{plain text}
\MySwitch{$x^2$}
\MySwitch{$\MyMacro{a - b}$}
math c: $\MyMacro[optional]{a - b}$%% <-- Following should produce this output
%\MySwitch{$\MyMacro[optional]{a - b}$}% <--- This is the problem
\end{document}
