# Unmodified Source Code

I have a macro that helps me print out(or do anything really) source code in various ways using lua:

The macro is

\def\SourceCode{%
\begingroup%
\endlinechar\^^J%
\catcode\\=12\catcode\^^M=12\catcode\#=12\catcode\~=12\catcode\%=12\catcode\^=12\catcode\_=12\catcode\@=12\catcode\ =12\catcode\|=12%
\SourceCodeAux}%
\def\SourceCodeAux#1#2{\endgroup\directlua{print("\luaescapestring{#1}")}}%


In this case we just print the code to the console to view if it is correct.

The issue is that \detokenize adds a space after macro's. Is there any way to stop this behavior or reverse it? (I'm thinking if we could use a different, non-visible or not used in the source code block, escape character then maybe it will be possible?)

\documentclass{minimal}
\usepackage{luatex}\directlua{tex.enableprimitives('',tex.extraprimitives())}

\def\SourceCode{%
\begingroup%
\endlinechar\^^J%
\catcode\\=12\catcode\^^M=12\catcode\#=12\catcode\~=12\catcode\%=12\catcode\^=12\catcode\_=12\catcode\@=12\catcode\ =12\catcode\|=12%
\SourceCodeAux}%
\def\SourceCodeAux#1#2{\endgroup\directlua{print("\luaescapestring{#1}")}}%

\begin{document}

\SourceCode{
\documentclass{minimal}
\def\SourceCode{%
\begingroup%
\obeyspaces%
\endlinechar\^^J%
\catcode\^^M=13%
\SourceCodeAux}%
\def\SourceCodeAux#1#2{\directlua{print("\luaescapestring\expandafter{\detokenize{#1}}")}\endgroup}%
}
\end{document}

-
Can you post your complete source? I can't reproduce any difference between LuaTeX and PDFTeX wrt \obeyspaces –  topskip Sep 9 '12 at 6:28
It's \detokenize that adds spaces after control words. –  egreg Sep 9 '12 at 10:35
@Mr.Gundla That essentially is the complete source. Add documentclass and wrap it with begin{document} and end{document}. I'm using lualatex. Are you saying that commenting out \obeyspaces does not change the output? –  AbstractDissonance Sep 9 '12 at 17:31
@AbstractDissonance Try \edef\x{\detokenize{\abc\xyz}}\show\x and you'll see spaces after \abc and \xyz. It's part of the workings of \detokenize (and of \write also). –  egreg Sep 9 '12 at 17:48
@AbstractDissonance It might help to have a complete code to play with. It makes it easier to get started, but there is of course no promise that I can help :) –  topskip Sep 9 '12 at 18:43

By using \catcode'\\=12 and removing the \detokenizer seems to effectively detokenize the code block:
\def\SourceCode{%
\endlinechar\^^J%
\catcode\\=12\catcode\^^M=12\catcode\#=12\catcode\~=12\catcode\%=12\catcode\^=12\catcode\_=12\catcode\@=12\catcode\ =12\catcode\|=12%
`