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

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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

share|improve this answer
    
Thanks for your reply, I get it. –  Timothy Li May 7 '13 at 9:44

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.

share|improve this answer
    
Thanks. Maybe I am too careless when reading the programming in Lua. –  Timothy 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

 
discard

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.