关于 tikzmath 的 For 循环不起作用

关于 tikzmath 的 For 循环不起作用

我正在尝试使用 绘制离散动力系统的动力学Tikz。我想使用循环来执行此操作,但由于某种原因,它不会执行\tkzmath循环内的操作。这是带有最小示例的代码。第一个图显示了我想要得到的结果,而第二个图显示了我想要得到的结果,而不必复制粘贴相同的代码 3 次。

\documentclass{article}%
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\begin{tikzpicture}[scale=1.5, line width=1.5]
    \draw[->, line width=1] (0,0) -- (6,0) node[right] {$L_t$};
    \draw[->, line width=1] (0,0) -- (0,4.2) node[above] {$L_{t+1}$};
    \draw[dashed, line width=1] (0,0) -- (4.2, 4.2)node[right] {$L_{t+1}=L_t$};
    \draw[scale=1,domain=0:5,smooth,variable=\x,blue]  plot ({\x},{1.25*\x^0.5})  node[right] {$L_{t+1}=\phi (L_{t};A)$};

    % Initialize location and do 1 iteration
    \tikzmath{\xt=4.8; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt,0) -- (\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);

    % Iterate 3 times to show path
    % 1
    \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
    % 2
    \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
    % 3
    \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
\end{tikzpicture}

\begin{tikzpicture}[scale=1.5, line width=1.5]
    \draw[->, line width=1] (0,0) -- (6,0) node[right] {$L_t$};
    \draw[->, line width=1] (0,0) -- (0,4.2) node[above] {$L_{t+1}$};
    \draw[dashed, line width=1] (0,0) -- (4.2, 4.2)node[right] {$L_{t+1}=L_t$};
    \draw[scale=1,domain=0:5,smooth,variable=\x,blue]  plot ({\x},{1.25*\x^0.5})  node[right] {$L_{t+1}=\phi (L_{t};A)$};

    % Initialize location and do 1 iteration
    \tikzmath{\xt=4.8; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt,0) -- (\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);

    % Iterate 3 times to show path
    \foreach \x in {0,1,2}{
        \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
        \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
        \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
    }
\end{tikzpicture}


\end{document}

任何帮助,将不胜感激。

答案1

您看到的问题源于 的参数\foreach是一个群,因此\xt\yt只在局部更新,而局部更新被“遗忘”。解决这个问题的一个相当直接的方法是使用 使\xt\yt成为全局的\xdef

\documentclass{article}%
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\begin{tikzpicture}[scale=1.5, line width=1.5]
    \draw[->, line width=1] (0,0) -- (6,0) node[right] {$L_t$};
    \draw[->, line width=1] (0,0) -- (0,4.2) node[above] {$L_{t+1}$};
    \draw[dashed, line width=1] (0,0) -- (4.2, 4.2)node[right] {$L_{t+1}=L_t$};
    \draw[scale=1,domain=0:5,smooth,variable=\x,blue]  plot ({\x},{1.25*\x^0.5})  node[right] {$L_{t+1}=\phi (L_{t};A)$};

    % Initialize location and do 1 iteration
    \tikzmath{\xt=4.8; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt,0) -- (\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);

    % Iterate 3 times to show path
    % 1
    \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
    % 2
    \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
    % 3
    \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
\end{tikzpicture}

\begin{tikzpicture}[scale=1.5, line width=1.5]
    \draw[->, line width=1] (0,0) -- (6,0) node[right] {$L_t$};
    \draw[->, line width=1] (0,0) -- (0,4.2) node[above] {$L_{t+1}$};
    \draw[dashed, line width=1] (0,0) -- (4.2, 4.2)node[right] {$L_{t+1}=L_t$};
    \draw[scale=1,domain=0:5,smooth,variable=\x,blue]  plot ({\x},{1.25*\x^0.5})  node[right] {$L_{t+1}=\phi (L_{t};A)$};

    % Initialize location and do 1 iteration
    \tikzmath{\xt=4.8; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt,0) -- (\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);

    % Iterate 3 times to show path
    \foreach \x in {0,1,2}{
        \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
        \xdef\xt{\xt}\xdef\yt{\yt}
        \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
        \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
    }
\end{tikzpicture}
\end{document}

在此处输入图片描述

一个可能不那么直接的方法是(滥用)使用remember可用的密钥\foreach

\documentclass{article}%
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\begin{tikzpicture}[scale=1.5, line width=1.5]
    \draw[->, line width=1] (0,0) -- (6,0) node[right] {$L_t$};
    \draw[->, line width=1] (0,0) -- (0,4.2) node[above] {$L_{t+1}$};
    \draw[dashed, line width=1] (0,0) -- (4.2, 4.2)node[right] {$L_{t+1}=L_t$};
    \draw[scale=1,domain=0:5,smooth,variable=\x,blue]  plot ({\x},{1.25*\x^0.5})  node[right] {$L_{t+1}=\phi (L_{t};A)$};

    % Initialize location and do 1 iteration
    \tikzmath{\xt=4.8; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt,0) -- (\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);

    % Iterate 3 times to show path
    % 1
    \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
    % 2
    \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
    % 3
    \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
\end{tikzpicture}

\begin{tikzpicture}[scale=1.5, line width=1.5]
    \draw[->, line width=1] (0,0) -- (6,0) node[right] {$L_t$};
    \draw[->, line width=1] (0,0) -- (0,4.2) node[above] {$L_{t+1}$};
    \draw[dashed, line width=1] (0,0) -- (4.2, 4.2)node[right] {$L_{t+1}=L_t$};
    \draw[scale=1,domain=0:5,smooth,variable=\x,blue]  plot ({\x},{1.25*\x^0.5})  node[right] {$L_{t+1}=\phi (L_{t};A)$};

    % Initialize location and do 1 iteration
    \tikzmath{\xt=4.8; \yt=1.25*\xt^0.5;}
    \draw[->, dotted, line width=1] (\xt,0) -- (\xt, \yt);
    \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);

    % Iterate 3 times to show path
    \foreach \x [remember=\yt as \yt] in {0,1,2}{
        \tikzmath{\xt=\yt; \yt=1.25*\xt^0.5;}
        \draw[->, dotted, line width=1] (\xt, \xt) --(\xt, \yt);
        \draw[->, dotted, line width=1] (\xt, \yt) --(\yt, \yt);
    }
\end{tikzpicture}
\end{document}

(与上面输出相同。)

相关内容