6

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}{%
  \edef\pgfmathresult{%
     \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?

5

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}{%
  \edef\pgfmathresult{%
     \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}{%
  \edef\pgfmathresult{%
     \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}
4

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}{%
    \edef\pgfmathresult{%
        \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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.