4

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}
6
  • Can you post your complete source? I can't reproduce any difference between LuaTeX and PDFTeX wrt \obeyspaces
    – topskip
    Sep 9, 2012 at 6:28
  • 3
    It's \detokenize that adds spaces after control words.
    – egreg
    Sep 9, 2012 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? Sep 9, 2012 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, 2012 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, 2012 at 18:43

1 Answer 1

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .