Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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
add comment

2 Answers 2

up vote 7 down vote accepted

Use

\documentclass{minimal}
\begin{document}
\edef\twodollars{\string$2} \twodollars
\end{document}

\string changes the category code for $ to 12 (other).

share|improve this answer
    
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
add comment

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.