10
\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?

2 Answers 2

9

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

0
9

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.

2
  • Thanks. Maybe I am too careless when reading the programming in Lua.
    – yanpengl
    May 7, 2013 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, 2013 at 9:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .