How to use lua code from external file in lualatex?
Clash Royale CLAN TAG#URR8PPP
up vote
13
down vote
favorite
I am learning lualatex because of its abilities to use lua and make it easier for more sophisticated documents.
I am using this document for running some introductory examples. https://www.unirioja.es/cu/jvarona/downloads/numerical-methods-luatex.pdf
I have two questions.
Is it possible to call external Lua libraries downloaded from the web from Lua code being used in Luatex?
Can we write long chunks of lua code in a different file than inside the TeX file itself? Consider the Lorentez attractor examples at the end, which compiles and works well on my machine. But the lua code to produce the attractor is rather long. Is there anyway for me to write this lua code in an external file instead? The following gives me a latex compilation error where I attempted to use
inputscrap.lua
documentclassarticle
usepackageluacode
usepackagepgfplots
usepackagetikz
beginluacode*
inputscrap.lua
endluacode*
newcommandaddLUADEDplot[3]%
directluaprint_LorAttrWithEulerMethod(#2,#3,[[#1]])%
begindocument
begintikzpicture
beginaxis
% SYNTAX: Solution of the Lorenz system
% with step h=0.02 sampled at 1000 points.
addLUADEDplot[color=red,smooth]0.021000;
addLUADEDplot[color=green,smooth]0.021000;
addLUADEDplot[color=blue,smooth]0.021000;
addLUADEDplot[color=cyan,smooth]0.021000;
addLUADEDplot[color=magenta,smooth]0.021000;
addLUADEDplot[color=yellow,smooth]0.021000;
endaxis
endtikzpicture
enddocument
The external file scrap.lua looks like
-- Differential equation of the Lorenz attractor
function f(x,y,z)
local sigma = 3
local rho = 26.5
local beta = 1
return sigma*(y-x), -x*z + rho*x - y, x*y - beta*z
end
-- Code to write PGFplots data as coordinates
function print_LorAttrWithEulerMethod(h,npoints,option)
-- The initial point (x0,y0,z0)
local x0 = 0.0
local y0 = 1.0
local z0 = 0.0
-- we add a random number between -0.25 and 0.25
local x = x0 + (math.random()-0.5)/2
local y = y0 + (math.random()-0.5)/2
local z = z0 + (math.random()-0.5)/2
if option~=[] then
tex.sprint("\addplot3["..option.."] coordinates")
else
tex.sprint("\addplot3 coordinates")
end
-- we dismiss the first 100 points to go into the attractor
for i=1, 100 do
m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
end
for i=1, npoints do
m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
tex.sprint("("..x..","..y..","..z..")")
end
tex.sprint("")
end
The error I get is
ex))) ! LuaTeX error [directlua]:1: unexpected symbol near ''.
luacode@dbg@exec ...code@maybe@printdbg #1 #1
l.8 endluacode*?
luatex
add a comment |
up vote
13
down vote
favorite
I am learning lualatex because of its abilities to use lua and make it easier for more sophisticated documents.
I am using this document for running some introductory examples. https://www.unirioja.es/cu/jvarona/downloads/numerical-methods-luatex.pdf
I have two questions.
Is it possible to call external Lua libraries downloaded from the web from Lua code being used in Luatex?
Can we write long chunks of lua code in a different file than inside the TeX file itself? Consider the Lorentez attractor examples at the end, which compiles and works well on my machine. But the lua code to produce the attractor is rather long. Is there anyway for me to write this lua code in an external file instead? The following gives me a latex compilation error where I attempted to use
inputscrap.lua
documentclassarticle
usepackageluacode
usepackagepgfplots
usepackagetikz
beginluacode*
inputscrap.lua
endluacode*
newcommandaddLUADEDplot[3]%
directluaprint_LorAttrWithEulerMethod(#2,#3,[[#1]])%
begindocument
begintikzpicture
beginaxis
% SYNTAX: Solution of the Lorenz system
% with step h=0.02 sampled at 1000 points.
addLUADEDplot[color=red,smooth]0.021000;
addLUADEDplot[color=green,smooth]0.021000;
addLUADEDplot[color=blue,smooth]0.021000;
addLUADEDplot[color=cyan,smooth]0.021000;
addLUADEDplot[color=magenta,smooth]0.021000;
addLUADEDplot[color=yellow,smooth]0.021000;
endaxis
endtikzpicture
enddocument
The external file scrap.lua looks like
-- Differential equation of the Lorenz attractor
function f(x,y,z)
local sigma = 3
local rho = 26.5
local beta = 1
return sigma*(y-x), -x*z + rho*x - y, x*y - beta*z
end
-- Code to write PGFplots data as coordinates
function print_LorAttrWithEulerMethod(h,npoints,option)
-- The initial point (x0,y0,z0)
local x0 = 0.0
local y0 = 1.0
local z0 = 0.0
-- we add a random number between -0.25 and 0.25
local x = x0 + (math.random()-0.5)/2
local y = y0 + (math.random()-0.5)/2
local z = z0 + (math.random()-0.5)/2
if option~=[] then
tex.sprint("\addplot3["..option.."] coordinates")
else
tex.sprint("\addplot3 coordinates")
end
-- we dismiss the first 100 points to go into the attractor
for i=1, 100 do
m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
end
for i=1, npoints do
m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
tex.sprint("("..x..","..y..","..z..")")
end
tex.sprint("")
end
The error I get is
ex))) ! LuaTeX error [directlua]:1: unexpected symbol near ''.
luacode@dbg@exec ...code@maybe@printdbg #1 #1
l.8 endluacode*?
luatex
Betweenbeginluacode*
andendluacode*
you have to write Lua code andinputscrap.lua
is not valid Lua. Hence you get this cryptic error.
– Henri Menke
Dec 10 at 0:40
add a comment |
up vote
13
down vote
favorite
up vote
13
down vote
favorite
I am learning lualatex because of its abilities to use lua and make it easier for more sophisticated documents.
I am using this document for running some introductory examples. https://www.unirioja.es/cu/jvarona/downloads/numerical-methods-luatex.pdf
I have two questions.
Is it possible to call external Lua libraries downloaded from the web from Lua code being used in Luatex?
Can we write long chunks of lua code in a different file than inside the TeX file itself? Consider the Lorentez attractor examples at the end, which compiles and works well on my machine. But the lua code to produce the attractor is rather long. Is there anyway for me to write this lua code in an external file instead? The following gives me a latex compilation error where I attempted to use
inputscrap.lua
documentclassarticle
usepackageluacode
usepackagepgfplots
usepackagetikz
beginluacode*
inputscrap.lua
endluacode*
newcommandaddLUADEDplot[3]%
directluaprint_LorAttrWithEulerMethod(#2,#3,[[#1]])%
begindocument
begintikzpicture
beginaxis
% SYNTAX: Solution of the Lorenz system
% with step h=0.02 sampled at 1000 points.
addLUADEDplot[color=red,smooth]0.021000;
addLUADEDplot[color=green,smooth]0.021000;
addLUADEDplot[color=blue,smooth]0.021000;
addLUADEDplot[color=cyan,smooth]0.021000;
addLUADEDplot[color=magenta,smooth]0.021000;
addLUADEDplot[color=yellow,smooth]0.021000;
endaxis
endtikzpicture
enddocument
The external file scrap.lua looks like
-- Differential equation of the Lorenz attractor
function f(x,y,z)
local sigma = 3
local rho = 26.5
local beta = 1
return sigma*(y-x), -x*z + rho*x - y, x*y - beta*z
end
-- Code to write PGFplots data as coordinates
function print_LorAttrWithEulerMethod(h,npoints,option)
-- The initial point (x0,y0,z0)
local x0 = 0.0
local y0 = 1.0
local z0 = 0.0
-- we add a random number between -0.25 and 0.25
local x = x0 + (math.random()-0.5)/2
local y = y0 + (math.random()-0.5)/2
local z = z0 + (math.random()-0.5)/2
if option~=[] then
tex.sprint("\addplot3["..option.."] coordinates")
else
tex.sprint("\addplot3 coordinates")
end
-- we dismiss the first 100 points to go into the attractor
for i=1, 100 do
m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
end
for i=1, npoints do
m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
tex.sprint("("..x..","..y..","..z..")")
end
tex.sprint("")
end
The error I get is
ex))) ! LuaTeX error [directlua]:1: unexpected symbol near ''.
luacode@dbg@exec ...code@maybe@printdbg #1 #1
l.8 endluacode*?
luatex
I am learning lualatex because of its abilities to use lua and make it easier for more sophisticated documents.
I am using this document for running some introductory examples. https://www.unirioja.es/cu/jvarona/downloads/numerical-methods-luatex.pdf
I have two questions.
Is it possible to call external Lua libraries downloaded from the web from Lua code being used in Luatex?
Can we write long chunks of lua code in a different file than inside the TeX file itself? Consider the Lorentez attractor examples at the end, which compiles and works well on my machine. But the lua code to produce the attractor is rather long. Is there anyway for me to write this lua code in an external file instead? The following gives me a latex compilation error where I attempted to use
inputscrap.lua
documentclassarticle
usepackageluacode
usepackagepgfplots
usepackagetikz
beginluacode*
inputscrap.lua
endluacode*
newcommandaddLUADEDplot[3]%
directluaprint_LorAttrWithEulerMethod(#2,#3,[[#1]])%
begindocument
begintikzpicture
beginaxis
% SYNTAX: Solution of the Lorenz system
% with step h=0.02 sampled at 1000 points.
addLUADEDplot[color=red,smooth]0.021000;
addLUADEDplot[color=green,smooth]0.021000;
addLUADEDplot[color=blue,smooth]0.021000;
addLUADEDplot[color=cyan,smooth]0.021000;
addLUADEDplot[color=magenta,smooth]0.021000;
addLUADEDplot[color=yellow,smooth]0.021000;
endaxis
endtikzpicture
enddocument
The external file scrap.lua looks like
-- Differential equation of the Lorenz attractor
function f(x,y,z)
local sigma = 3
local rho = 26.5
local beta = 1
return sigma*(y-x), -x*z + rho*x - y, x*y - beta*z
end
-- Code to write PGFplots data as coordinates
function print_LorAttrWithEulerMethod(h,npoints,option)
-- The initial point (x0,y0,z0)
local x0 = 0.0
local y0 = 1.0
local z0 = 0.0
-- we add a random number between -0.25 and 0.25
local x = x0 + (math.random()-0.5)/2
local y = y0 + (math.random()-0.5)/2
local z = z0 + (math.random()-0.5)/2
if option~=[] then
tex.sprint("\addplot3["..option.."] coordinates")
else
tex.sprint("\addplot3 coordinates")
end
-- we dismiss the first 100 points to go into the attractor
for i=1, 100 do
m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
end
for i=1, npoints do
m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
tex.sprint("("..x..","..y..","..z..")")
end
tex.sprint("")
end
The error I get is
ex))) ! LuaTeX error [directlua]:1: unexpected symbol near ''.
luacode@dbg@exec ...code@maybe@printdbg #1 #1
l.8 endluacode*?
luatex
luatex
edited Dec 10 at 0:25
asked Dec 10 at 0:09
smilingbuddha
84441016
84441016
Betweenbeginluacode*
andendluacode*
you have to write Lua code andinputscrap.lua
is not valid Lua. Hence you get this cryptic error.
– Henri Menke
Dec 10 at 0:40
add a comment |
Betweenbeginluacode*
andendluacode*
you have to write Lua code andinputscrap.lua
is not valid Lua. Hence you get this cryptic error.
– Henri Menke
Dec 10 at 0:40
Between
beginluacode*
and endluacode*
you have to write Lua code and inputscrap.lua
is not valid Lua. Hence you get this cryptic error.– Henri Menke
Dec 10 at 0:40
Between
beginluacode*
and endluacode*
you have to write Lua code and inputscrap.lua
is not valid Lua. Hence you get this cryptic error.– Henri Menke
Dec 10 at 0:40
add a comment |
1 Answer
1
active
oldest
votes
up vote
14
down vote
accepted
The best way to do this is to make your external file into a module. That means, you make sure that all variables are local
and in the end you return a table which exports all user-accessible functions. For example f(x,y,z)
is not going to be used outside, so I won't export it. As you can see I can also export functions under a different name.
-- Differential equation of the Lorenz attractor
local function f(x,y,z)
local sigma = 3
local rho = 26.5
local beta = 1
return sigma*(y-x), -x*z + rho*x - y, x*y - beta*z
end
-- Code to write PGFplots data as coordinates
local function print_LorAttrWithEulerMethod(h,npoints,option)
-- The initial point (x0,y0,z0)
local x0 = 0.0
local y0 = 1.0
local z0 = 0.0
-- we add a random number between -0.25 and 0.25
local x = x0 + (math.random()-0.5)/2
local y = y0 + (math.random()-0.5)/2
local z = z0 + (math.random()-0.5)/2
if option~=[] then
tex.sprint("\addplot3["..option.."] coordinates{")
else
tex.sprint("\addplot3 coordinates")
end
-- we dismiss the first 100 points to go into the attractor
for i=1, 100 do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
end
for i=1, npoints do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
tex.sprint("("..x..","..y..","..z..")")
end
tex.sprint("")
end
return LorenzAttractor = print_LorAttrWithEulerMethod
An advantage of having the Lua code in a separate file is that you do not have to worry about catcodes. Therefore you don't need the luacode
package at all. The module you created in scrap.lua
can be loaded like any regular Lua module. Usually you would load it like
local scrap = require("scrap")
but that won't work in LuaTeX because the scrap
variable would only be local to the directlua
chunk it is mentioned in and can hence not be used in other chunks. Therefore you have to make a global variable scrap
to encapsulate the module
directluascrap = require("scrap")
The full document would read
documentclassarticle
usepackagepgfplots
usepackagetikz
directluascrap = require("scrap")
newcommandaddLUADEDplot[3]%
directluascrap.LorenzAttractor(#2,#3,[[#1]])%
begindocument
begintikzpicture
beginaxis
% SYNTAX: Solution of the Lorenz system
% with step h=0.02 sampled at 1000 points.
addLUADEDplot[color=red,smooth]0.021000;
addLUADEDplot[color=green,smooth]0.021000;
addLUADEDplot[color=blue,smooth]0.021000;
addLUADEDplot[color=cyan,smooth]0.021000;
addLUADEDplot[color=magenta,smooth]0.021000;
addLUADEDplot[color=yellow,smooth]0.021000;
endaxis
endtikzpicture
enddocument
Wow! Nice answer! Exactly what I was looking for. Regarding question 1, I will be able to use external Lua libraries fromscrap.lua
right? I am particularly interested in callinghttps://github.com/gvvaughan/lyaml
to read yaml files which contain some experimental data
– smilingbuddha
Dec 10 at 0:56
@smilingbuddha Yes, howeverlyaml
has to be placed such that LuaTeX can find it. Don't worry there is a package to help you:luapackageloader
.
– Henri Menke
Dec 10 at 1:00
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%2f464045%2fhow-to-use-lua-code-from-external-file-in-lualatex%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
14
down vote
accepted
The best way to do this is to make your external file into a module. That means, you make sure that all variables are local
and in the end you return a table which exports all user-accessible functions. For example f(x,y,z)
is not going to be used outside, so I won't export it. As you can see I can also export functions under a different name.
-- Differential equation of the Lorenz attractor
local function f(x,y,z)
local sigma = 3
local rho = 26.5
local beta = 1
return sigma*(y-x), -x*z + rho*x - y, x*y - beta*z
end
-- Code to write PGFplots data as coordinates
local function print_LorAttrWithEulerMethod(h,npoints,option)
-- The initial point (x0,y0,z0)
local x0 = 0.0
local y0 = 1.0
local z0 = 0.0
-- we add a random number between -0.25 and 0.25
local x = x0 + (math.random()-0.5)/2
local y = y0 + (math.random()-0.5)/2
local z = z0 + (math.random()-0.5)/2
if option~=[] then
tex.sprint("\addplot3["..option.."] coordinates{")
else
tex.sprint("\addplot3 coordinates")
end
-- we dismiss the first 100 points to go into the attractor
for i=1, 100 do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
end
for i=1, npoints do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
tex.sprint("("..x..","..y..","..z..")")
end
tex.sprint("")
end
return LorenzAttractor = print_LorAttrWithEulerMethod
An advantage of having the Lua code in a separate file is that you do not have to worry about catcodes. Therefore you don't need the luacode
package at all. The module you created in scrap.lua
can be loaded like any regular Lua module. Usually you would load it like
local scrap = require("scrap")
but that won't work in LuaTeX because the scrap
variable would only be local to the directlua
chunk it is mentioned in and can hence not be used in other chunks. Therefore you have to make a global variable scrap
to encapsulate the module
directluascrap = require("scrap")
The full document would read
documentclassarticle
usepackagepgfplots
usepackagetikz
directluascrap = require("scrap")
newcommandaddLUADEDplot[3]%
directluascrap.LorenzAttractor(#2,#3,[[#1]])%
begindocument
begintikzpicture
beginaxis
% SYNTAX: Solution of the Lorenz system
% with step h=0.02 sampled at 1000 points.
addLUADEDplot[color=red,smooth]0.021000;
addLUADEDplot[color=green,smooth]0.021000;
addLUADEDplot[color=blue,smooth]0.021000;
addLUADEDplot[color=cyan,smooth]0.021000;
addLUADEDplot[color=magenta,smooth]0.021000;
addLUADEDplot[color=yellow,smooth]0.021000;
endaxis
endtikzpicture
enddocument
Wow! Nice answer! Exactly what I was looking for. Regarding question 1, I will be able to use external Lua libraries fromscrap.lua
right? I am particularly interested in callinghttps://github.com/gvvaughan/lyaml
to read yaml files which contain some experimental data
– smilingbuddha
Dec 10 at 0:56
@smilingbuddha Yes, howeverlyaml
has to be placed such that LuaTeX can find it. Don't worry there is a package to help you:luapackageloader
.
– Henri Menke
Dec 10 at 1:00
add a comment |
up vote
14
down vote
accepted
The best way to do this is to make your external file into a module. That means, you make sure that all variables are local
and in the end you return a table which exports all user-accessible functions. For example f(x,y,z)
is not going to be used outside, so I won't export it. As you can see I can also export functions under a different name.
-- Differential equation of the Lorenz attractor
local function f(x,y,z)
local sigma = 3
local rho = 26.5
local beta = 1
return sigma*(y-x), -x*z + rho*x - y, x*y - beta*z
end
-- Code to write PGFplots data as coordinates
local function print_LorAttrWithEulerMethod(h,npoints,option)
-- The initial point (x0,y0,z0)
local x0 = 0.0
local y0 = 1.0
local z0 = 0.0
-- we add a random number between -0.25 and 0.25
local x = x0 + (math.random()-0.5)/2
local y = y0 + (math.random()-0.5)/2
local z = z0 + (math.random()-0.5)/2
if option~=[] then
tex.sprint("\addplot3["..option.."] coordinates{")
else
tex.sprint("\addplot3 coordinates")
end
-- we dismiss the first 100 points to go into the attractor
for i=1, 100 do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
end
for i=1, npoints do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
tex.sprint("("..x..","..y..","..z..")")
end
tex.sprint("")
end
return LorenzAttractor = print_LorAttrWithEulerMethod
An advantage of having the Lua code in a separate file is that you do not have to worry about catcodes. Therefore you don't need the luacode
package at all. The module you created in scrap.lua
can be loaded like any regular Lua module. Usually you would load it like
local scrap = require("scrap")
but that won't work in LuaTeX because the scrap
variable would only be local to the directlua
chunk it is mentioned in and can hence not be used in other chunks. Therefore you have to make a global variable scrap
to encapsulate the module
directluascrap = require("scrap")
The full document would read
documentclassarticle
usepackagepgfplots
usepackagetikz
directluascrap = require("scrap")
newcommandaddLUADEDplot[3]%
directluascrap.LorenzAttractor(#2,#3,[[#1]])%
begindocument
begintikzpicture
beginaxis
% SYNTAX: Solution of the Lorenz system
% with step h=0.02 sampled at 1000 points.
addLUADEDplot[color=red,smooth]0.021000;
addLUADEDplot[color=green,smooth]0.021000;
addLUADEDplot[color=blue,smooth]0.021000;
addLUADEDplot[color=cyan,smooth]0.021000;
addLUADEDplot[color=magenta,smooth]0.021000;
addLUADEDplot[color=yellow,smooth]0.021000;
endaxis
endtikzpicture
enddocument
Wow! Nice answer! Exactly what I was looking for. Regarding question 1, I will be able to use external Lua libraries fromscrap.lua
right? I am particularly interested in callinghttps://github.com/gvvaughan/lyaml
to read yaml files which contain some experimental data
– smilingbuddha
Dec 10 at 0:56
@smilingbuddha Yes, howeverlyaml
has to be placed such that LuaTeX can find it. Don't worry there is a package to help you:luapackageloader
.
– Henri Menke
Dec 10 at 1:00
add a comment |
up vote
14
down vote
accepted
up vote
14
down vote
accepted
The best way to do this is to make your external file into a module. That means, you make sure that all variables are local
and in the end you return a table which exports all user-accessible functions. For example f(x,y,z)
is not going to be used outside, so I won't export it. As you can see I can also export functions under a different name.
-- Differential equation of the Lorenz attractor
local function f(x,y,z)
local sigma = 3
local rho = 26.5
local beta = 1
return sigma*(y-x), -x*z + rho*x - y, x*y - beta*z
end
-- Code to write PGFplots data as coordinates
local function print_LorAttrWithEulerMethod(h,npoints,option)
-- The initial point (x0,y0,z0)
local x0 = 0.0
local y0 = 1.0
local z0 = 0.0
-- we add a random number between -0.25 and 0.25
local x = x0 + (math.random()-0.5)/2
local y = y0 + (math.random()-0.5)/2
local z = z0 + (math.random()-0.5)/2
if option~=[] then
tex.sprint("\addplot3["..option.."] coordinates{")
else
tex.sprint("\addplot3 coordinates")
end
-- we dismiss the first 100 points to go into the attractor
for i=1, 100 do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
end
for i=1, npoints do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
tex.sprint("("..x..","..y..","..z..")")
end
tex.sprint("")
end
return LorenzAttractor = print_LorAttrWithEulerMethod
An advantage of having the Lua code in a separate file is that you do not have to worry about catcodes. Therefore you don't need the luacode
package at all. The module you created in scrap.lua
can be loaded like any regular Lua module. Usually you would load it like
local scrap = require("scrap")
but that won't work in LuaTeX because the scrap
variable would only be local to the directlua
chunk it is mentioned in and can hence not be used in other chunks. Therefore you have to make a global variable scrap
to encapsulate the module
directluascrap = require("scrap")
The full document would read
documentclassarticle
usepackagepgfplots
usepackagetikz
directluascrap = require("scrap")
newcommandaddLUADEDplot[3]%
directluascrap.LorenzAttractor(#2,#3,[[#1]])%
begindocument
begintikzpicture
beginaxis
% SYNTAX: Solution of the Lorenz system
% with step h=0.02 sampled at 1000 points.
addLUADEDplot[color=red,smooth]0.021000;
addLUADEDplot[color=green,smooth]0.021000;
addLUADEDplot[color=blue,smooth]0.021000;
addLUADEDplot[color=cyan,smooth]0.021000;
addLUADEDplot[color=magenta,smooth]0.021000;
addLUADEDplot[color=yellow,smooth]0.021000;
endaxis
endtikzpicture
enddocument
The best way to do this is to make your external file into a module. That means, you make sure that all variables are local
and in the end you return a table which exports all user-accessible functions. For example f(x,y,z)
is not going to be used outside, so I won't export it. As you can see I can also export functions under a different name.
-- Differential equation of the Lorenz attractor
local function f(x,y,z)
local sigma = 3
local rho = 26.5
local beta = 1
return sigma*(y-x), -x*z + rho*x - y, x*y - beta*z
end
-- Code to write PGFplots data as coordinates
local function print_LorAttrWithEulerMethod(h,npoints,option)
-- The initial point (x0,y0,z0)
local x0 = 0.0
local y0 = 1.0
local z0 = 0.0
-- we add a random number between -0.25 and 0.25
local x = x0 + (math.random()-0.5)/2
local y = y0 + (math.random()-0.5)/2
local z = z0 + (math.random()-0.5)/2
if option~=[] then
tex.sprint("\addplot3["..option.."] coordinates{")
else
tex.sprint("\addplot3 coordinates")
end
-- we dismiss the first 100 points to go into the attractor
for i=1, 100 do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
end
for i=1, npoints do
local m = f(x,y,z)
x = x + h * m[1]
y = y + h * m[2]
z = z + h * m[3]
tex.sprint("("..x..","..y..","..z..")")
end
tex.sprint("")
end
return LorenzAttractor = print_LorAttrWithEulerMethod
An advantage of having the Lua code in a separate file is that you do not have to worry about catcodes. Therefore you don't need the luacode
package at all. The module you created in scrap.lua
can be loaded like any regular Lua module. Usually you would load it like
local scrap = require("scrap")
but that won't work in LuaTeX because the scrap
variable would only be local to the directlua
chunk it is mentioned in and can hence not be used in other chunks. Therefore you have to make a global variable scrap
to encapsulate the module
directluascrap = require("scrap")
The full document would read
documentclassarticle
usepackagepgfplots
usepackagetikz
directluascrap = require("scrap")
newcommandaddLUADEDplot[3]%
directluascrap.LorenzAttractor(#2,#3,[[#1]])%
begindocument
begintikzpicture
beginaxis
% SYNTAX: Solution of the Lorenz system
% with step h=0.02 sampled at 1000 points.
addLUADEDplot[color=red,smooth]0.021000;
addLUADEDplot[color=green,smooth]0.021000;
addLUADEDplot[color=blue,smooth]0.021000;
addLUADEDplot[color=cyan,smooth]0.021000;
addLUADEDplot[color=magenta,smooth]0.021000;
addLUADEDplot[color=yellow,smooth]0.021000;
endaxis
endtikzpicture
enddocument
answered Dec 10 at 0:28
Henri Menke
69.2k8153257
69.2k8153257
Wow! Nice answer! Exactly what I was looking for. Regarding question 1, I will be able to use external Lua libraries fromscrap.lua
right? I am particularly interested in callinghttps://github.com/gvvaughan/lyaml
to read yaml files which contain some experimental data
– smilingbuddha
Dec 10 at 0:56
@smilingbuddha Yes, howeverlyaml
has to be placed such that LuaTeX can find it. Don't worry there is a package to help you:luapackageloader
.
– Henri Menke
Dec 10 at 1:00
add a comment |
Wow! Nice answer! Exactly what I was looking for. Regarding question 1, I will be able to use external Lua libraries fromscrap.lua
right? I am particularly interested in callinghttps://github.com/gvvaughan/lyaml
to read yaml files which contain some experimental data
– smilingbuddha
Dec 10 at 0:56
@smilingbuddha Yes, howeverlyaml
has to be placed such that LuaTeX can find it. Don't worry there is a package to help you:luapackageloader
.
– Henri Menke
Dec 10 at 1:00
Wow! Nice answer! Exactly what I was looking for. Regarding question 1, I will be able to use external Lua libraries from
scrap.lua
right? I am particularly interested in calling https://github.com/gvvaughan/lyaml
to read yaml files which contain some experimental data– smilingbuddha
Dec 10 at 0:56
Wow! Nice answer! Exactly what I was looking for. Regarding question 1, I will be able to use external Lua libraries from
scrap.lua
right? I am particularly interested in calling https://github.com/gvvaughan/lyaml
to read yaml files which contain some experimental data– smilingbuddha
Dec 10 at 0:56
@smilingbuddha Yes, however
lyaml
has to be placed such that LuaTeX can find it. Don't worry there is a package to help you: luapackageloader
.– Henri Menke
Dec 10 at 1:00
@smilingbuddha Yes, however
lyaml
has to be placed such that LuaTeX can find it. Don't worry there is a package to help you: luapackageloader
.– Henri Menke
Dec 10 at 1:00
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f464045%2fhow-to-use-lua-code-from-external-file-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
Between
beginluacode*
andendluacode*
you have to write Lua code andinputscrap.lua
is not valid Lua. Hence you get this cryptic error.– Henri Menke
Dec 10 at 0:40