3

I try to create a dynamic graph where a node move along the axis line. When a condition on x is filled (X>1.8), I need the labels to be translated as i tried to do it in the following 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}  

enter image description here

When S_0 gets close to F_1, F_1 disappears, F_2 becomes F_1, F_3 become F_2. Getting back to the initial graph.

enter image description here

enter image description here

Based on @marmot solution, here is an updated code where I still miss the F_2 becomes F_1, F_3 become 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
    You can only use integers with \ifnum. On the other hand, \ifdim works with noninteger dimensions. So if you replace \ifnum\i<1.8\relax by \ifdim\i pt<1.8pt\relax your code runs through. Whether or not this is the desired outcome I don't know. – marmot Jan 11 at 22:20
  • @marmot. Thank you. I updated my code with your comment and updated it so that I can better show the switch effect in labels when \i>=1.8. This code is not elegant at all. Would you see a solution where labels would be in a list ? – Julien-Elie Taieb Jan 11 at 22:41
6

OK, I am very busy now, so this is a quick proposal. Yes, you can repeat things, this is what the /.list key does. And, as already mentioned, it is a standard trick to convert a number to a dimension to be able to play with non-integers.

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

enter image description here

  • You're a magician ! – Julien-Elie Taieb Jan 11 at 23:06
  • 1
    @Julien-ElieTaieb No, just a marmot. (You may already know these ;-) – marmot Jan 11 at 23:09
  • ahahah. :) I updated my code based on youre great solution but with a little adjusmnet you'll see. To be the final solution, I need F2 to become F1 and F3 to become S2 when the threshold is reached. thank you marmot ! – Julien-Elie Taieb Jan 11 at 23:53
  • 1
    @Julien-ElieTaieb You're welcome! You can remove the jumps by replacing \def\modifRand{abs(rand*0.4)} by \pgfmathsetmacro{\modifRand}{abs(rand*0.4)}, but it seems to me that you might have done this precisely because you wanted these jumps, so I didn't touch that. – marmot Jan 12 at 0:37
  • 1
    It's great to have the option of the jumps (and the solution to it!) – Julien-Elie Taieb Jan 12 at 0:40

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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