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.

I have never tried to include Lua code in a document before so please be gentle. I am after a pattern matching expression that will eventually take (stuff1)->(stuff2) and replace it with !(stuff1)||(stuff2) but baby steps first...

At the bottom of this page: Programming In Lua is a mention of a pattern for matching balanced strings. After modifying some code I found on site I decided to try the following:

\documentclass{article}

\def\swap#1{%
    \directlua{%
      local s, _ = string.gsub("#1","%b()%b()","%2%1")
      tex.sprint(s)}
}

\begin{document}

\swap{(a)(b)}

\end{document}

Which does not work at all. Can anyone explain what I've done wrong?

share|improve this question

1 Answer 1

up vote 9 down vote accepted

The pattern %b()%b() does not contain captures, therefore %1 and %2 are not assigned. You need an additional pair of parentheses

(%b())(%b())

The next problem is that the percent character is a comment character in TeX usually. There are lots of ways to deal with this, the example below defines the string with the percent chars separately in a group where the catcode of the percent char is changed to the catcode of symbols like digits. See also package luacode.

Using "#1" is risky, #1 might contain stuff that breaks the syntax of the string. Therefore I have added \luatexluaescapestring (the name in LuaLaTeX for \luaescapestring).

\documentclass{article}

\begingroup
  \catcode`\%=12\relax
  \gdef\swapgsubargs{"(%b())(%b())","%2%1"}
\endgroup
\def\swap#1{%
  \directlua{%
    local s, _ = string.gsub("\luatexluaescapestring{#1}",\swapgsubargs)
    tex.sprint(s)%
  }%
}

\begin{document}

\swap{(a)(b)}

\end{document}

Result

share|improve this answer
    
Thank you kindly, that helps to clear up a number of misunderstandings I had (although I'm sure that there are many more). I'll have a look at the luacode package as well. –  Scott H. Sep 15 '12 at 0:49
    
It is worth noting that \string\% is an easier way to get a percent character in a Lua string (it will be escaped by a backslash), so \swapgsubargs could be replaced by "(\string\%b())(\string\%b())","\string\%2\string\%1", avoiding any catcode change. –  Bruno Le Floch Sep 3 at 12:00
    
@BrunoLeFloch \string\% depends on the correct setting of \escapechar. Another alternative is LaTeX's \@percentchar. –  Heiko Oberdiek Sep 3 at 16:17
    
@HeikoOberdiek: You are right. –  Bruno Le Floch Sep 4 at 12:22

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.