# Conditionally use package and define command

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?

-

## 2 Answers

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 (##1) in an argument? for more information

-

Similar to answers like Expandable version of \InputIfFileExists or \IfFileExists

\documentclass{article}
\IfFileExists{eforms.sty}{\def\x{T}}{\def\x{F}}
\if T\x%
\usepackage{eforms}
\everyTextField{\textFont{GaramondNo8}\textSize{12}}
\newcommand{\dsaTextInput}[3][12]{\raisebox{-0.35ex}{\textField[\textSize{#1}]{#2}{#3}{1em}}}
\else
\newcommand{\dsaTextInput}[3]{dsaTextInput Not Defined}
\fi
\begin{document}
\x
\dsaTextInput{10pt}{12pt}{Text}
\end{document}

-