# LuaTeX example invalid (printing)

On page 127 of the luatex manual it has the example

before\directlua{tex.sprint("\\relax")tex.sprint(" inbetween")}after


but the double slash throws an error. Similarly a single slash also has the same problem.

Obviously tex or luatex is is not treating the \\ as I expect. (I assume tex is trying to parse it as a macro.)

So what is the else is needed to get the sample to run?

-
The luatex manual is written in ConTeXt, and \\  is unexpandable macro (or a protected macro in LaTeX's parlance). So, the above example works in ConTeXt but not in LaTeX or plain tex. Add a \noexpand before \\  to get it to work in LaTeX. –  Aditya Mar 17 '12 at 23:56
ok, that explains a lot. –  Uiy Mar 18 '12 at 0:49

This is only there fore demonstration purpose. The reference manual is written with no specific format / macro package in mind. Therefore it doesn't know if \\ is defined or not. You can emulate a "pristine" behavior by putting the code in the luacode* environment from the luacode package:

\documentclass{article}
\usepackage{luacode}
\begin{document}
before\begin{luacode*}
tex.sprint("\\relax")tex.sprint(" inbetween")
\end{luacode*}
after
\end{document}


But (I keep on about it) you should rather put all Lua code in a separate file. Then you don't get into trouble.

-
OT, but in ConTeXt, I have not had any difficulties mixing TeX and Lua code in a single file. Can you clarify why the situation in LaTeX is different or if separating Lua and TeX code is desirable in ConTeXt as well. –  Aditya Mar 19 '12 at 3:03
@Aditya with the environment luacode or luacode* things are mostly harmless. I think that one of these environments (probably luacode) behaves like ConTeXt's environment. So the advise is not really necessary when you use this environment. But I see a lot of people trying to write code in \directlua{...} which breaks, because the catcodes are not changed, and thus for example a ~ expands to something that is unwanted in most cases. When you put code in a separate file, you don't need to think about it. –  topskip Mar 19 '12 at 7:55