Calculating equations without duplication using LuaTeX or similar
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
If I have a numerical equation (i.e. no unknowns) in LaTeX is it possible to have the equation parsed and evaluated somehow?
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
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.
add a comment |Â
up vote
4
down vote
favorite
If I have a numerical equation (i.e. no unknowns) in LaTeX is it possible to have the equation parsed and evaluated somehow?
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
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.
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
If I have a numerical equation (i.e. no unknowns) in LaTeX is it possible to have the equation parsed and evaluated somehow?
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
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?
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
equations luatex
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.
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.
add a comment |Â
add a comment |Â
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".
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
The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
– Sebastiano
17 mins ago
add a comment |Â
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
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.
add a comment |Â
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".
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
The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
– Sebastiano
17 mins ago
add a comment |Â
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".
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
The best cannot remain without a vote. Congratulations on your detailed answer. My best regards.
– Sebastiano
17 mins ago
add a comment |Â
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".
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
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".
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
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
add a comment |Â
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
add a comment |Â
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
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.
add a comment |Â
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
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.
add a comment |Â
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
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.
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
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.
answered 5 hours ago


marmot
64.9k471139
64.9k471139
add a comment |Â
add a comment |Â
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.
O.MS is a new contributor. Be nice, and check out our Code of Conduct.
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
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
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
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
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