标签切换条件值

标签切换条件值

我尝试创建一个动态图,其中节点沿轴线移动。当满足 x 上的条件(X>1.8)时,我需要翻译标签,就像我在以下 MWE 中尝试做的那样

  \documentclass{beamer}
  \usepackage{tikz}
  \usetikzlibrary{arrows,positioning} 

\begin{document}
%definition des styles des noeuds
\tikzstyle{D0}=
[draw=yellow!40,fill=red!40,circle,minimum size=20]
\tikzstyle{N_date}=[circle, draw=yellow!40, fill=yellow!80,minimum size=20]
\tikzstyle{comment}=[rectangle, rounded corners,minimum size=20]
\tikzstyle{fleche}=[->, dashed,thick]

\def\Cotango{1}
\def\FactorOne{1}


\foreach \i in {0.1,0.2,...,2.2}
{\begin{frame}
    {Futures dynamic}
    \begin{center}
        \resizebox{7cm}{!}
    {
        \begin{tikzpicture}
            %x axis
            \draw[->, thick] (0,0) -- (10,0);
            %S_0 is moving along the axis
            \node [D0]  (Start) at (\i,0) {$S_0$};

            \ifdim\i pt<1.8pt\relax
                \node [comment] (E1) at (2,5) {\small{1$^{st}$ maturity}};
                \node [comment] (E2) at (5,5) {\small{2$^{nd}$ maturity}};
                \node [comment] (E3) at (8,5) {\small{3$^{rd}$ maturity}};

                    %4- Noeuds des Forwards cotango
                    \ifnum\numexpr\Cotango>0\relax
                        \node [N_date] (FC1) at (2,{ln(2*\FactorOne)}) {\small{$F_1$}};
                        \node [N_date] (FC2) at (5,{ln(5*\FactorOne)}) {\small{$F_2$}};
                        \node [N_date] (FC3) at (8,{ln(8*\FactorOne)}){\small{$F_3$}};
                        \draw [fleche] (FC1) to [bend left=45] (FC2);
                    \draw [fleche] (FC2) to [bend left=45] (FC3);
                    \draw [fleche] (Start) to [bend left=45] (FC1);
                \fi
            \fi

            \ifdim\i pt>=1.8pt\relax
%                   \node [comment] (E1) at (2,5) {\small{1$^{st}$ maturity}};
                    \node [comment] (E2) at (5,5) {\small{1$^{st}$ maturity}};
                    \node [comment] (E3) at (8,5) {\small{2$^{nd}$ maturity}};

                %4- Noeuds des Forwards cotango
                \ifnum\numexpr\Cotango>0\relax
%                       \node [N_date] (FC1) at (2,{ln(2*\FactorOne)}) {\small{$F_1$}};
                        \node [N_date] (FC2) at (5,{ln(5*\FactorOne)}) {\small{$F_1$}};
                        \node [N_date] (FC3) at (8,{ln(8*\FactorOne)}){\small{$F_2$}};
%                       \draw [fleche] (FC1) to [bend left=45] (FC2);
                        \draw [fleche] (FC2) to [bend left=45] (FC3);
                        \draw [fleche] (Start) to [bend left=45] (FC2);
                    \fi
            \fi

        \end{tikzpicture}
    }
\end{center}
\end{frame}
}
\end{document}  

在此处输入图片描述

当 S_0 接近 F_1 时,F_1 消失,F_2 变成 F_1,F_3 变成 F_2。回到初始图表。

在此处输入图片描述

在此处输入图片描述

