Arithmetic with LuaLaTeXHow to do a 'printline' in LuaTeXLuaTeX: How to handle a Lua function that prints TeX macrosLuaLatex: Difference between `dofile` and `require` when loading lua filesLuaLaTeX issue with string (need to escape)Compilation error with LuaLaTeXCompilation error with LuaLaTeX (texnansi.enc)Compilation sequence with lualatexLuaLatex, includespread and libreoffice table with %Print Latex Code with LuaLatex in functionCompiling classicthesis with LuaLaTexDoes LuaLaTeX mess with pagetotal?Lualatex, deal with pictures in luaConTeXt passing current counter value to lua

Can a Cauchy sequence converge for one metric while not converging for another?

How is it possible to have an ability score that is less than 3?

Get value of a counter

What does "Puller Prush Person" mean?

Arrow those variables!

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

Did Shadowfax go to Valinor?

If human space travel is limited by the G force vulnerability, is there a way to counter G forces?

What is the word for reserving something for yourself before others do?

What would happen to a modern skyscraper if it rains micro blackholes?

Do infinite dimensional systems make sense?

DC-DC converter from low voltage at high current, to high voltage at low current

Today is the Center

How does one intimidate enemies without having the capacity for violence?

Arithmetic with LuaLaTeX

Convert two switches to a dual stack, and add outlet - possible here?

Is it possible to run Internet Explorer on OS X El Capitan?

What's the point of deactivating Num Lock on login screens?

Modeling an IP Address

Which country benefited the most from UN Security Council vetoes?

Java Casting: Java 11 throws LambdaConversionException while 1.8 does not

Languages that we cannot (dis)prove to be Context-Free

Client team has low performances and low technical skills: we always fix their work and now they stop collaborate with us. How to solve?

How do I deal with an unproductive colleague in a small company?



Arithmetic with LuaLaTeX


How to do a 'printline' in LuaTeXLuaTeX: How to handle a Lua function that prints TeX macrosLuaLatex: Difference between `dofile` and `require` when loading lua filesLuaLaTeX issue with string (need to escape)Compilation error with LuaLaTeXCompilation error with LuaLaTeX (texnansi.enc)Compilation sequence with lualatexLuaLatex, includespread and libreoffice table with %Print Latex Code with LuaLatex in functionCompiling classicthesis with LuaLaTexDoes LuaLaTeX mess with pagetotal?Lualatex, deal with pictures in luaConTeXt passing current counter value to lua













1















The function I'm trying to create is one that takes two numbers and prints the result with some math. The following is my code:



documentclass[12pt,a4paper]article

begindocument
directlua
function prod(a,b)
tex.print(a "$times$" b "$=$" a*c)
end


The product of 2 and 3: directluaprod(2,3).
enddocument


I can't make it print the whole statement correctly. How to solve it?










