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?


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).

  • Can you please explain in which sense one has \\ = \? (I just don't see what kind of expansion you mean.) 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?) 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. Feb 28 '11 at 17:19

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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