Skip to main content
3 of 4
added 2 characters in body
Ulrich Diez
  • 32.7k
  • 2
  • 34
  • 63

Seems making commands robust by defining via \NewDocumentCommand instead of \newcommand does the trick.

\documentclass{article} %\usepackage{xparse} \usepackage{xstring} \NewDocumentCommand{\Prob}{m}{% \IfEqCase{#1}{% {1}{p}% {2}{q}% {3}{r}% }[{#1}]% }% \NewDocumentCommand{\RandomVar}{m}{% \IfEqCase{#1}{% {1}{X}% {2}{Y}% {3}{Z}% }[{#1}]% }% \begin{document} $\Prob1$, $\Prob2$, $\Prob{p_{X,Y}}$, $p_{\RandomVar1, \RandomVar2}$ $\Prob{p_{\RandomVar1,\RandomVar2}}$ \end{document} 

The last line yields p_{\RandomVar1,\RandomVar2} which in turn yields p_{X,Y}.

enter image description here


A better approach might be using expandable \str_case:nnTF:

\documentclass{article} \ExplSyntaxOn \cs_new:Npn \Prob #1 {% \exp_args:Ne \str_case:nnTF {#1} { {1} {p} {2} {q} {3} {r} } {} {#1} } \cs_new:Npn \RandomVar #1 {% \exp_args:Ne \str_case:nnTF {#1} { {1} {X} {2} {Y} {3} {Z} } {} {#1} } \ExplSyntaxOff \begin{document} $\Prob1$, $\Prob2$, $\Prob{p_{X,Y}}$, $p_{\RandomVar1, \RandomVar2}$ $\Prob{p_{\RandomVar1,\RandomVar2}}$ \end{document} 

enter image description here

Ulrich Diez
  • 32.7k
  • 2
  • 34
  • 63