Arithmetics in LuaLaTeXHow can I list fonts available to LuaTeX in ConTeXt (TeX Live 2013)?LuaLaTeX for dummies: basic directlua useHelp integrating some LUA code into a Luatex document?LuaLaTeX: compilation fails when sorting text using luacodeLuaLaTeX — Attempt to index global 'luatexbase'LuaLaTeX: Calculate length in LuaMy first luaLaTeX exampleLuaTeX io.lines and number of open file descriptorsHow to use lua code from external file in lualatex?Problem with miktex-2.9.6942-x64 and lualatex/luatex

Why did House of Representatives need to condemn Trumps Tweets?

Is it safe if the neutral lead is exposed and disconnected?

What is this 4 sharp symbol and what does it mean?

Copying an existing HTML page and use it, is that against any copyright law?

How many oliphaunts died in all of the Lord of the Rings battles?

What is "aligned sequences" and "consensus sequence" in the context of sequence logo? How to compute these?

How do you pronounce "Hain"?

Struggling with cyclical dependancies in unit tests

Move the outer key inward in an association

Is there an antonym(a complementary antonym) for "spicy" or "hot" regarding food (I do NOT mean "seasoned" but "hot")?

Why did some Apollo missions carry a grenade launcher?

How can I kill my goat?

Does Wolfram Mathworld make a mistake describing a discrete probability distribution with a probability density function?

Can you place a support header in the ceiling?

Why is the Apollo LEM ladder so far from the ground?

How does one get an animal off of the Altar surreptitiously?

Should I accept an invitation to give a talk from someone who might review my proposal?

What is the most efficient way to write 'for' loops in Matlab?

Why does the Eurostar not show youth pricing?

Spacing after a tikz figure

Why would anyone ever invest in a cash-only etf?

How can I say in Russian "I am not afraid to write anything"?

Why does Canada require mandatory bilingualism in a lot of federal government posts?

Do the books ever say oliphaunts aren’t elephants?



Arithmetics in LuaLaTeX


How can I list fonts available to LuaTeX in ConTeXt (TeX Live 2013)?LuaLaTeX for dummies: basic directlua useHelp integrating some LUA code into a Luatex document?LuaLaTeX: compilation fails when sorting text using luacodeLuaLaTeX — Attempt to index global 'luatexbase'LuaLaTeX: Calculate length in LuaMy first luaLaTeX exampleLuaTeX io.lines and number of open file descriptorsHow to use lua code from external file in lualatex?Problem with miktex-2.9.6942-x64 and lualatex/luatex






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















I have the following code.



