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

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

Finding the number of integers that are a square and a cube at the same time

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

How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?

Criticizing long fiction. How is it different from short?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

Should I choose Itemized or Standard deduction?

What is better: yes / no radio, or simple checkbox?

raspberry pi change directory (cd) command not working with USB drive

Finding ratio of the area of triangles

Incompressible fluid definition

Why didn't Eru and/or the Valar intervene when Sauron corrupted Númenor?

How to avoid being sexist when trying to employ someone to function in a very sexist environment?

How to push a box with physics engine by another object?

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

Is my plan for fixing my water heater leak bad?

Am I a Rude Number?

Removing debris from PCB

Avoiding morning and evening handshakes

Do my Windows system binaries contain sensitive information?

Why is my solution for the partial pressures of two different gases incorrect?

Is divide-by-zero a security vulnerability?

How do Japanese speakers determine the implied topic when none has been mentioned?

Auto Insert date into Notepad



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
    15 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
    15 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 13 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 15 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
    15 hours ago



















  • welcome to tex.se!

    – Zarko
    15 hours ago

















welcome to tex.se!

– Zarko
15 hours ago





welcome to tex.se!

– Zarko
15 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 14 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 13 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

                      Фонтен-ла-Гаярд Зміст Демографія | Економіка | Посилання |...

                      Список ссавців Італії Природоохоронні статуси | Список |...

                      Маріан Котлеба Зміст Життєпис | Політичні погляди |...