luacode能用在独立类型文档的前言部分吗?

luacode能用在独立类型文档的前言部分吗?

我试图输入/导入一个包含 tikz 图像的独立文档,该文档是使用 luacode 的帮助制作的。我能够编译包含图像的独立文档,但是,在文档的序言中使用 luacode(即使未使用)导致没有输入任何内容,从而导致日志中出现以下错误消息(以及其他消息):

“独立软件包警告:文件‘diagram.tex’的子序言已更改。内容将被忽略。请在输入行 36 处重新运行 LaTeX!。”

此行包含begin{document}

我不太确定如何很好地给出一个例子,因为问题涉及多个文件,所以我选择分别显示每个文件的代码。请注意文件。注意:我使用 LuaLaTeX 作为编译器。

主文本

\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}

有谁知道/有建议说这个问题可能是什么,我该如何解决它?或者你知道有其他方法可以达到类似但有效的结果吗?

答案1

当独立程序提取子前导时,它会删除所有换行符,这样前导就会保存为一行。这不仅会破坏 lua 代码(例如“then”和“return”变成“thenreturn”),还会混淆luacode哪些需要\end{luacode*}放在一行上。

一个解决方案就是将luacode*块移出序言:

\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}

另一种方法是重写 lua 块,使其在没有换行符的情况下工作,并使用\directlua:在每一行末尾添加一个明确的空格,\\使用\stringTeX 而不是 Lua 注释。

\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}

答案2

\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}

相关内容