Perform and show arithmetic with LuaLaTeXHow to do a 'printline' in LuaTeXLuaTeX: How to handle a Lua...
Question on branch cuts and branch points
Why can't we play rap on piano?
Do infinite dimensional systems make sense?
What's that red-plus icon near a text?
if condition in the past
expand `ifthenelse` immediately
How to source a part of a file
How does one intimidate enemies without having the capacity for violence?
Theorems that impeded progress
Replacing matching entries in one column of a file by another column from a different file
Codimension of non-flat locus
Can I make popcorn with any corn?
Mutually beneficial digestive system symbiotes
Is it inappropriate for a student to attend their mentor's dissertation defense?
A case of the sniffles
Did Shadowfax go to Valinor?
Roll the carpet
Important Resources for Dark Age Civilizations?
dbcc cleantable batch size explanation
Why do I get two different answers for this counting problem?
Could an aircraft fly or hover using only jets of compressed air?
A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?
Why is consensus so controversial in Britain?
Get value of a counter
Perform and show 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
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}
begin{document}
directlua{
function prod(a,b)
tex.print(a "$times$" b "$=$" a*c)
end
}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}
I can't make it print the whole statement correctly. How to solve it?
luatex calculations
add a comment |
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}
begin{document}
directlua{
function prod(a,b)
tex.print(a "$times$" b "$=$" a*c)
end
}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}
I can't make it print the whole statement correctly. How to solve it?
luatex calculations
3
Trytex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
– moewe
49 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 likea bis a syntax error (assumingaandbare variables). Here,tex.printis a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms oftex.printtoo, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses..to concatenate strings.
– ShreevatsaR
42 mins ago
2
BTW instead of concatenating different strings with.., you can also usestring.formatto build a string, e.g. in a filetest.luaputfunction prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) endand in your file dodirectlua{dofile('test.lua')}-- here the[[instead of"is to avoid needing to escape the backslash intimes.
– ShreevatsaR
35 mins ago
@ShreevatsaR Thanks for that option!
– Levy
11 mins ago
add a comment |
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}
begin{document}
directlua{
function prod(a,b)
tex.print(a "$times$" b "$=$" a*c)
end
}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}
I can't make it print the whole statement correctly. How to solve it?
luatex calculations
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}
begin{document}
directlua{
function prod(a,b)
tex.print(a "$times$" b "$=$" a*c)
end
}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}
I can't make it print the whole statement correctly. How to solve it?
luatex calculations
luatex calculations
edited 4 mins ago
Mico
285k31388778
285k31388778
asked 59 mins ago
LevyLevy
432312
432312
3
Trytex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
– moewe
49 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 likea bis a syntax error (assumingaandbare variables). Here,tex.printis a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms oftex.printtoo, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses..to concatenate strings.
– ShreevatsaR
42 mins ago
2
BTW instead of concatenating different strings with.., you can also usestring.formatto build a string, e.g. in a filetest.luaputfunction prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) endand in your file dodirectlua{dofile('test.lua')}-- here the[[instead of"is to avoid needing to escape the backslash intimes.
– ShreevatsaR
35 mins ago
@ShreevatsaR Thanks for that option!
– Levy
11 mins ago
add a comment |
3
Trytex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
– moewe
49 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 likea bis a syntax error (assumingaandbare variables). Here,tex.printis a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms oftex.printtoo, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses..to concatenate strings.
– ShreevatsaR
42 mins ago
2
BTW instead of concatenating different strings with.., you can also usestring.formatto build a string, e.g. in a filetest.luaputfunction prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) endand in your file dodirectlua{dofile('test.lua')}-- here the[[instead of"is to avoid needing to escape the backslash intimes.
– ShreevatsaR
35 mins ago
@ShreevatsaR Thanks for that option!
– Levy
11 mins ago
3
3
Try
tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")– moewe
49 mins ago
Try
tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")– moewe
49 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
42 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
42 mins ago
2
2
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 directlua{dofile('test.lua')} -- here the [[ instead of " is to avoid needing to escape the backslash in times.– ShreevatsaR
35 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 directlua{dofile('test.lua')} -- here the [[ instead of " is to avoid needing to escape the backslash in times.– ShreevatsaR
35 mins ago
@ShreevatsaR Thanks for that option!
– Levy
11 mins ago
@ShreevatsaR Thanks for that option!
– Levy
11 mins ago
add a comment |
3 Answers
3
active
oldest
votes
documentclass[12pt,a4paper]{article}
directlua{
function prod(a,b)
tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
end
}
begin{document}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

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.
Also moved the directlua function definition into the preamble. (Thanks to Mico for the suggestion.)
That's what I was looking for. It worked here. Thank you!
– Levy
41 mins ago
And the explanation was really helpful!
– Levy
41 mins ago
@Mico Done. Thanks for the idea.
– moewe
17 mins ago
add a comment |
documentclass[12pt,a4paper]{article}
begin{document}
directlua{
function prod(a,b)
tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
end
}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

add a comment |
Just for completeness, here's a solution that shows how to (a) write the Lua code to an external file, (b) load the Luacode via a directlua{dofile("...")} directive, and (c) set up a LaTeX "wrapper" macro (called showprod in the example below) whose function (pun intended) is to invoke the Lua function.
Note that with this setup, one can write \ rather than string\ to denote a single backslash character. (This is also the case for the luacode and luacode* environments that are provided by the luacode package.)

RequirePackage{filecontents}
begin{filecontents*}{show_prod.lua}
function show_prod ( a , b )
tex.sprint ( "$"..a.."\times"..b.."="..a*b.."$" )
end
end{filecontents*}
documentclass{article}
%% Load Lua code from external file and define a LaTeX "wrapper" macro
directlua{dofile("show_prod.lua")}
newcommandshowprod[2]{directlua{show_prod(#1,#2)}}
begin{document}
The product of 2 and 3: showprod{2}{3}.
end{document}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f483416%2fperform-and-show-arithmetic-with-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
documentclass[12pt,a4paper]{article}
directlua{
function prod(a,b)
tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
end
}
begin{document}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

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.
Also moved the directlua function definition into the preamble. (Thanks to Mico for the suggestion.)
That's what I was looking for. It worked here. Thank you!
– Levy
41 mins ago
And the explanation was really helpful!
– Levy
41 mins ago
@Mico Done. Thanks for the idea.
– moewe
17 mins ago
add a comment |
documentclass[12pt,a4paper]{article}
directlua{
function prod(a,b)
tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
end
}
begin{document}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

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.
Also moved the directlua function definition into the preamble. (Thanks to Mico for the suggestion.)
That's what I was looking for. It worked here. Thank you!
– Levy
41 mins ago
And the explanation was really helpful!
– Levy
41 mins ago
@Mico Done. Thanks for the idea.
– moewe
17 mins ago
add a comment |
documentclass[12pt,a4paper]{article}
directlua{
function prod(a,b)
tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
end
}
begin{document}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

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.
Also moved the directlua function definition into the preamble. (Thanks to Mico for the suggestion.)
documentclass[12pt,a4paper]{article}
directlua{
function prod(a,b)
tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")
end
}
begin{document}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

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.
Also moved the directlua function definition into the preamble. (Thanks to Mico for the suggestion.)
edited 17 mins ago
answered 47 mins ago
moewemoewe
96.2k10117360
96.2k10117360
That's what I was looking for. It worked here. Thank you!
– Levy
41 mins ago
And the explanation was really helpful!
– Levy
41 mins ago
@Mico Done. Thanks for the idea.
– moewe
17 mins ago
add a comment |
That's what I was looking for. It worked here. Thank you!
– Levy
41 mins ago
And the explanation was really helpful!
– Levy
41 mins ago
@Mico Done. Thanks for the idea.
– moewe
17 mins ago
That's what I was looking for. It worked here. Thank you!
– Levy
41 mins ago
That's what I was looking for. It worked here. Thank you!
– Levy
41 mins ago
And the explanation was really helpful!
– Levy
41 mins ago
And the explanation was really helpful!
– Levy
41 mins ago
@Mico Done. Thanks for the idea.
– moewe
17 mins ago
@Mico Done. Thanks for the idea.
– moewe
17 mins ago
add a comment |
documentclass[12pt,a4paper]{article}
begin{document}
directlua{
function prod(a,b)
tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
end
}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

add a comment |
documentclass[12pt,a4paper]{article}
begin{document}
directlua{
function prod(a,b)
tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
end
}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

add a comment |
documentclass[12pt,a4paper]{article}
begin{document}
directlua{
function prod(a,b)
tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
end
}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

documentclass[12pt,a4paper]{article}
begin{document}
directlua{
function prod(a,b)
tex.print(a.. "$string\times$".. b.. "$=$".. a*b)
end
}
The product of 2 and 3: directlua{prod(2,3)}.
end{document}

answered 47 mins ago
Ulrike FischerUlrike Fischer
198k9305692
198k9305692
add a comment |
add a comment |
Just for completeness, here's a solution that shows how to (a) write the Lua code to an external file, (b) load the Luacode via a directlua{dofile("...")} directive, and (c) set up a LaTeX "wrapper" macro (called showprod in the example below) whose function (pun intended) is to invoke the Lua function.
Note that with this setup, one can write \ rather than string\ to denote a single backslash character. (This is also the case for the luacode and luacode* environments that are provided by the luacode package.)

RequirePackage{filecontents}
begin{filecontents*}{show_prod.lua}
function show_prod ( a , b )
tex.sprint ( "$"..a.."\times"..b.."="..a*b.."$" )
end
end{filecontents*}
documentclass{article}
%% Load Lua code from external file and define a LaTeX "wrapper" macro
directlua{dofile("show_prod.lua")}
newcommandshowprod[2]{directlua{show_prod(#1,#2)}}
begin{document}
The product of 2 and 3: showprod{2}{3}.
end{document}
add a comment |
Just for completeness, here's a solution that shows how to (a) write the Lua code to an external file, (b) load the Luacode via a directlua{dofile("...")} directive, and (c) set up a LaTeX "wrapper" macro (called showprod in the example below) whose function (pun intended) is to invoke the Lua function.
Note that with this setup, one can write \ rather than string\ to denote a single backslash character. (This is also the case for the luacode and luacode* environments that are provided by the luacode package.)

RequirePackage{filecontents}
begin{filecontents*}{show_prod.lua}
function show_prod ( a , b )
tex.sprint ( "$"..a.."\times"..b.."="..a*b.."$" )
end
end{filecontents*}
documentclass{article}
%% Load Lua code from external file and define a LaTeX "wrapper" macro
directlua{dofile("show_prod.lua")}
newcommandshowprod[2]{directlua{show_prod(#1,#2)}}
begin{document}
The product of 2 and 3: showprod{2}{3}.
end{document}
add a comment |
Just for completeness, here's a solution that shows how to (a) write the Lua code to an external file, (b) load the Luacode via a directlua{dofile("...")} directive, and (c) set up a LaTeX "wrapper" macro (called showprod in the example below) whose function (pun intended) is to invoke the Lua function.
Note that with this setup, one can write \ rather than string\ to denote a single backslash character. (This is also the case for the luacode and luacode* environments that are provided by the luacode package.)

RequirePackage{filecontents}
begin{filecontents*}{show_prod.lua}
function show_prod ( a , b )
tex.sprint ( "$"..a.."\times"..b.."="..a*b.."$" )
end
end{filecontents*}
documentclass{article}
%% Load Lua code from external file and define a LaTeX "wrapper" macro
directlua{dofile("show_prod.lua")}
newcommandshowprod[2]{directlua{show_prod(#1,#2)}}
begin{document}
The product of 2 and 3: showprod{2}{3}.
end{document}
Just for completeness, here's a solution that shows how to (a) write the Lua code to an external file, (b) load the Luacode via a directlua{dofile("...")} directive, and (c) set up a LaTeX "wrapper" macro (called showprod in the example below) whose function (pun intended) is to invoke the Lua function.
Note that with this setup, one can write \ rather than string\ to denote a single backslash character. (This is also the case for the luacode and luacode* environments that are provided by the luacode package.)

RequirePackage{filecontents}
begin{filecontents*}{show_prod.lua}
function show_prod ( a , b )
tex.sprint ( "$"..a.."\times"..b.."="..a*b.."$" )
end
end{filecontents*}
documentclass{article}
%% Load Lua code from external file and define a LaTeX "wrapper" macro
directlua{dofile("show_prod.lua")}
newcommandshowprod[2]{directlua{show_prod(#1,#2)}}
begin{document}
The product of 2 and 3: showprod{2}{3}.
end{document}
answered 7 mins ago
MicoMico
285k31388778
285k31388778
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f483416%2fperform-and-show-arithmetic-with-lualatex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
Try
tex.print("$" .. a .. "string\times" .. b .. "=" .. a*b .. "$")– moewe
49 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 bis a syntax error (assumingaandbare variables). Here,tex.printis a Lua function that takes a single string as input, so you need to give it a single string. (There are other forms oftex.printtoo, that you can read in the LuaTeX manual, but those are probably not what you want.) Lua uses..to concatenate strings.– ShreevatsaR
42 mins ago
2
BTW instead of concatenating different strings with
.., you can also usestring.formatto build a string, e.g. in a filetest.luaputfunction prod(a,b) tex.print(string.format([[$%d times %d = %d$]], a, b, a*b)) endand in your file dodirectlua{dofile('test.lua')}-- here the[[instead of"is to avoid needing to escape the backslash intimes.– ShreevatsaR
35 mins ago
@ShreevatsaR Thanks for that option!
– Levy
11 mins ago