7

The follow code is trying simply to print the first argument of a macro. I've tried various things found on the net(there is very little) to get #1 printed in lua but nothing works. I assume there is an issue with interpreting it in lua. Would be nice if there was some tutorial on bridging the gap between tex and lua. Most stuff I've seen use external lua files or do not reference anything except macros in a directlua block.

\documentclass[11pt]{book} % use larger type; default would be 10pt

\usepackage{luatex}
\usepackage{pgffor}

\tracingonline 6

\directlua{
    tex.enableprimitives('',tex.extraprimitives())
    local lpeg = require "lpeg"
}

\begin{document}

\def\Parse#1{

    \directlua{
        cmdString = tostring(#1)

        tex.print(cmdString)
    }
}

\Parse{hello12341234asdf}


\end{document}

2 Answers 2

10

You shouldn't just specify #1 as the argument of tostring; rather, try something like "\luatexluaescapestring{#1}". With this modificaton, your MWE works fine:

\documentclass[11pt]{book}
\usepackage{luatex,pgffor}
\tracingonline 6
\directlua{
    tex.enableprimitives('',tex.extraprimitives())
    local lpeg = require "lpeg"
}
\def\Parse#1{
    \directlua{
        cmdString = tostring("\luatexluaescapestring{#1}")
        tex.print(cmdString)
    }
}
\begin{document}
\Parse{hello12341234asdf}
\end{document}

Update, May 2016: Alain Matthes has pointed out in a comment that the code shown above no longer runs, at least not with TeXLive2015. The problem turns out to be related to the version of the luatex package that's installed on one's system. Happily, earlier this month Heiko Oberdiek released an update to the luatex package which fixes this issue.

As TeXLive2015 is currently "frozen", one needs to either download the updated package from the CTAN and install it manually, switch to the latest release of TeXLive2016-pre, or wait a few more weeks until the official version of TeXLive2016 is released and may be installed.

Alternatively, load the luacode package instead of the luatex package.

5
  • I get an error with this code and texlive 2015 Latex Error: /usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/luatex.sty:373 LaTeX Error: Command \newattribute already defined. May 16, 2016 at 16:48
  • @AlainMatthes - I'll post an update, which uses the luacode package instead of the luatex package. Incidentally, I noticed that Heiko Oberdiek posted an update to the luatex package about a week ago. I haven't had a chance yet to check it out in detail, but it may offer a fix that (re)enables the correct functioning of the code I gave originally.
    – Mico
    May 17, 2016 at 19:45
  • @AlainMatthes - I've just checked out the latest version of luatex.sty ("luatex 2016/05/10 v0.5"). Good news: The error you've identified no longer crops up with the updated version of this package.
    – Mico
    May 17, 2016 at 22:02
  • Thanks for the update, I try to learn lua with some examples so it is very interesting May 17, 2016 at 22:07
  • @AlainMatthes - You're welcome. Incidentally, if the luacode package is used, it's possible to write cmdString = \luastring{#1}, which is rather more succinct than cmdString = tostring("\luatexluaescapestring{#1}").
    – Mico
    May 17, 2016 at 22:50
11

Mico has already given the correct answer. Let me extend the explanation. When you write:

\def\Parse#1{\directlua{   cmdString = tostring(#1)  }}

and call \Parse{hello12341234asdf}, the result is pretty much like this:

\directlua{   cmdString = tostring(hello12341234asdf)  }

Which is, of course, not what you want. You need to put the quotation marks around the definition:

\def\Parse#1{\directlua{   cmdString = tostring("#1")  }}

and a call to \Parse{hello12341234asdf}, gives

\directlua{   cmdString = tostring("hello12341234asdf")  }

Now what happens if you call \Parse{hello"1234}? You get

\directlua{   cmdString = tostring("hello"1234")  }

which, again, is not what you want. Therefore you need to escape the contents of #1. This is what \luaescapestring{} does for you. But this won't work in LaTeX, because in LaTeX every Lua command from the reference manual is prefixed with luatex (except for \directlua). Therefore the defintion is that what Mico writes:

\def\Parse#1{\directlua{   cmdString = tostring("\luatexluaescapestring{#1}")  }}

You must log in to answer this question.

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