I have a macro that helps me print out(or do anything really) source code in various ways using lua:
see: xparse verbatim with newline
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}
\obeyspaces
\detokenize
that adds spaces after control words.\obeyspaces
does not change the output?\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).