documentclassarticle
usepackageluacode
beginluacode
function fact(n)
if n == 0 then
return 1
else
tex.sprint ( n * fact(n - 1) )
end
end
endluacode
newcommandfactorial[1]directluafact(#1)
begindocument
factorial5
enddocument


This throws the error that attempt to perform arithmetic on a nil value. Where is the problem?










share|improve this question
























  • Your lua function returns nothing if n!=0. So fact(n-1) is nil.

    – Alain Merigot
    Jul 18 at 11:21

















7















I have the following code.



documentclassarticle
usepackageluacode
beginluacode
function fact(n)
if n == 0 then
return 1
else
tex.sprint ( n * fact(n - 1) )
end
end
endluacode
newcommandfactorial[1]directluafact(#1)
begindocument
factorial5
enddocument


This throws the error that attempt to perform arithmetic on a nil value. Where is the problem?










share|improve this question
























  • Your lua function returns nothing if n!=0. So fact(n-1) is nil.

    – Alain Merigot
    Jul 18 at 11:21













7












7








7








I have the following code.



documentclassarticle
usepackageluacode
beginluacode
function fact(n)
if n == 0 then
return 1
else
tex.sprint ( n * fact(n - 1) )
end
end
endluacode
newcommandfactorial[1]directluafact(#1)
begindocument
factorial5
enddocument


This throws the error that attempt to perform arithmetic on a nil value. Where is the problem?










share|improve this question














I have the following code.



documentclassarticle
usepackageluacode
beginluacode
function fact(n)
if n == 0 then
return 1
else
tex.sprint ( n * fact(n - 1) )
end
end
endluacode
newcommandfactorial[1]directluafact(#1)
begindocument
factorial5
enddocument


This throws the error that attempt to perform arithmetic on a nil value. Where is the problem?







luatex lua luacode directlua






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 18 at 11:11









user61681user61681

4522 silver badges11 bronze badges




4522 silver badges11 bronze badges















  • Your lua function returns nothing if n!=0. So fact(n-1) is nil.

    – Alain Merigot
    Jul 18 at 11:21

















  • Your lua function returns nothing if n!=0. So fact(n-1) is nil.

    – Alain Merigot
    Jul 18 at 11:21
















Your lua function returns nothing if n!=0. So fact(n-1) is nil.

– Alain Merigot
Jul 18 at 11:21





Your lua function returns nothing if n!=0. So fact(n-1) is nil.

– Alain Merigot
Jul 18 at 11:21










3 Answers
3






active

oldest

votes


















8














With the code you posted, evaluating fact(5) causes the following expression to be evaluated by the Lua interpreter inside LuaTeX:



tex.sprint(5*fact(4))


which is



tex.sprint(
5*tex.sprint(
4*fact(3)))


which is



tex.sprint(
5*tex.sprint(
4*tex.sprint(
3*fact(2))))


which is



tex.sprint(
5*tex.sprint(
4*tex.sprint(
3*tex.sprint(
2*fact(1)))))


which is



tex.sprint(
5*tex.sprint(
4*tex.sprint(
3*tex.sprint(
2*tex.sprint(
1*fact(0))))))


which is



tex.sprint(
5*tex.sprint(
4*tex.sprint(
3*tex.sprint(
2*tex.sprint(
1*1)))))


The first fact() call to return a value when the Lua interpreter evaluates fact(5) is thus fact(0). According to your definition of fact(), this evaluates to the Lua integer 1. Then 1*1 is evaluated, results in the Lua integer 1, and the next recursion level thus tries to evaluate 2*tex.sprint(1). This is where your code fails, since tex.sprint(1) evaluates to nil and 2*nil isn't a valid arithmetic expression (tex.sprint() prints contents to the DVI or PDF output, but as a Lua function, returns nil). Because of this error, the recursion stops.



You can fix the problem this way:



documentclassarticle
usepackageluacode

beginluacode
function fact(n)
if n == 0 then
return 1
else
return n * fact(n - 1)
end
end

function printfact(n)
tex.sprint (fact(n))
end

endluacode

newcommand*factorial[1]directluaprintfact(#1)

begindocument
factorial5
enddocument


which produces the following output (DVI or PDF):



enter image description here



In this code, the fact() function always returns a Lua number (not nil), while printfact() writes the corresponding number to the TeX output file (DVI or PDF).






share|improve this answer


































    4














    Here's my recommendation: Move the tex.sprint directive to the definition of the factorial macro, and use return, not tex.sprint, to return numbers from the fact function.



    Note also that since there are no TeX-special characters (such as #, %, and ) in the setup of the Lua function fact, one can define it via a directlua call, as is done in the code below. No need, in this case, to load the luacode package.



    documentclassarticle
    directlua


    function fact ( n )
    if n == 0 or n == 1 then
    return 1
    else
    return ( n * fact (n - 1) )
    end
    end




    newcommandfactorial[1]directluatex.sprint(fact(#1))

    begindocument
    factorial5
    enddocument





    share|improve this answer
































      2














      documentclassarticle
      usepackageluacode
      beginluacode
      function printfact(n)
      if n == 0 then n = 1 end
      for i=2, n-1 do n=n*i end
      tex.sprint(n)
      end
      endluacode
      newcommand*factorial[1]directluaprintfact(#1)

      begindocument
      factorial5
      enddocument





      share|improve this answer



























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "85"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f500490%2farithmetics-in-lualatex%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8














        With the code you posted, evaluating fact(5) causes the following expression to be evaluated by the Lua interpreter inside LuaTeX:



        tex.sprint(5*fact(4))


        which is



        tex.sprint(
        5*tex.sprint(
        4*fact(3)))


        which is



        tex.sprint(
        5*tex.sprint(
        4*tex.sprint(
        3*fact(2))))


        which is



        tex.sprint(
        5*tex.sprint(
        4*tex.sprint(
        3*tex.sprint(
        2*fact(1)))))


        which is



        tex.sprint(
        5*tex.sprint(
        4*tex.sprint(
        3*tex.sprint(
        2*tex.sprint(
        1*fact(0))))))


        which is



        tex.sprint(
        5*tex.sprint(
        4*tex.sprint(
        3*tex.sprint(
        2*tex.sprint(
        1*1)))))


        The first fact() call to return a value when the Lua interpreter evaluates fact(5) is thus fact(0). According to your definition of fact(), this evaluates to the Lua integer 1. Then 1*1 is evaluated, results in the Lua integer 1, and the next recursion level thus tries to evaluate 2*tex.sprint(1). This is where your code fails, since tex.sprint(1) evaluates to nil and 2*nil isn't a valid arithmetic expression (tex.sprint() prints contents to the DVI or PDF output, but as a Lua function, returns nil). Because of this error, the recursion stops.



        You can fix the problem this way:



        documentclassarticle
        usepackageluacode

        beginluacode
        function fact(n)
        if n == 0 then
        return 1
        else
        return n * fact(n - 1)
        end
        end

        function printfact(n)
        tex.sprint (fact(n))
        end

        endluacode

        newcommand*factorial[1]directluaprintfact(#1)

        begindocument
        factorial5
        enddocument


        which produces the following output (DVI or PDF):



        enter image description here



        In this code, the fact() function always returns a Lua number (not nil), while printfact() writes the corresponding number to the TeX output file (DVI or PDF).






        share|improve this answer































          8














          With the code you posted, evaluating fact(5) causes the following expression to be evaluated by the Lua interpreter inside LuaTeX:



          tex.sprint(5*fact(4))


          which is



          tex.sprint(
          5*tex.sprint(
          4*fact(3)))


          which is



          tex.sprint(
          5*tex.sprint(
          4*tex.sprint(
          3*fact(2))))


          which is



          tex.sprint(
          5*tex.sprint(
          4*tex.sprint(
          3*tex.sprint(
          2*fact(1)))))


          which is



          tex.sprint(
          5*tex.sprint(
          4*tex.sprint(
          3*tex.sprint(
          2*tex.sprint(
          1*fact(0))))))


          which is



          tex.sprint(
          5*tex.sprint(
          4*tex.sprint(
          3*tex.sprint(
          2*tex.sprint(
          1*1)))))


          The first fact() call to return a value when the Lua interpreter evaluates fact(5) is thus fact(0). According to your definition of fact(), this evaluates to the Lua integer 1. Then 1*1 is evaluated, results in the Lua integer 1, and the next recursion level thus tries to evaluate 2*tex.sprint(1). This is where your code fails, since tex.sprint(1) evaluates to nil and 2*nil isn't a valid arithmetic expression (tex.sprint() prints contents to the DVI or PDF output, but as a Lua function, returns nil). Because of this error, the recursion stops.



          You can fix the problem this way:



          documentclassarticle
          usepackageluacode

          beginluacode
          function fact(n)
          if n == 0 then
          return 1
          else
          return n * fact(n - 1)
          end
          end

          function printfact(n)
          tex.sprint (fact(n))
          end

          endluacode

          newcommand*factorial[1]directluaprintfact(#1)

          begindocument
          factorial5
          enddocument


          which produces the following output (DVI or PDF):



          enter image description here



          In this code, the fact() function always returns a Lua number (not nil), while printfact() writes the corresponding number to the TeX output file (DVI or PDF).






          share|improve this answer





























            8












            8








            8







            With the code you posted, evaluating fact(5) causes the following expression to be evaluated by the Lua interpreter inside LuaTeX:



            tex.sprint(5*fact(4))


            which is



            tex.sprint(
            5*tex.sprint(
            4*fact(3)))


            which is



            tex.sprint(
            5*tex.sprint(
            4*tex.sprint(
            3*fact(2))))


            which is



            tex.sprint(
            5*tex.sprint(
            4*tex.sprint(
            3*tex.sprint(
            2*fact(1)))))


            which is



            tex.sprint(
            5*tex.sprint(
            4*tex.sprint(
            3*tex.sprint(
            2*tex.sprint(
            1*fact(0))))))


            which is



            tex.sprint(
            5*tex.sprint(
            4*tex.sprint(
            3*tex.sprint(
            2*tex.sprint(
            1*1)))))


            The first fact() call to return a value when the Lua interpreter evaluates fact(5) is thus fact(0). According to your definition of fact(), this evaluates to the Lua integer 1. Then 1*1 is evaluated, results in the Lua integer 1, and the next recursion level thus tries to evaluate 2*tex.sprint(1). This is where your code fails, since tex.sprint(1) evaluates to nil and 2*nil isn't a valid arithmetic expression (tex.sprint() prints contents to the DVI or PDF output, but as a Lua function, returns nil). Because of this error, the recursion stops.



            You can fix the problem this way:



            documentclassarticle
            usepackageluacode

            beginluacode
            function fact(n)
            if n == 0 then
            return 1
            else
            return n * fact(n - 1)
            end
            end

            function printfact(n)
            tex.sprint (fact(n))
            end

            endluacode

            newcommand*factorial[1]directluaprintfact(#1)

            begindocument
            factorial5
            enddocument


            which produces the following output (DVI or PDF):



            enter image description here



            In this code, the fact() function always returns a Lua number (not nil), while printfact() writes the corresponding number to the TeX output file (DVI or PDF).






            share|improve this answer















            With the code you posted, evaluating fact(5) causes the following expression to be evaluated by the Lua interpreter inside LuaTeX:



            tex.sprint(5*fact(4))


            which is



            tex.sprint(
            5*tex.sprint(
            4*fact(3)))


            which is



            tex.sprint(
            5*tex.sprint(
            4*tex.sprint(
            3*fact(2))))


            which is



            tex.sprint(
            5*tex.sprint(
            4*tex.sprint(
            3*tex.sprint(
            2*fact(1)))))


            which is



            tex.sprint(
            5*tex.sprint(
            4*tex.sprint(
            3*tex.sprint(
            2*tex.sprint(
            1*fact(0))))))


            which is



            tex.sprint(
            5*tex.sprint(
            4*tex.sprint(
            3*tex.sprint(
            2*tex.sprint(
            1*1)))))


            The first fact() call to return a value when the Lua interpreter evaluates fact(5) is thus fact(0). According to your definition of fact(), this evaluates to the Lua integer 1. Then 1*1 is evaluated, results in the Lua integer 1, and the next recursion level thus tries to evaluate 2*tex.sprint(1). This is where your code fails, since tex.sprint(1) evaluates to nil and 2*nil isn't a valid arithmetic expression (tex.sprint() prints contents to the DVI or PDF output, but as a Lua function, returns nil). Because of this error, the recursion stops.



            You can fix the problem this way:



            documentclassarticle
            usepackageluacode

            beginluacode
            function fact(n)
            if n == 0 then
            return 1
            else
            return n * fact(n - 1)
            end
            end

            function printfact(n)
            tex.sprint (fact(n))
            end

            endluacode

            newcommand*factorial[1]directluaprintfact(#1)

            begindocument
            factorial5
            enddocument


            which produces the following output (DVI or PDF):



            enter image description here



            In this code, the fact() function always returns a Lua number (not nil), while printfact() writes the corresponding number to the TeX output file (DVI or PDF).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 18 at 12:42

























            answered Jul 18 at 11:25









            frougonfrougon

            6,0251 gold badge10 silver badges20 bronze badges




            6,0251 gold badge10 silver badges20 bronze badges


























                4














                Here's my recommendation: Move the tex.sprint directive to the definition of the factorial macro, and use return, not tex.sprint, to return numbers from the fact function.



                Note also that since there are no TeX-special characters (such as #, %, and ) in the setup of the Lua function fact, one can define it via a directlua call, as is done in the code below. No need, in this case, to load the luacode package.



                documentclassarticle
                directlua


                function fact ( n )
                if n == 0 or n == 1 then
                return 1
                else
                return ( n * fact (n - 1) )
                end
                end




                newcommandfactorial[1]directluatex.sprint(fact(#1))

                begindocument
                factorial5
                enddocument





                share|improve this answer





























                  4














                  Here's my recommendation: Move the tex.sprint directive to the definition of the factorial macro, and use return, not tex.sprint, to return numbers from the fact function.



                  Note also that since there are no TeX-special characters (such as #, %, and ) in the setup of the Lua function fact, one can define it via a directlua call, as is done in the code below. No need, in this case, to load the luacode package.



                  documentclassarticle
                  directlua


                  function fact ( n )
                  if n == 0 or n == 1 then
                  return 1
                  else
                  return ( n * fact (n - 1) )
                  end
                  end




                  newcommandfactorial[1]directluatex.sprint(fact(#1))

                  begindocument
                  factorial5
                  enddocument





                  share|improve this answer



























                    4












                    4








                    4







                    Here's my recommendation: Move the tex.sprint directive to the definition of the factorial macro, and use return, not tex.sprint, to return numbers from the fact function.



                    Note also that since there are no TeX-special characters (such as #, %, and ) in the setup of the Lua function fact, one can define it via a directlua call, as is done in the code below. No need, in this case, to load the luacode package.



                    documentclassarticle
                    directlua


                    function fact ( n )
                    if n == 0 or n == 1 then
                    return 1
                    else
                    return ( n * fact (n - 1) )
                    end
                    end




                    newcommandfactorial[1]directluatex.sprint(fact(#1))

                    begindocument
                    factorial5
                    enddocument





                    share|improve this answer













                    Here's my recommendation: Move the tex.sprint directive to the definition of the factorial macro, and use return, not tex.sprint, to return numbers from the fact function.



                    Note also that since there are no TeX-special characters (such as #, %, and ) in the setup of the Lua function fact, one can define it via a directlua call, as is done in the code below. No need, in this case, to load the luacode package.



                    documentclassarticle
                    directlua


                    function fact ( n )
                    if n == 0 or n == 1 then
                    return 1
                    else
                    return ( n * fact (n - 1) )
                    end
                    end




                    newcommandfactorial[1]directluatex.sprint(fact(#1))

                    begindocument
                    factorial5
                    enddocument






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 18 at 20:13









                    MicoMico

                    299k32 gold badges410 silver badges813 bronze badges




                    299k32 gold badges410 silver badges813 bronze badges
























                        2














                        documentclassarticle
                        usepackageluacode
                        beginluacode
                        function printfact(n)
                        if n == 0 then n = 1 end
                        for i=2, n-1 do n=n*i end
                        tex.sprint(n)
                        end
                        endluacode
                        newcommand*factorial[1]directluaprintfact(#1)

                        begindocument
                        factorial5
                        enddocument





                        share|improve this answer





























                          2














                          documentclassarticle
                          usepackageluacode
                          beginluacode
                          function printfact(n)
                          if n == 0 then n = 1 end
                          for i=2, n-1 do n=n*i end
                          tex.sprint(n)
                          end
                          endluacode
                          newcommand*factorial[1]directluaprintfact(#1)

                          begindocument
                          factorial5
                          enddocument





                          share|improve this answer



























                            2












                            2








                            2







                            documentclassarticle
                            usepackageluacode
                            beginluacode
                            function printfact(n)
                            if n == 0 then n = 1 end
                            for i=2, n-1 do n=n*i end
                            tex.sprint(n)
                            end
                            endluacode
                            newcommand*factorial[1]directluaprintfact(#1)

                            begindocument
                            factorial5
                            enddocument





                            share|improve this answer













                            documentclassarticle
                            usepackageluacode
                            beginluacode
                            function printfact(n)
                            if n == 0 then n = 1 end
                            for i=2, n-1 do n=n*i end
                            tex.sprint(n)
                            end
                            endluacode
                            newcommand*factorial[1]directluaprintfact(#1)

                            begindocument
                            factorial5
                            enddocument






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jul 18 at 12:34









                            Red-CloudRed-Cloud

                            4,9702 silver badges17 bronze badges




                            4,9702 silver badges17 bronze badges






























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f500490%2farithmetics-in-lualatex%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                                Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                                Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form