share|improve this question

















  • 3





    Try tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")

    – moewe
    25 mins ago






  • 1





    Unlike TeX, to which everything is (by default) a token to be typeset so you can simply write "hello world" and have those words appear in the typeset output, Lua is a general-purpose programming language in which something like a b is a syntax error (assuming a and b are variables). Here, tex.print is a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms of tex.print too, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses .. to concatenate strings.

    – ShreevatsaR
    18 mins ago






  • 1





    BTW instead of concatenating different strings with .., you can also use string.format to build a string, e.g. in a file test.lua put function prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) end and in your file do directluadofile('test.lua') -- here the [[ instead of " is to avoid needing to escape the backslash in times.

    – ShreevatsaR
    12 mins ago















1















The function I'm trying to create is one that takes two numbers and prints the result with some math. The following is my code:



documentclass[12pt,a4paper]article

begindocument
directlua
function prod(a,b)
tex.print(a "$times$" b "$=$" a*c)
end


The product of 2 and 3: directluaprod(2,3).
enddocument


I can't make it print the whole statement correctly. How to solve it?










share|improve this question

















  • 3





    Try tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")

    – moewe
    25 mins ago






  • 1





    Unlike TeX, to which everything is (by default) a token to be typeset so you can simply write "hello world" and have those words appear in the typeset output, Lua is a general-purpose programming language in which something like a b is a syntax error (assuming a and b are variables). Here, tex.print is a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms of tex.print too, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses .. to concatenate strings.

    – ShreevatsaR
    18 mins ago






  • 1





    BTW instead of concatenating different strings with .., you can also use string.format to build a string, e.g. in a file test.lua put function prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) end and in your file do directluadofile('test.lua') -- here the [[ instead of " is to avoid needing to escape the backslash in times.

    – ShreevatsaR
    12 mins ago













1












1








1








The function I'm trying to create is one that takes two numbers and prints the result with some math. The following is my code:



documentclass[12pt,a4paper]article

begindocument
directlua
function prod(a,b)
tex.print(a "$times$" b "$=$" a*c)
end


The product of 2 and 3: directluaprod(2,3).
enddocument


I can't make it print the whole statement correctly. How to solve it?










share|improve this question














The function I'm trying to create is one that takes two numbers and prints the result with some math. The following is my code:



documentclass[12pt,a4paper]article

begindocument
directlua
function prod(a,b)
tex.print(a "$times$" b "$=$" a*c)
end


The product of 2 and 3: directluaprod(2,3).
enddocument


I can't make it print the whole statement correctly. How to solve it?







luatex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 35 mins ago









LevyLevy

427312




427312







  • 3





    Try tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")

    – moewe
    25 mins ago






  • 1





    Unlike TeX, to which everything is (by default) a token to be typeset so you can simply write "hello world" and have those words appear in the typeset output, Lua is a general-purpose programming language in which something like a b is a syntax error (assuming a and b are variables). Here, tex.print is a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms of tex.print too, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses .. to concatenate strings.

    – ShreevatsaR
    18 mins ago






  • 1





    BTW instead of concatenating different strings with .., you can also use string.format to build a string, e.g. in a file test.lua put function prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) end and in your file do directluadofile('test.lua') -- here the [[ instead of " is to avoid needing to escape the backslash in times.

    – ShreevatsaR
    12 mins ago












  • 3





    Try tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")

    – moewe
    25 mins ago






  • 1





    Unlike TeX, to which everything is (by default) a token to be typeset so you can simply write "hello world" and have those words appear in the typeset output, Lua is a general-purpose programming language in which something like a b is a syntax error (assuming a and b are variables). Here, tex.print is a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms of tex.print too, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses .. to concatenate strings.

    – ShreevatsaR
    18 mins ago






  • 1





    BTW instead of concatenating different strings with .., you can also use string.format to build a string, e.g. in a file test.lua put function prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) end and in your file do directluadofile('test.lua') -- here the [[ instead of " is to avoid needing to escape the backslash in times.

    – ShreevatsaR
    12 mins ago







3




3





Try tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")

– moewe
25 mins ago





Try tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")

– moewe
25 mins ago




1




1





Unlike TeX, to which everything is (by default) a token to be typeset so you can simply write "hello world" and have those words appear in the typeset output, Lua is a general-purpose programming language in which something like a b is a syntax error (assuming a and b are variables). Here, tex.print is a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms of tex.print too, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses .. to concatenate strings.

– ShreevatsaR
18 mins ago





Unlike TeX, to which everything is (by default) a token to be typeset so you can simply write "hello world" and have those words appear in the typeset output, Lua is a general-purpose programming language in which something like a b is a syntax error (assuming a and b are variables). Here, tex.print is a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms of tex.print too, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses .. to concatenate strings.

– ShreevatsaR
18 mins ago




1




1





BTW instead of concatenating different strings with .., you can also use string.format to build a string, e.g. in a file test.lua put function prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) end and in your file do directluadofile('test.lua') -- here the [[ instead of " is to avoid needing to escape the backslash in times.

– ShreevatsaR
12 mins ago





BTW instead of concatenating different strings with .., you can also use string.format to build a string, e.g. in a file test.lua put function prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) end and in your file do directluadofile('test.lua') -- here the [[ instead of " is to avoid needing to escape the backslash in times.

– ShreevatsaR
12 mins ago










2 Answers
2






active

oldest

votes


















4














documentclass[12pt,a4paper]article

begindocument
directlua
function prod(a,b)
tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
end


The product of 2 and 3: directluaprod(2,3).
enddocument


works just fine.



The product of 2 and 3: 2 × 3 = 6.



One tricky thing is getting the backslash escaping game right: LuaTeX: How to handle a Lua function that prints TeX macros. directlua expands macros before passing them on to Lua, so times gets messed up. But something like stringtimes, which should stop that expansion does not quite work as intended because t is a special escape for the tab in Lua. Hence we need to escape the backslash there. In Lua you would have to type \times, but in TeX we need to stop the \ from being expanded, so we need string\times. That is one of the reasons why it is often recommended to use the luacode package or externalise Lua functions into their own .lua files and then load them with dofile or require (see for example How to do a 'printline' in LuaTeX, a bit on dofile and require can be found at LuaLatex: Difference between `dofile` and `require` when loading lua files).



Another thing is that you need .. to concatenate strings.



Finally, you probably want the entire expression in math mode and not just certain bits.






share|improve this answer

























  • That's what I was looking for. It worked here. Thank you!

    – Levy
    18 mins ago











  • And the explanation was really helpful!

    – Levy
    17 mins ago


















3














documentclass[12pt,a4paper]article

begindocument
directlua
function prod(a,b)
tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
end


The product of 2 and 3: directluaprod(2,3).
enddocument


enter image description here






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%2f483416%2farithmetic-with-lualatex%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    documentclass[12pt,a4paper]article

    begindocument
    directlua
    function prod(a,b)
    tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
    end


    The product of 2 and 3: directluaprod(2,3).
    enddocument


    works just fine.



    The product of 2 and 3: 2 × 3 = 6.



    One tricky thing is getting the backslash escaping game right: LuaTeX: How to handle a Lua function that prints TeX macros. directlua expands macros before passing them on to Lua, so times gets messed up. But something like stringtimes, which should stop that expansion does not quite work as intended because t is a special escape for the tab in Lua. Hence we need to escape the backslash there. In Lua you would have to type \times, but in TeX we need to stop the \ from being expanded, so we need string\times. That is one of the reasons why it is often recommended to use the luacode package or externalise Lua functions into their own .lua files and then load them with dofile or require (see for example How to do a 'printline' in LuaTeX, a bit on dofile and require can be found at LuaLatex: Difference between `dofile` and `require` when loading lua files).



    Another thing is that you need .. to concatenate strings.



    Finally, you probably want the entire expression in math mode and not just certain bits.






    share|improve this answer

























    • That's what I was looking for. It worked here. Thank you!

      – Levy
      18 mins ago











    • And the explanation was really helpful!

      – Levy
      17 mins ago















    4














    documentclass[12pt,a4paper]article

    begindocument
    directlua
    function prod(a,b)
    tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
    end


    The product of 2 and 3: directluaprod(2,3).
    enddocument


    works just fine.



    The product of 2 and 3: 2 × 3 = 6.



    One tricky thing is getting the backslash escaping game right: LuaTeX: How to handle a Lua function that prints TeX macros. directlua expands macros before passing them on to Lua, so times gets messed up. But something like stringtimes, which should stop that expansion does not quite work as intended because t is a special escape for the tab in Lua. Hence we need to escape the backslash there. In Lua you would have to type \times, but in TeX we need to stop the \ from being expanded, so we need string\times. That is one of the reasons why it is often recommended to use the luacode package or externalise Lua functions into their own .lua files and then load them with dofile or require (see for example How to do a 'printline' in LuaTeX, a bit on dofile and require can be found at LuaLatex: Difference between `dofile` and `require` when loading lua files).



    Another thing is that you need .. to concatenate strings.



    Finally, you probably want the entire expression in math mode and not just certain bits.






    share|improve this answer

























    • That's what I was looking for. It worked here. Thank you!

      – Levy
      18 mins ago











    • And the explanation was really helpful!

      – Levy
      17 mins ago













    4












    4








    4







    documentclass[12pt,a4paper]article

    begindocument
    directlua
    function prod(a,b)
    tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
    end


    The product of 2 and 3: directluaprod(2,3).
    enddocument


    works just fine.



    The product of 2 and 3: 2 × 3 = 6.



    One tricky thing is getting the backslash escaping game right: LuaTeX: How to handle a Lua function that prints TeX macros. directlua expands macros before passing them on to Lua, so times gets messed up. But something like stringtimes, which should stop that expansion does not quite work as intended because t is a special escape for the tab in Lua. Hence we need to escape the backslash there. In Lua you would have to type \times, but in TeX we need to stop the \ from being expanded, so we need string\times. That is one of the reasons why it is often recommended to use the luacode package or externalise Lua functions into their own .lua files and then load them with dofile or require (see for example How to do a 'printline' in LuaTeX, a bit on dofile and require can be found at LuaLatex: Difference between `dofile` and `require` when loading lua files).



    Another thing is that you need .. to concatenate strings.



    Finally, you probably want the entire expression in math mode and not just certain bits.






    share|improve this answer















    documentclass[12pt,a4paper]article

    begindocument
    directlua
    function prod(a,b)
    tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
    end


    The product of 2 and 3: directluaprod(2,3).
    enddocument


    works just fine.



    The product of 2 and 3: 2 × 3 = 6.



    One tricky thing is getting the backslash escaping game right: LuaTeX: How to handle a Lua function that prints TeX macros. directlua expands macros before passing them on to Lua, so times gets messed up. But something like stringtimes, which should stop that expansion does not quite work as intended because t is a special escape for the tab in Lua. Hence we need to escape the backslash there. In Lua you would have to type \times, but in TeX we need to stop the \ from being expanded, so we need string\times. That is one of the reasons why it is often recommended to use the luacode package or externalise Lua functions into their own .lua files and then load them with dofile or require (see for example How to do a 'printline' in LuaTeX, a bit on dofile and require can be found at LuaLatex: Difference between `dofile` and `require` when loading lua files).



    Another thing is that you need .. to concatenate strings.



    Finally, you probably want the entire expression in math mode and not just certain bits.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 12 mins ago

























    answered 24 mins ago









    moewemoewe

    96.1k10117360




    96.1k10117360












    • That's what I was looking for. It worked here. Thank you!

      – Levy
      18 mins ago











    • And the explanation was really helpful!

      – Levy
      17 mins ago

















    • That's what I was looking for. It worked here. Thank you!

      – Levy
      18 mins ago











    • And the explanation was really helpful!

      – Levy
      17 mins ago
















    That's what I was looking for. It worked here. Thank you!

    – Levy
    18 mins ago





    That's what I was looking for. It worked here. Thank you!

    – Levy
    18 mins ago













    And the explanation was really helpful!

    – Levy
    17 mins ago





    And the explanation was really helpful!

    – Levy
    17 mins ago











    3














    documentclass[12pt,a4paper]article

    begindocument
    directlua
    function prod(a,b)
    tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
    end


    The product of 2 and 3: directluaprod(2,3).
    enddocument


    enter image description here






    share|improve this answer



























      3














      documentclass[12pt,a4paper]article

      begindocument
      directlua
      function prod(a,b)
      tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
      end


      The product of 2 and 3: directluaprod(2,3).
      enddocument


      enter image description here






      share|improve this answer

























        3












        3








        3







        documentclass[12pt,a4paper]article

        begindocument
        directlua
        function prod(a,b)
        tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
        end


        The product of 2 and 3: directluaprod(2,3).
        enddocument


        enter image description here






        share|improve this answer













        documentclass[12pt,a4paper]article

        begindocument
        directlua
        function prod(a,b)
        tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
        end


        The product of 2 and 3: directluaprod(2,3).
        enddocument


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 24 mins ago









        Ulrike FischerUlrike Fischer

        198k9305692




        198k9305692



























            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%2f483416%2farithmetic-with-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

            Era Viking Índice Início da Era Viquingue | Cotidiano | Sociedade | Língua | Religião | A arte | As primeiras cidades | As viagens dos viquingues | Viquingues do Oeste e Leste | Fim da Era Viquingue | Fontes históricas | Referências Bibliografia | Ligações externas | Menu de navegação«Sverige då!»«Handel I vikingetid»«O que é Nórdico Antigo»Mito, magia e religião na volsunga saga Um olhar sobre a trajetória mítica do herói sigurd«Bonden var den verklige vikingen»«Vikingatiden»«Vikingatiden»«Vinland»«Guerreiras de Óðinn: As Valkyrjor na Mitologia Viking»1519-9053«Esculpindo símbolos e seres: A arte viking em pedras rúnicas»1679-9313Historia - Tema: VikingarnaAventura e Magia no Mundo das Sagas IslandesasEra Vikinge

            What's the metal clinking sound at the end of credits in Avengers: Endgame?What makes Thanos so strong in Avengers: Endgame?Who is the character that appears at the end of Endgame?What happens to Mjolnir (Thor's hammer) at the end of Endgame?The People's Ages in Avengers: EndgameWhat did Nebula do in Avengers: Endgame?Messing with time in the Avengers: Endgame climaxAvengers: Endgame timelineWhat are the time-travel rules in Avengers Endgame?Why use this song in Avengers: Endgame Opening Logo Sequence?Peggy's age in Avengers Endgame

            Are there legal definitions of ethnicities/races? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Legal definitions in the United StatesAre there truly legal limits on US interest rates?Are gender identity and sexual orientation federally protected?Why is there an apparent legal bias against digital services?What limits are there to the powers of individual judges in the United States legal system?Are women only scholarships legal under Irish / EU law?Is the term “race” defined by Public Law enacted by Congress of the United StatesIs there a legal definition of race in the US?Neighbors are spying for landlord on Renters is it legal?Are Protected Classes Bi-directional?