2

The string manipulation wiki page has an example that does not compile successfully. There must be some syntax to place before and after this code, but I cannot find any details about this in the documentation. The snippet is:

\starttext
    str = "Luxury Yacht"

    rep = {
        [1] = { "Luxury", "Throatwobbler"   },
        [2] = { "Yacht",  "Mangrove"        },
    }

    print("My name is spelled “" .. str .. "”, but it's pronounced “" .. lpeg.replacer(rep):match(str) .. "”.")

\stoptext
  • How can I use this feature in my document?
  • Can I place all of the text from my document inside print() to perform some changes to all of the text? E.g., to replace all occurrences of "apple" with "fruit" in the document?

I am particularly interested in using the lpeg.replacer(table) feature.

4

2 Answers 2

8

Two things wrong here. First, you need to 'escape to Lua', which could be done using the \directlua primitive but for which there are 'higher level' wrappers: \ctxlua{...} or \startluacode ... \stopluacode . Secondly, you need to 'print' your output to TeX, which can be done using tex.print(...) or context(...); the latter also calls string.format, so you can use printf style arguments.

\starttext
  \startluacode
    local str = "Luxury Yacht"

    local rep = {
        [1] = { "Luxury", "Throatwobbler"   },
        [2] = { "Yacht",  "Mangrove"        },
    }
    context("My name is spelled “%s”, but it's pronounced “%s”.", str, lpeg.replacer(rep):match(str))
  \stopluacode

\stoptext
4
  • Does it need any special instructions to compile? With texexec file.tex it does not create a file, but I could not find any errors.
    – Village
    Jan 17, 2012 at 14:05
  • @Village You need ConTeXt Mark IV: context file.tex.
    – Joseph Wright
    Jan 17, 2012 at 14:09
  • @JosephWright: I edited the answer to use context(...) and printf style arguments, which I think are easier to read than string concatenation.
    – Aditya
    Jan 17, 2012 at 14:16
  • @Aditya I know the basics for ConTeXt and Lua, so am very happy to have things improved. I assume that the context() function is ConTeXt-only. [What is 'easier to read' depends on whether you are used to a printf-style approach :-)]
    – Joseph Wright
    Jan 17, 2012 at 17:31
1
\starttext
  \def\LuaTest#1#2{\directlua{
    str = "#1 #2"
    rep = {
        [1] = { "Luxury", "Throatwobbler"   },
        [2] = { "Yacht",  "Mangrove"        },
    }
    tex.print("My name is spelled “" .. str .. "”, but it's pronounced “" .. lpeg.replacer(rep):match(str) .. "”.")}}

\LuaTest{Luxury}{Yacht}
\stoptext
2
  • 2
    If you want to define a TeX macro, the usual practice is to define a lua function, say userdata.annouce_name(str) and then use \def\LuaTest#1#2{\ctxlua{userdata.annouce_name("#1 #2")}
    – Aditya
    Jan 17, 2012 at 14:19
  • that's true in general if one has more than one TeX macro with Lua functions to define
    – user2478
    Jan 17, 2012 at 14:57

You must log in to answer this question.

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