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;
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
add a comment |
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
Your lua function returns nothing if n!=0. So fact(n-1) is nil.
– Alain Merigot
Jul 18 at 11:21
add a comment |
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
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
luatex lua luacode directlua
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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):
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).
add a comment |
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
add a comment |
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
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%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
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):
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).
add a comment |
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):
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).
add a comment |
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):
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).
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):
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).
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
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jul 18 at 20:13
MicoMico
299k32 gold badges410 silver badges813 bronze badges
299k32 gold badges410 silver badges813 bronze badges
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jul 18 at 12:34
Red-CloudRed-Cloud
4,9702 silver badges17 bronze badges
4,9702 silver badges17 bronze badges
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%2f500490%2farithmetics-in-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
Your lua function returns nothing if n!=0. So fact(n-1) is nil.
– Alain Merigot
Jul 18 at 11:21