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.

Can someone explain what happens here:




The result is xxyy%b zz%c. (The xx,yy,zz is clear.) The \%a is "ignored", the \\%b and the \\\%c results in the "same" %b (or c). What is the preferred way to get a % in the PDF output? Why is the \%a ignored? What does TeX see after returning from the lua call?

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

I assume that luacode in LaTeX works the same way as \startluacode...\stopluacode in ConTeXt. luacode expands its contents before passing control to lua. So,

  • xx\%a => xx%a (because \% = %)
  • yy\\%b => yy\%b (because \\ = \)
  • zz\\\%c => zz\%c (because \\ = \ and \% = %)

Thus, (as Paŭlo Ebermann) mentioned, lua writes the following to TeX input stream:


which explains the output that you get. Perhaps the following test file will make this clear (Compare the console output from pdf output).

share|improve this answer
Can you please explain in which sense one has \\ = \? (I just don't see what kind of expansion you mean.) –  Hendrik Vogt Feb 25 '11 at 8:22
I don't know how luacode is implemented in LaTeX. But in ConTeXt, there is \appendtoks \let\\\lualetterbackslash .... \to \evveryluacode where \edef\lualetterbackslash{\string\\} –  Aditya Feb 25 '11 at 16:41
Just found your comment, thanks. However, I'm not sure if I understand it. When you write "luacode expands its contents" in your answer, you mean "looks for backslash escaping" rather than "macro expansion"? (Can you please use "@Hendrik" if you have an answer?) –  Hendrik Vogt Feb 28 '11 at 16:39
@Hendrik Vogt: No, I mean macro expansion. In ConTeXt, \`=\lualetterbackslash`. If you want to change what \` expands to, you can redefine \luatetterbackslash`. Once all the content is expanded, it is processed by lua, which then looks for backslash escaping. Since the example writes material to the TeX stream, TeX then looks for macro expansion again. I know that this is a bit confusing. I find it easy to think that \startluacode...\stopluacode` write it contents to a file that is then read by lua. Although this is not strictly true, it helps in understanding what is happening. –  Aditya Feb 28 '11 at 17:15
Thanks for the explanation, it's quite a bit clearer now. –  Hendrik Vogt Feb 28 '11 at 17:19
add comment

I think the single \ serves to escape the next letter in Lua, letting itself dissappear. So, TeX gets to see these three lines:


And obviously the first one causes the %a (+ the following line-break) to be ignored, while the second and third write out a %.

It is a bit annoying that all (most) programming languages use the same \ as an escape character, so when embedding one in another you need multiple-escaping.

share|improve this answer
add comment

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.