Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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
2  
@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
show 1 more comment

1 Answer

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}")}}%
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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