Consider this plain TeX file:
\newtoks\t
\t={#}
\showthe\t
\bye
If you compile it (with tex of pdftex), then in the output you get
> ##.
l.3 \showthe\t
Why do I get two of the #s?
(The background: I want to build the body of a macro definition in a token list, and the macro takes one argument that I want to put into the token list as #1. The idea is to use \edef\macro#1{\the\t}.)
\detokenize{#}gives two##. Is that a bug in pdfTeX? – TH. Jan 04 '11 at 07:25\detokenizeis meant to work very much like a toks, in the sense that you could do the same as\detokenizeachieves by writing a toks to file and reading back with appropriate catcode changes. However,\detokenizeis expandable whereas working via a file is not. If you try the file-based route, you'll also get two#. – Joseph Wright Jan 04 '11 at 08:03\detokenizeto write a single#to the file which is where I ran across it. I forget the exact reason now. Still, I'd assume that a#_6token would be replaced by a single#_12token. E.g., I'd expect\scantokens\expandafter{\detokenize{\def\foo#1{#1}}}to work. – TH. Jan 04 '11 at 08:21\show. But why doesn't\edef\macro#1{\the\t}work? (I knew about the\expandafterworkaround, but it's nasty, and I'm puzzled why this does work then.) – Hendrik Vogt Jan 04 '11 at 08:55