# The strange \luaescapestring in LuaTeX

\directlua{%
local s = [[\luaescapestring{\TeX}]]
print(s)}


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


why?

compare the different output:

\documentclass{article}
\directlua{tex.enableprimitives("",tex.extraprimitives())}
\begin{document}

\directlua{%
local s = [[\luaescapestring{\TeX}]]
print(s)}

\directlua{%
local s = "\luaescapestring{\TeX}"
print(s)}

\end{document}

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]]
print(a)

b = "foo \\ bar"
print(b)


prints

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