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}
  • 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
  • 3
    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
  • 3
    @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
up vote 0 down vote accepted

By using \catcode'\\=12 and removing the \detokenizer seems to effectively detokenize the code block:

\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}")}}%

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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