2

I have some limitation in my understanding of how to deal with tex and lua variable.

I would like to pass the result of a Tex command in order to do some computation on it. Inside the lua function, the variable is \case{2}, when I try to do and tex.sprint on it in my lualatex file, it is 4. I'm looking for a way to have 4 inside my lua function in order to make some computation on it.

This is my minimal example.

% !TeX encoding = utf-8
% !TeX program = LuaLaTeX
\documentclass{article}

\usepackage{luacode}
\usepackage{xstring}

\begin{luacode}

  compute = function(argone, argtwo)
    tex.sprint(argone, argtow)
    print("arg1", argone, "arg2", argtwo)
    return argtwo
  end

\end{luacode}

\newcommand\case[1]{\IfEqCase{#1}{%
  {1}{2}%
  {2}{4}%
  {3}{6}%
  {4}{8}%
  }}
\newcommand\computefn[2]{%
  \directlua{
    compute(#1, \luastringN{#2})
}}
    % -- First compilation
    %compute(#1, #2)%
    % -- Résult ! Argument of \xs_IfStringCase_ii has an extra \}.
    % --<inserted text> 
    % -- \\par 
    % -- Second compilation
    % -- Result : arg1  2   arg2    \case {2}
    % -- I want to have the resulte of \case{2} in arg2
\newcommand\displayvals[1]{%
    Value = #1

    Case\#1 = \case{#1}

    Function = \computefn{#1}{\case{#1}}
  }

\begin{document}
\case{3}

\displayvals{2}
\end{document}

2
  • 2
    xstring commands are not expandable so make this harder than it need be, are you committed to xstring? If you defined \case so \case{1} expanded to 2, it would just naturally pass 2 to Lua. Jul 27, 2022 at 8:58
  • By the way luacode package has some functionalities to allow debugging what Lua code gets executed exactly which might help to debug these things
    – user202729
    Jul 27, 2022 at 13:19

1 Answer 1

0

The expansion (=replacement text) passes to lua, as the comment attests.

function

A function in a programming language would have a return value (like in lua); in a macro language (like TeX or SAS macro), the "output" of a macro is replacement text (more code for the interpreter) and so is not typed. So macro names could be somewhat misleading in what they comport, in some contexts.

MWE

\documentclass{article}

\usepackage{luacode}
%\usepackage{xstring}

\begin{luacode}

  compute = function(argone, argtwo, argthree)
    tex.sprint("arg1=", argone, "; arg2=", argtwo, "; arg3=", argthree)
    print("arg1=", argone, "; arg2=", argtwo, "; arg3=", argthree)
    return argthree
  end

\end{luacode}

\newcommand\case[1]{%
  \ifcase#1\or2\or4\or6\or8\fi}
  
\newcommand\computefn[3]{%
  \directlua{
    compute(#1, "#2", #3)
}}

\newcommand\displayvals[1]{%
    Value = #1

    Case\#1 = \case{#1}

    ``Function'' : \computefn{#1}{\\textbackslash case\\{#1\\}}{\case{#1}}
  }

\begin{document}
[case3=6]:\par
 \case{3}

[displayvals2=2,4,2,(command),4]:\par
 \displayvals{2}
 
[displayvals4=4,8,4,(command),8]:\par
 \displayvals{4}
\end{document}


Added

Doing a \show\IfEqCase shows that it is replaced by:

> \IfEqCase=macro:
->\xs_ifstar {\xs_IfStringCase {\IfEq *}}{\xs_IfStringCase \IfEq }

And similarly for the other macros.

What you pass to lua, and how and when, is going to depend on your actual use-case. (lua could take the inputs and do the "function", for example - there will be permutations)

You must log in to answer this question.

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