Can luacode be used in the preamble of a standalone-type document?
I was trying to input/import a standalone document containing a tikz image, which was made using the help of luacode.
I was able to compile the standalone document containing the image, however, luacode being used in the premble of the document (even when unused) caused nothing to be inputted resulting in following error message (among others) in the logs:
"Package standalone Warning: Sub-preamble of file 'diagram.tex' has changed. Content will be ignored. Please rerun LaTeX! on input line 36."
This line contained begin{document}
.
I wasn't quite sure how to nicely give an example, since the problem spans multiple files, so I opted so show the code of each file separately.
Note that the files. Note: I'm using LuaLaTeX as a compiler.
main.tex:
documentclass{article}
usepackage[subpreambles=true]{standalone}
usepackage[utf8]{inputenc}
usepackage[english]{babel}
usepackage{import}
RequirePackage{luatex85}
begin{document}
import{../}{diagram.tex}
end{document}
dragram.tex:
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
%the lua code:
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, ymax=5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
Does anyone know/have suggestions for what the problem might be, and how I could possibly solve it? Or maybe you know of a different way to reach a similar, but working result?
standalone luacode
New contributor
add a comment |
I was trying to input/import a standalone document containing a tikz image, which was made using the help of luacode.
I was able to compile the standalone document containing the image, however, luacode being used in the premble of the document (even when unused) caused nothing to be inputted resulting in following error message (among others) in the logs:
"Package standalone Warning: Sub-preamble of file 'diagram.tex' has changed. Content will be ignored. Please rerun LaTeX! on input line 36."
This line contained begin{document}
.
I wasn't quite sure how to nicely give an example, since the problem spans multiple files, so I opted so show the code of each file separately.
Note that the files. Note: I'm using LuaLaTeX as a compiler.
main.tex:
documentclass{article}
usepackage[subpreambles=true]{standalone}
usepackage[utf8]{inputenc}
usepackage[english]{babel}
usepackage{import}
RequirePackage{luatex85}
begin{document}
import{../}{diagram.tex}
end{document}
dragram.tex:
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
%the lua code:
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, ymax=5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
Does anyone know/have suggestions for what the problem might be, and how I could possibly solve it? Or maybe you know of a different way to reach a similar, but working result?
standalone luacode
New contributor
welcome to tex.se!
– Zarko
12 hours ago
add a comment |
I was trying to input/import a standalone document containing a tikz image, which was made using the help of luacode.
I was able to compile the standalone document containing the image, however, luacode being used in the premble of the document (even when unused) caused nothing to be inputted resulting in following error message (among others) in the logs:
"Package standalone Warning: Sub-preamble of file 'diagram.tex' has changed. Content will be ignored. Please rerun LaTeX! on input line 36."
This line contained begin{document}
.
I wasn't quite sure how to nicely give an example, since the problem spans multiple files, so I opted so show the code of each file separately.
Note that the files. Note: I'm using LuaLaTeX as a compiler.
main.tex:
documentclass{article}
usepackage[subpreambles=true]{standalone}
usepackage[utf8]{inputenc}
usepackage[english]{babel}
usepackage{import}
RequirePackage{luatex85}
begin{document}
import{../}{diagram.tex}
end{document}
dragram.tex:
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
%the lua code:
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, ymax=5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
Does anyone know/have suggestions for what the problem might be, and how I could possibly solve it? Or maybe you know of a different way to reach a similar, but working result?
standalone luacode
New contributor
I was trying to input/import a standalone document containing a tikz image, which was made using the help of luacode.
I was able to compile the standalone document containing the image, however, luacode being used in the premble of the document (even when unused) caused nothing to be inputted resulting in following error message (among others) in the logs:
"Package standalone Warning: Sub-preamble of file 'diagram.tex' has changed. Content will be ignored. Please rerun LaTeX! on input line 36."
This line contained begin{document}
.
I wasn't quite sure how to nicely give an example, since the problem spans multiple files, so I opted so show the code of each file separately.
Note that the files. Note: I'm using LuaLaTeX as a compiler.
main.tex:
documentclass{article}
usepackage[subpreambles=true]{standalone}
usepackage[utf8]{inputenc}
usepackage[english]{babel}
usepackage{import}
RequirePackage{luatex85}
begin{document}
import{../}{diagram.tex}
end{document}
dragram.tex:
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
%the lua code:
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, ymax=5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
Does anyone know/have suggestions for what the problem might be, and how I could possibly solve it? Or maybe you know of a different way to reach a similar, but working result?
standalone luacode
standalone luacode
New contributor
New contributor
edited 11 hours ago
Mico
281k31384773
281k31384773
New contributor
asked 12 hours ago
lamcarlamcar
283
283
New contributor
New contributor
welcome to tex.se!
– Zarko
12 hours ago
add a comment |
welcome to tex.se!
– Zarko
12 hours ago
welcome to tex.se!
– Zarko
12 hours ago
welcome to tex.se!
– Zarko
12 hours ago
add a comment |
2 Answers
2
active
oldest
votes
When standalone extracts the subpreamble, it drops all newlines, such that the preamble is saved as a single line. This not only breaks the lua code (for example "then" and "return" become "thenreturn"), it also confuses luacode
which requires end{luacode*}
to be on a line of it's own.
One solution would be to move the luacode*
block out of the preamble:
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
%the lua code:
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
The allternative way would be to rewrite the lua block to work without newlines and using directlua
: Ass an explicit space at the end of every line, escape \
using string
and use TeX instead of Lua comments.
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
%the lua code:
directlua{
function myfunc(x) %
if x==0 then %
return 0 %
else %
x=3-(x-math.sqrt(3))^2 %
return x %
end %
end %
function cobweb(x,n) %x is the starting point, n is the number of iterations
y1=0 %
for t=1,n,1 do %
y2=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);") %
y1=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);") %
x=y2 %
end %
end
}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
add a comment |
put it before the documentclass
:
RequirePackage{luacode}
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
documentclass[tikz]{standalone}
usepackage{pgfplots}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
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
});
}
});
lamcar 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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f477562%2fcan-luacode-be-used-in-the-preamble-of-a-standalone-type-document%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When standalone extracts the subpreamble, it drops all newlines, such that the preamble is saved as a single line. This not only breaks the lua code (for example "then" and "return" become "thenreturn"), it also confuses luacode
which requires end{luacode*}
to be on a line of it's own.
One solution would be to move the luacode*
block out of the preamble:
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
%the lua code:
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
The allternative way would be to rewrite the lua block to work without newlines and using directlua
: Ass an explicit space at the end of every line, escape \
using string
and use TeX instead of Lua comments.
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
%the lua code:
directlua{
function myfunc(x) %
if x==0 then %
return 0 %
else %
x=3-(x-math.sqrt(3))^2 %
return x %
end %
end %
function cobweb(x,n) %x is the starting point, n is the number of iterations
y1=0 %
for t=1,n,1 do %
y2=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);") %
y1=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);") %
x=y2 %
end %
end
}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
add a comment |
When standalone extracts the subpreamble, it drops all newlines, such that the preamble is saved as a single line. This not only breaks the lua code (for example "then" and "return" become "thenreturn"), it also confuses luacode
which requires end{luacode*}
to be on a line of it's own.
One solution would be to move the luacode*
block out of the preamble:
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
%the lua code:
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
The allternative way would be to rewrite the lua block to work without newlines and using directlua
: Ass an explicit space at the end of every line, escape \
using string
and use TeX instead of Lua comments.
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
%the lua code:
directlua{
function myfunc(x) %
if x==0 then %
return 0 %
else %
x=3-(x-math.sqrt(3))^2 %
return x %
end %
end %
function cobweb(x,n) %x is the starting point, n is the number of iterations
y1=0 %
for t=1,n,1 do %
y2=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);") %
y1=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);") %
x=y2 %
end %
end
}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
add a comment |
When standalone extracts the subpreamble, it drops all newlines, such that the preamble is saved as a single line. This not only breaks the lua code (for example "then" and "return" become "thenreturn"), it also confuses luacode
which requires end{luacode*}
to be on a line of it's own.
One solution would be to move the luacode*
block out of the preamble:
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
%the lua code:
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
The allternative way would be to rewrite the lua block to work without newlines and using directlua
: Ass an explicit space at the end of every line, escape \
using string
and use TeX instead of Lua comments.
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
%the lua code:
directlua{
function myfunc(x) %
if x==0 then %
return 0 %
else %
x=3-(x-math.sqrt(3))^2 %
return x %
end %
end %
function cobweb(x,n) %x is the starting point, n is the number of iterations
y1=0 %
for t=1,n,1 do %
y2=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);") %
y1=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);") %
x=y2 %
end %
end
}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
When standalone extracts the subpreamble, it drops all newlines, such that the preamble is saved as a single line. This not only breaks the lua code (for example "then" and "return" become "thenreturn"), it also confuses luacode
which requires end{luacode*}
to be on a line of it's own.
One solution would be to move the luacode*
block out of the preamble:
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
%the lua code:
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
The allternative way would be to rewrite the lua block to work without newlines and using directlua
: Ass an explicit space at the end of every line, escape \
using string
and use TeX instead of Lua comments.
documentclass{standalone}
RequirePackage{luatex85}
usepackage{pgfplots}
usepackage{luacode}
%the lua code:
directlua{
function myfunc(x) %
if x==0 then %
return 0 %
else %
x=3-(x-math.sqrt(3))^2 %
return x %
end %
end %
function cobweb(x,n) %x is the starting point, n is the number of iterations
y1=0 %
for t=1,n,1 do %
y2=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);") %
y1=myfunc(x) %
tex.sprint("string\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);") %
x=y2 %
end %
end
}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
answered 11 hours ago
Marcel KrügerMarcel Krüger
12.1k11636
12.1k11636
add a comment |
add a comment |
put it before the documentclass
:
RequirePackage{luacode}
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
documentclass[tikz]{standalone}
usepackage{pgfplots}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
add a comment |
put it before the documentclass
:
RequirePackage{luacode}
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
documentclass[tikz]{standalone}
usepackage{pgfplots}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
add a comment |
put it before the documentclass
:
RequirePackage{luacode}
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
documentclass[tikz]{standalone}
usepackage{pgfplots}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
put it before the documentclass
:
RequirePackage{luacode}
begin{luacode*}
function myfunc(x)
if x==0 then
return 0
else
x=3-(x-math.sqrt(3))^2
return x
end
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
y1=0
for t=1,n,1 do
y2=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
y1=myfunc(x)
tex.sprint("\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
x=y2
end
end
end{luacode*}
documentclass[tikz]{standalone}
usepackage{pgfplots}
pgfmathdeclarefunction{Myfunc}{1}{%
edefpgfmathresult{%
directlua{tex.print("" .. myfunc(#1))}%
}%
}
begin{document}
begin{tikzpicture}[
x = 1cm,
scale = 2,
myfunc/.style = {domain = 0:5, samples = 100}
]
draw[->,thick] (0,0)--(5,0) node[right]{$x$};
draw[->,thick] (0,0)--(0,5) node[above]{$y$};
clip (0,0) rectangle (5,5);
color{red}directlua{cobweb(.01,10)}
draw[thick, blue] (0,0)--(5,5);
draw[black, thick] plot [myfunc] (x, {Myfunc(x)});
end{tikzpicture}
end{document}
answered 10 hours ago
HerbertHerbert
275k24418730
275k24418730
add a comment |
add a comment |
lamcar is a new contributor. Be nice, and check out our Code of Conduct.
lamcar is a new contributor. Be nice, and check out our Code of Conduct.
lamcar is a new contributor. Be nice, and check out our Code of Conduct.
lamcar is a new contributor. Be nice, and check out our Code of Conduct.
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%2f477562%2fcan-luacode-be-used-in-the-preamble-of-a-standalone-type-document%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
welcome to tex.se!
– Zarko
12 hours ago