Dollar signs in an edef yield tex capacity exceeded

I want my two dollars:

\documentclass{minimal}
\begin{document}
\edef\twodollars{\$2} \end{document}  However, all I get is: ! TeX capacity exceeded, sorry [input stack size=5000]. \font@name -> \OT1/cmr/m/n/10 l.3 \edef\twodollars{\$
2}


Is it possible to use \edef with a value that includes a dollar sign?

-
Why not just use \def\twodollars{\$2}? – Werner Nov 30 '11 at 5:07 @Werner: in the application the "2" is a macro \twoOrThree, and I want to use the value \twoOrThree has at the time of \twodollars definition, rather than the value at a later expansion of \twodollars. – Jack Schmidt Nov 30 '11 at 5:10 Also, the dollar sign just has to look like a dollar sign when \twodollars is expanded in plain horizontal mode text. I didn't see a "\textdollarsign" like for underscore. – Jack Schmidt Nov 30 '11 at 5:11 2 Answers Use \documentclass{minimal} \begin{document} \edef\twodollars{\string$2} \twodollars
\end{document}


\string changes the category code for $ to 12 (other). - Works like a charm, even in the convoluted mess of my real application. Thanks! – Jack Schmidt Nov 30 '11 at 5:19 @JackSchmidt: If you need this more often, consider defining a macro for the dollar. For example, the newverbs package allows for \Verbdef\dollar|$|, which also works for other special characters including %, where \string does not work. –  Martin Scharrer Dec 1 '11 at 8:59

The correct way is to say

\makeatletter
\protected@edef\twodollars{\$2} \makeatother  as \$ is defined via \DeclareRobustCommand. An alternative is to say

\usepackage{etoolbox}
\robustify\$\edef\twodollars{\$2}

-