Calculating equations without duplication using LuaTeX or similar

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












If I have a numerical equation (i.e. no unknowns) in LaTeX is it possible to have the equation parsed and evaluated somehow?



equation showing expanded and calculated forms



I know calculations can be done relatively easily by embedding Lua code using LuaTeX, but these require re-writing the equation. A solution would also have to cope with very small and large numbers, so I believe this is outside the scope of normal TeX.



It just seems silly to define an equation in LaTeX then have to write it again in a linear format in order to be computed.



Thanks in advance.










share|improve this question







New contributor




O.MS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    4
    down vote

    favorite
    1












    If I have a numerical equation (i.e. no unknowns) in LaTeX is it possible to have the equation parsed and evaluated somehow?



    equation showing expanded and calculated forms



    I know calculations can be done relatively easily by embedding Lua code using LuaTeX, but these require re-writing the equation. A solution would also have to cope with very small and large numbers, so I believe this is outside the scope of normal TeX.



    It just seems silly to define an equation in LaTeX then have to write it again in a linear format in order to be computed.



    Thanks in advance.










    share|improve this question







    New contributor




    O.MS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      If I have a numerical equation (i.e. no unknowns) in LaTeX is it possible to have the equation parsed and evaluated somehow?



      equation showing expanded and calculated forms



      I know calculations can be done relatively easily by embedding Lua code using LuaTeX, but these require re-writing the equation. A solution would also have to cope with very small and large numbers, so I believe this is outside the scope of normal TeX.



      It just seems silly to define an equation in LaTeX then have to write it again in a linear format in order to be computed.



      Thanks in advance.










      share|improve this question







      New contributor




      O.MS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      If I have a numerical equation (i.e. no unknowns) in LaTeX is it possible to have the equation parsed and evaluated somehow?



      equation showing expanded and calculated forms



      I know calculations can be done relatively easily by embedding Lua code using LuaTeX, but these require re-writing the equation. A solution would also have to cope with very small and large numbers, so I believe this is outside the scope of normal TeX.



      It just seems silly to define an equation in LaTeX then have to write it again in a linear format in order to be computed.



      Thanks in advance.







      equations luatex






      share|improve this question







      New contributor




      O.MS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      O.MS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      O.MS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 hours ago









      O.MS

      234




      234




      New contributor




      O.MS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      O.MS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      O.MS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          This answer is merely an extension of @marmot's answer. It uses (a) the luacode environment to define a Lua function in a way that allows the use of the % "magic" character and (b) the num macro of the siunitx package, to prettify the output of the Lua calculations.



          It's definitely possible to create a preprocessor-type function that captures and modifies all instances of frac......, log, sin, pi, etc. so that the code can be parsed by Lua. (It has to be at the preprocessor stage, so that TeX will not try to act on frac, ^, etc.) One very important consideration is that TeX's grouping characters, and , will have to be replaced with ( and ). Unless you need to perform lots and lots of formula evaluations in your document, it's almost certainly much quicker to perform the simplifications and adjustments "by hand".



          enter image description here



          documentclassarticle
          usepackageluacode,siunitx
          %% Lua-side code:
          beginluacode
          function luaprint ( n )
          return tex.sprint ( string.format ( "%.3e" , n ) )
          end
          endluacode
          %% LaTeX-side code:
          defluaprint#1numdirectlualuaprint(#1)

          begindocument
          [
          N = frac19.32times1times10^6times6.023times10^23197
          =luaprint(19.32*1*10^6*6.023*10^(23))/197
          ]
          enddocument





          share|improve this answer




















          • The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
            – Sebastiano
            17 mins ago

















          up vote
          4
          down vote













          Welcome to TeX.SE! There is whole discussion on doing serious computations in LaTeX. My answer is based on this answer and at best a starting point, which shows that it is indeed possible to do something along those lines.



          documentclassarticle
          usepackage[fleqn]amsmath
          usepackageluacode
          % from https://tex.stackexchange.com/a/294465/121799
          defluaprint#1directluatex.print(#1)
          begindocument
          beginequation*
          N = frac6.022cdot10^231.2cdot10^19
          =luaprint(1.932*10^6)*6.022*10^(23)/(197)
          endequation*
          enddocument


          enter image description here



          Notice also that there are various possibilities to print these numbers in LateX. Unfortunately, I do not know too much on luacode but what I do know is that section 92 of the pgfmanual lists several possibilities to do that in TikZ/pgf.






          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            O.MS is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f454999%2fcalculating-equations-without-duplication-using-luatex-or-similar%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            This answer is merely an extension of @marmot's answer. It uses (a) the luacode environment to define a Lua function in a way that allows the use of the % "magic" character and (b) the num macro of the siunitx package, to prettify the output of the Lua calculations.



            It's definitely possible to create a preprocessor-type function that captures and modifies all instances of frac......, log, sin, pi, etc. so that the code can be parsed by Lua. (It has to be at the preprocessor stage, so that TeX will not try to act on frac, ^, etc.) One very important consideration is that TeX's grouping characters, and , will have to be replaced with ( and ). Unless you need to perform lots and lots of formula evaluations in your document, it's almost certainly much quicker to perform the simplifications and adjustments "by hand".



            enter image description here



            documentclassarticle
            usepackageluacode,siunitx
            %% Lua-side code:
            beginluacode
            function luaprint ( n )
            return tex.sprint ( string.format ( "%.3e" , n ) )
            end
            endluacode
            %% LaTeX-side code:
            defluaprint#1numdirectlualuaprint(#1)

            begindocument
            [
            N = frac19.32times1times10^6times6.023times10^23197
            =luaprint(19.32*1*10^6*6.023*10^(23))/197
            ]
            enddocument





            share|improve this answer




















            • The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
              – Sebastiano
              17 mins ago














            up vote
            3
            down vote



            accepted










            This answer is merely an extension of @marmot's answer. It uses (a) the luacode environment to define a Lua function in a way that allows the use of the % "magic" character and (b) the num macro of the siunitx package, to prettify the output of the Lua calculations.



            It's definitely possible to create a preprocessor-type function that captures and modifies all instances of frac......, log, sin, pi, etc. so that the code can be parsed by Lua. (It has to be at the preprocessor stage, so that TeX will not try to act on frac, ^, etc.) One very important consideration is that TeX's grouping characters, and , will have to be replaced with ( and ). Unless you need to perform lots and lots of formula evaluations in your document, it's almost certainly much quicker to perform the simplifications and adjustments "by hand".



            enter image description here



            documentclassarticle
            usepackageluacode,siunitx
            %% Lua-side code:
            beginluacode
            function luaprint ( n )
            return tex.sprint ( string.format ( "%.3e" , n ) )
            end
            endluacode
            %% LaTeX-side code:
            defluaprint#1numdirectlualuaprint(#1)

            begindocument
            [
            N = frac19.32times1times10^6times6.023times10^23197
            =luaprint(19.32*1*10^6*6.023*10^(23))/197
            ]
            enddocument





            share|improve this answer




















            • The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
              – Sebastiano
              17 mins ago












            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            This answer is merely an extension of @marmot's answer. It uses (a) the luacode environment to define a Lua function in a way that allows the use of the % "magic" character and (b) the num macro of the siunitx package, to prettify the output of the Lua calculations.



            It's definitely possible to create a preprocessor-type function that captures and modifies all instances of frac......, log, sin, pi, etc. so that the code can be parsed by Lua. (It has to be at the preprocessor stage, so that TeX will not try to act on frac, ^, etc.) One very important consideration is that TeX's grouping characters, and , will have to be replaced with ( and ). Unless you need to perform lots and lots of formula evaluations in your document, it's almost certainly much quicker to perform the simplifications and adjustments "by hand".



            enter image description here



            documentclassarticle
            usepackageluacode,siunitx
            %% Lua-side code:
            beginluacode
            function luaprint ( n )
            return tex.sprint ( string.format ( "%.3e" , n ) )
            end
            endluacode
            %% LaTeX-side code:
            defluaprint#1numdirectlualuaprint(#1)

            begindocument
            [
            N = frac19.32times1times10^6times6.023times10^23197
            =luaprint(19.32*1*10^6*6.023*10^(23))/197
            ]
            enddocument





            share|improve this answer












            This answer is merely an extension of @marmot's answer. It uses (a) the luacode environment to define a Lua function in a way that allows the use of the % "magic" character and (b) the num macro of the siunitx package, to prettify the output of the Lua calculations.



            It's definitely possible to create a preprocessor-type function that captures and modifies all instances of frac......, log, sin, pi, etc. so that the code can be parsed by Lua. (It has to be at the preprocessor stage, so that TeX will not try to act on frac, ^, etc.) One very important consideration is that TeX's grouping characters, and , will have to be replaced with ( and ). Unless you need to perform lots and lots of formula evaluations in your document, it's almost certainly much quicker to perform the simplifications and adjustments "by hand".



            enter image description here



            documentclassarticle
            usepackageluacode,siunitx
            %% Lua-side code:
            beginluacode
            function luaprint ( n )
            return tex.sprint ( string.format ( "%.3e" , n ) )
            end
            endluacode
            %% LaTeX-side code:
            defluaprint#1numdirectlualuaprint(#1)

            begindocument
            [
            N = frac19.32times1times10^6times6.023times10^23197
            =luaprint(19.32*1*10^6*6.023*10^(23))/197
            ]
            enddocument






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 30 mins ago









            Mico

            266k30359743




            266k30359743











            • The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
              – Sebastiano
              17 mins ago
















            • The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
              – Sebastiano
              17 mins ago















            The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
            – Sebastiano
            17 mins ago




            The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
            – Sebastiano
            17 mins ago










            up vote
            4
            down vote













            Welcome to TeX.SE! There is whole discussion on doing serious computations in LaTeX. My answer is based on this answer and at best a starting point, which shows that it is indeed possible to do something along those lines.



            documentclassarticle
            usepackage[fleqn]amsmath
            usepackageluacode
            % from https://tex.stackexchange.com/a/294465/121799
            defluaprint#1directluatex.print(#1)
            begindocument
            beginequation*
            N = frac6.022cdot10^231.2cdot10^19
            =luaprint(1.932*10^6)*6.022*10^(23)/(197)
            endequation*
            enddocument


            enter image description here



            Notice also that there are various possibilities to print these numbers in LateX. Unfortunately, I do not know too much on luacode but what I do know is that section 92 of the pgfmanual lists several possibilities to do that in TikZ/pgf.






            share|improve this answer
























              up vote
              4
              down vote













              Welcome to TeX.SE! There is whole discussion on doing serious computations in LaTeX. My answer is based on this answer and at best a starting point, which shows that it is indeed possible to do something along those lines.



              documentclassarticle
              usepackage[fleqn]amsmath
              usepackageluacode
              % from https://tex.stackexchange.com/a/294465/121799
              defluaprint#1directluatex.print(#1)
              begindocument
              beginequation*
              N = frac6.022cdot10^231.2cdot10^19
              =luaprint(1.932*10^6)*6.022*10^(23)/(197)
              endequation*
              enddocument


              enter image description here



              Notice also that there are various possibilities to print these numbers in LateX. Unfortunately, I do not know too much on luacode but what I do know is that section 92 of the pgfmanual lists several possibilities to do that in TikZ/pgf.






              share|improve this answer






















                up vote
                4
                down vote










                up vote
                4
                down vote









                Welcome to TeX.SE! There is whole discussion on doing serious computations in LaTeX. My answer is based on this answer and at best a starting point, which shows that it is indeed possible to do something along those lines.



                documentclassarticle
                usepackage[fleqn]amsmath
                usepackageluacode
                % from https://tex.stackexchange.com/a/294465/121799
                defluaprint#1directluatex.print(#1)
                begindocument
                beginequation*
                N = frac6.022cdot10^231.2cdot10^19
                =luaprint(1.932*10^6)*6.022*10^(23)/(197)
                endequation*
                enddocument


                enter image description here



                Notice also that there are various possibilities to print these numbers in LateX. Unfortunately, I do not know too much on luacode but what I do know is that section 92 of the pgfmanual lists several possibilities to do that in TikZ/pgf.






                share|improve this answer












                Welcome to TeX.SE! There is whole discussion on doing serious computations in LaTeX. My answer is based on this answer and at best a starting point, which shows that it is indeed possible to do something along those lines.



                documentclassarticle
                usepackage[fleqn]amsmath
                usepackageluacode
                % from https://tex.stackexchange.com/a/294465/121799
                defluaprint#1directluatex.print(#1)
                begindocument
                beginequation*
                N = frac6.022cdot10^231.2cdot10^19
                =luaprint(1.932*10^6)*6.022*10^(23)/(197)
                endequation*
                enddocument


                enter image description here



                Notice also that there are various possibilities to print these numbers in LateX. Unfortunately, I do not know too much on luacode but what I do know is that section 92 of the pgfmanual lists several possibilities to do that in TikZ/pgf.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 5 hours ago









                marmot

                64.9k471139




                64.9k471139




















                    O.MS is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    O.MS is a new contributor. Be nice, and check out our Code of Conduct.












                    O.MS is a new contributor. Be nice, and check out our Code of Conduct.











                    O.MS is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f454999%2fcalculating-equations-without-duplication-using-luatex-or-similar%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    List of Gilmore Girls characters

                    What does second last employer means? [closed]

                    One-line joke