根据@marmot 解决方案,这里是更新的代码,但我仍然错过 F_2 变成 F_1、F_3 变成 F_2。

  \documentclass{beamer}
   \usepackage{tikz}
   \usetikzlibrary{arrows,positioning} 
    \usetikzlibrary{calc,decorations,decorations.pathreplacing}

  \begin{document}
  %definition des styles des noeuds
  \def\Cotango{1}
  \def\modifRand{abs(rand*0.4)}
  \def\FactorOne{1}

  \pgfmathsetseed{1}
  \tikzset{
     D0/.style={draw=red,fill=red!40,circle,minimum size=20},
    N_date/.style={circle, draw=yellow!40, fill=yellow!80,minimum size=20},
    comment/.style={rectangle, fill=blue!40,rounded corners,minimum size=20},
    fleche/.style={->,thin},
    jump/.style={insert path={ edge[bend left=45,fleche] 
        (-1+#1*3,{ln((-1+#1*3)*\FactorOne)+\modifRand})
        (-1+#1*3,{ln((-1+#1*3)*\FactorOne)+\modifRand}) node[N_date] (F-#1)
        {\small{$F_{{#1}}$}} (F-#1) }}}


\foreach \i in {0.3,0.6,...,8.0}
{\begin{frame}
   {Futures dynamic}
    \begin{center}
    \resizebox{7cm}{!}
    {
        \begin{tikzpicture}
            %x axis
            \draw[gray!80,->, thick] (0,0) -- (10,0);
            %S_0 is moving along the axis

    \ifdim\i pt<1.8pt\relax                   
            \draw[fleche] node [D0]  (Start) at (\i,0) {$S_0$} [jump/.list={1,2,3}];
            \node [comment] (E1) at (2,5) {\small{1$^{st}$ maturity}};
            \node [comment] (E2) at (5,5) {\small{2$^{nd}$ maturity}};
            \node [comment] (E3) at (8,5) {\small{3$^{rd}$ maturity}};
    \else
        \ifdim\i pt<4.8pt\relax 
            \draw[fleche] node [D0]  (Start) at (\i,0) {$S_0$} [jump/.list={2,3}];
            \node [comment] (E2) at (5,5) {\small{1$^{st}$ maturity}};
            \node [comment] (E3) at (8,5) {\small{2$^{nd}$ maturity}};
        \else
            \draw[fleche] node [D0]  (Start) at (\i,0) {$S_0$} [jump=3];
            \node [comment] (E3) at (8,5) {\small{1$^{st}$ maturity}};

        \fi 
    \fi

        \end{tikzpicture}
    }
    \end{center}
    \end{frame}
}
\end{document} 

答案1

好的,我现在很忙,所以这是一个简短的建议。是的,你可以重复做事,这就是关键所在/.list。而且,正如前面提到的,将数字转换为维度以便能够处理非整数是一个标准技巧。

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
%definition des styles des noeuds
\def\Cotango{1}
\def\modifRand{abs(rand*0.4)}
\def\FactorOne{1}

\pgfmathsetseed{1}
\tikzset{
   D0/.style={draw=red,fill=red!40,circle,minimum size=20},
  N_date/.style={circle, draw=yellow!40, fill=yellow!80,minimum size=20},
  comment/.style={rectangle, fill=blue!40,rounded corners,minimum size=20},
  fleche/.style={->,thin},
  jump/.style args={#1|#2}{insert path={ edge[bend left=45,fleche] 
      (-1+#1*3,{ln((-1+#1*3)*\FactorOne)+\modifRand})
      (-1+#1*3,{ln((-1+#1*3)*\FactorOne)+\modifRand}) node[N_date,font=\small] (F-#1)
      {$F_{{#2}}$} (F-#1) }}}


\foreach \i in {0.3,0.6,...,8.0}
{\begin{frame}
   {Futures dynamic}
    \begin{center}
    \resizebox{7cm}{!}
    {
        \begin{tikzpicture}
            %x axis
            \draw[gray!80,->, thick] (0,0) -- (10,0);
            %S_0 is moving along the axis

    \ifdim\i pt<1.8pt\relax                   
            \draw[fleche] node [D0]  (Start) at (\i,0) {$S_0$} 
            [jump/.list={1|1,2|2,3|3}];
            \node [comment] (E1) at (2,5) {\small{1$^{st}$ maturity}};
            \node [comment] (E2) at (5,5) {\small{2$^{nd}$ maturity}};
            \node [comment] (E3) at (8,5) {\small{3$^{rd}$ maturity}};
    \else
        \ifdim\i pt<4.8pt\relax 
            \draw[fleche] node [D0]  (Start) at (\i,0) {$S_0$} 
            [jump/.list={2|1,3|2}];
            \node [comment] (E2) at (5,5) {\small{1$^{st}$ maturity}};
            \node [comment] (E3) at (8,5) {\small{2$^{nd}$ maturity}};
        \else
            \draw[fleche] node [D0]  (Start) at (\i,0) {$S_0$} [jump=3|1];
            \node [comment] (E3) at (8,5) {\small{1$^{st}$ maturity}};

        \fi 
    \fi

        \end{tikzpicture}
    }
    \end{center}
    \end{frame}
}
\end{document} 

在此处输入图片描述

相关内容