Can luacode be used in the preamble of a standalone-type document?Standalone document with chapterUse of...

Where was Karl Mordo in Infinity War?

How can I improve my fireworks photography?

On what did Lego base the appearance of the new Hogwarts minifigs?

Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?

Meaning of すきっとした

How should I state my MS degree in my CV when it was in practice a joint-program?

What's a good word to describe a public place that looks like it wouldn't be rough?

Could quantum mechanics be necessary to analyze some biology scenarios?

Obtaining a matrix of complex values from associations giving the real and imaginary parts of each element?

Inventor that creates machine that grabs man from future

Avoiding morning and evening handshakes

Connecting top and bottom of adjacent circles

How to approximate rolls for potions of healing using only d6's?

Why can I easily sing or whistle a tune I've just heard, but not as easily reproduce it on an instrument?

Word to be used for "standing with your toes pointing out"

Can I retract my name from an already published manuscript?

How to add multiple differently colored borders around a node?

Why do neural networks need so many training examples to perform?

How to mitigate "bandwagon attacking" from players?

Find the number of ways to express 1050 as sum of consecutive integers

How to prepare vegetables for a sandwich that can last for several days in a fridge?

For Loop and Sum

Meth dealer reference in Family Guy

Crystal compensation for temp and voltage



Can luacode be used in the preamble of a standalone-type document?


Standalone document with chapterUse of apacite package with subfiles package or standalone package results in LaTeX Error: Can be used only in preambleHow can I make a standalone document loading siunitx work?Can the standalone package work with sections?Standalone package, preserve preambleCan the standalone document type be made compatible with tikzexternalize?How to make the (COMPLETE) preamble of a Standalone document available to a `Main Document`?standalone - Can I specify the font size in the document?include a preamble in a standaloneStandalone: document size













5















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?










share|improve this question









New contributor




lamcar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • welcome to tex.se!

    – Zarko
    13 hours ago
















5















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?










share|improve this question









New contributor




lamcar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • welcome to tex.se!

    – Zarko
    13 hours ago














5












5








5








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?










share|improve this question









New contributor




lamcar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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






share|improve this question









New contributor




lamcar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




lamcar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 12 hours ago









Mico

281k31384773




281k31384773






New contributor




lamcar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 13 hours ago









lamcarlamcar

283




283




New contributor




lamcar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





lamcar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






lamcar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • welcome to tex.se!

    – Zarko
    13 hours ago



















  • welcome to tex.se!

    – Zarko
    13 hours ago

















welcome to tex.se!

– Zarko
13 hours ago





welcome to tex.se!

– Zarko
13 hours ago










2 Answers
2






active

oldest

votes


















4














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}





share|improve this answer































    3














    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}





    share|improve this answer























      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.










      draft saved

      draft discarded


















      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









      4














      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}





      share|improve this answer




























        4














        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}





        share|improve this answer


























          4












          4








          4







          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}





          share|improve this answer













          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}






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 12 hours ago









          Marcel KrügerMarcel Krüger

          12.1k11636




          12.1k11636























              3














              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}





              share|improve this answer




























                3














                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}





                share|improve this answer


























                  3












                  3








                  3







                  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}





                  share|improve this answer













                  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}






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 11 hours ago









                  HerbertHerbert

                  275k24418730




                  275k24418730






















                      lamcar is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      El tren de la libertad Índice Antecedentes "Porque yo decido" Desarrollo de la...

                      Puerta de Hutt Referencias Enlaces externos Menú de navegación15°58′00″S 5°42′00″O /...

                      Castillo d'Acher Características Menú de navegación