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.

The ConTeXt wiki has an article about string manipulation, however, when I copy the code directly into my tex file, it compiles this code as text and does not try to compile it. There must be some syntax to place before and after this code, but I cannot find any details about this in the documentation.

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

share|improve this question
    
Can you show us a M(non-)WE? If I'm not mistaken it's all lua code, so perhaps you forgot to put it into \directlua? –  Psirus Jan 17 '12 at 13:05
    
I did not see \directlua in the manual. I have tried placing \directlua{} around the code, but this is an "Undefined control sequence." –  Village Jan 17 '12 at 13:17
    
In ConTeXt, the \directlua primitive is wrapped up as \ctxlua. –  Joseph Wright Jan 17 '12 at 13:46
    
@Village: See wiki.contextgarden.net/Programming_in_LuaTeX for a basic overview of going back and forth between lua and tex. –  Aditya Jan 17 '12 at 14:06
add comment

2 Answers

up vote 8 down vote accepted

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
share|improve this answer
1  
The more canonical way will be to use \startluacode ... \stopluacode instead of \ctxlua{...} and context(...) instead of tex.print(...). –  Aditya Jan 17 '12 at 14:04
    
@Aditya I was not sure what the official position was on the 'best' interface for Lua in ConTeXt, so I used what I know. I'll edit my answer. –  Joseph Wright Jan 17 '12 at 14:05
    
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 '12 at 14:05
    
@Village: You need to use the luatex engine, so compile using context file.tex. –  Aditya Jan 17 '12 at 14:08
    
@Village You need ConTeXt Mark IV: context file.tex. –  Joseph Wright Jan 17 '12 at 14:09
show 2 more comments
\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
share|improve this answer
1  
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 '12 at 14:19
    
that's true in general if one has more than one TeX macro with Lua functions to define –  Herbert Jan 17 '12 at 14:57
add comment

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.