12

The answer is probably simple but I can't find anything in the documentation

For example:

\begin{luacode}
function divmod(a,b)
    local q = 0
    local r = a
    while not (r < b) do
        q = q+1
        r = r-b
    end
    return q,r
end
\end{luacode}

I compile of course with lualatex and I want to assign for example the result qand r of divmod(\c,\d) with \def\c{45} and def\d{7} to two macros \x and \y. How do I do this? I know how to do it with one value but not two

\documentclass{article} 
\usepackage{luacode}

\begin{luacode}

function divmod(a,b)
    local q = 0
    local r = a
    while not (r < b) do
        q = q+1
        r = r-b
    end
    return q,r
end

\end{luacode}


\begin{document} 
\def\c{45} \def\d{7}
???
\def\x{..?..} \def\y{..?..}
\end{document}

3 Answers 3

17

enter image description here

\documentclass{article} 
\usepackage{luacode}

\begin{luacode}

function divmod(a,b)
    local q = 0
    local r = a
    while not (r < b) do
        q = q+1
        r = r-b
    end
    return q,r
end

\end{luacode}
\def\x{}\def\y{}

\begin{document} 
\def\c{45} \def\d{7}

\directlua{
q,r=divmod(\c,\d)
token.set_macro("x",q)
token.set_macro("y", r)
}

x is \x, y is \y
\end{document}
3
  • 1
    Thanks. I just discovered token.set_macro. Where is this defined? Jan 19, 2023 at 19:44
  • 1
    I only found set_macro in the LuaTeX manual Jan 19, 2023 at 19:58
  • 5
    @AlainMatthes it is in the section describing the token library, the luatex manual isn't always explicit but it avoids prefixing the names, but they won't work unprefixed by default Jan 19, 2023 at 20:14
11

The \directlua chunk returns the results in the format {q}{r}, so the macro \divmodA can read it as two parameters.

\directlua{
function divmod(a,b)
    local q = 0
    local r = a
    while not (r < b) do
        q = q+1
        r = r-b
    end
    return q,r
end
}
\def\divmod#1#2{%
   \expandafter\divmodA\directlua{q,r=divmod(#1,#2) tex.sprint("{",q,"}{",r,"}")}%
}
\def\divmodA#1#2{\def\x{#1}\def\y{#2}}

\divmod{45}{7}
x is \x, y is \y.

\bye
4
  • Interesting answer,thanks ! I didn't know that the \directlua chunk returns the results in the format {q}{r} Jan 19, 2023 at 22:53
  • @AlainMatthes wipet means this function returns a brace pair due to the tex.sprint not that that is a general feature Jan 20, 2023 at 8:24
  • @DavidCarlisle I misspoke and maybe wipet did too. I understood that the format was due to tex.sprint("{",q,"}{",r,"}")}but that is only valid if the author decides and if it is feasible! Did I understand correctly? Jan 20, 2023 at 12:35
  • @AlainMatthes yes, the advantage of wipet's suggestion is it passes both values back from Lua to tex without hard coding tex macro names in the Lua side, it just returns {6}{3} so then you are free to do whatever you want on the tex side, perhaps define two macros, or perhaps use directly as argument to some two-argument macro, or whatever. Which reminds me, I should vote +1 here... done:-) Jan 20, 2023 at 12:40
7

If the results of divmod are always to be stored in \q and \r, the following solution may be of interest.

enter image description here

\documentclass{article} 
\usepackage{luacode}

\begin{luacode}

function divmod ( a , b )
    local q = 0
    local r = a
    while not (r < b) do
        q = q+1
        r = r-b
    end
    tex.sprint("\\def\\q{"..q.."}")
    tex.sprint("\\def\\r{"..r.."}")
end

\end{luacode}

% LaTeX utility macro:
\newcommand\divmod[2]{\directlua{divmod(#1,#2)}}

\begin{document} 
\divmod{45}{7}
\q, \r

\def\c{100} \def\d{11}
\directlua{divmod(\c,\d)}
\q, \r
\end{document}

You must log in to answer this question.

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