TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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
up vote 4 down vote accepted

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:

tex.sprint("\\relax")tex.sprint(" inbetween")

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

share|improve this answer
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

Your Answer


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.