local s = [[\luaescapestring{\TeX}]]

After \luaescapestring{\TeX}, I think I should get

T\\kern -.1667em\\lower .5ex\\hbox {E}\\kern -.125emX

then this line will be converted into a string in Lua by using [[ ]] operation(at the same time, \\ will be \), thus,

s = "T\kern -.1667em\lower .5ex\hbox {E}\kern -.125emX"

but the result printed is

T\\kern -.1667em\\lower .5ex\\hbox {E}\\kern -.125emX



compare the different output:


  local s = [[\luaescapestring{\TeX}]]

  local s = "\luaescapestring{\TeX}"


T\\kern -.1667em\\lower .5ex\\hbox {E}\\kern -.125emX\\spacefactor \\@m 
T\kern -.1667em\lower .5ex\hbox {E}\kern -.125emX\spacefactor \@m 

With [[ ... ]] the backslash has no meaning and is escaped


This is because [[...]] is a "safe" string, which is not what \luaescapestring is made for. This adds two backslashes to a string instead of one.

This is with regular Lua and is nothing special with LuaTeX:

a = [[foo \\ bar]]

b = "foo \\ bar"


foo \\ bar
foo \ bar

\luaescapestring{} replaces each backslash with two backslashes, regardless of the type of string you are defining (double bracket or double quote), because it doesn't know about it. That way you get two backslashes instead of one in the first string.

  • Thanks. Maybe I am too careless when reading the programming in Lua. – Lucas Li May 7 '13 at 9:43
  • @TimothyLi I always skip these details when leaning something, because I don't understand its implications. Only when I stumble upon issues like the one you mention, I re-read the documentation. – topskip May 7 '13 at 9:54

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.