[Tex/LaTex] Conditionally use package and define command

conditionalsmacrospackages

I'm creating a class which should define a command if some package is available:

\IfFileExists{eforms.sty}{
   \usepackage[xetex]{eforms}
   \everyTextField{\textFont{GaramondNo8}\textSize{12}}
   \newcommand{\dsaTextInput}[3][12]{\raisebox{-0.35ex}{\textField[\textSize{#1}]{#2}{#3}{1em}}}
}{}

However, if I try to compile this, I get:

! Illegal parameter number in definition of \reserved@a.
<to be read again>
                   1
l.7 }{}

l.7 is the last line of the code I've shown. If I remove the conditional, it works as expected. What am I doing wrong?

Best Answer

You just need to double the # symbols:

\IfFileExists{eforms.sty}{
   \usepackage[xetex]{eforms}
   \everyTextField{\textFont{GaramondNo8}\textSize{12}}
   \newcommand{\dsaTextInput}[3][12]{\raisebox{-0.35ex}{\textField[\textSize{##1}]{##2}{##3}{1em}}}
}{}

Why the doubling? Because the "true" and "false" branches are passed to \def\reserved@a{<true>} and \def\reserved@b{<false>}, so the usual rule applies.

See What is the meaning of double pound symbol (number sign, hash character) ##1 in an argument? for more information

Related Question