1

I am trying to create an animated flow diagram with Tikz, and I want to use the Tikz matrix inside the nodes of my flow diagram as you can see bellow

enter image description here

The idea is that the matrix A as well as B would be highlighted as follow enter image description here

With the colored boxes changing position with time as the animation runs. When I try to input a Tikz matrix the math nodes are not sized properly, here is my code for some matrix Alpha inside a flowchart node

\node (out1) [data,on chain,join] {$\alpha,\beta$
 $
 \alpha =
 \begin{tikzpicture}[baseline=(m-2-1.base)]
 \matrix [{matrix of math nodes}, column sep=5pt, row sep=1pt,
     left delimiter=(,right delimiter=),ampersand replacement=\&] (m)
 {
  1 \& 1 \& 1 \& 0 \& 0 \\
  0 \& 0 \& 0 \& 1 \& 0 \\
  0 \& 0 \& 0 \& 0 \& 1 \\
 };
 \node[myNo=red, fit=(m-1-1) (m-1-3)] {};
 \node[myNo=green, fit=(m-2-4) (m-2-4)] {};
 \node[myNo=blue, fit=(m-3-5) (m-3-5)] {};
 \end{tikzpicture}
 $
 };

Thank you very much for your help.

Romain

7
  • 2
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – dexteritas
    Jun 30 '17 at 16:56
  • 1
    Nesting tikzpictures should in general be avoided, it won't always work as you'd think. Do you really need to do it here? Jun 30 '17 at 17:39
  • You definitely don't need to do it here and you definitely shouldn't do it. Even if it happens to work in a simple case, it isn't meant to work so you have to expect it to break in the future.
    – cfr
    Jul 1 '17 at 0:27
  • Ok, I see no nested tikzpicture environments. How can I draw boxes around some matrix elements in a flow chart node ?
    – RockyRock
    Jul 2 '17 at 8:32
  • I would like to use the Tikz matrix inside a node, but it doesn't seem to work. Then access the Tikz matrix nodes to draw the boxes. Any ideas ?
    – RockyRock
    Jul 3 '17 at 10:05
1

I managed to find an animated Tikz flow chart solution using the Tikz and Animate packages. Ideally, I was looking for a way to run the animation on the boxes around the matrix elements but this solution worked for what I was trying to show.

Hope this can be useful to some of you.

If you have any idea on how to animate boxes within the matrices instead of changing the colors of the matrix elements that would be great.

Romain

\documentclass[aspectratio=169]{beamer}

\mode<presentation> {
\usetheme{Madrid}
}
\usepackage{booktabs} % Allows the use of \toprule, \midrule and \bottomrule in tables
\usepackage{stmaryrd}
\usepackage{mathtools}
\usepackage{tikz}
\usepackage{animate}
\usepackage{ifthen}
\usepackage{color}
\usetikzlibrary{shapes,arrows,chains,fit,matrix}
%---------------------------------------------------------------------------
%   TIKZ COMMANDS
%---------------------------------------------------------------------------
\tikzset{
  myNo/.style={
    draw=#1, thick,
    inner sep=0pt,
    rounded corners
    },
  startstop/.style={
    rectangle,
    rounded corners,
    minimum width=2.5cm,
    minimum height=1cm,
    align=center,
    draw=black,
    fill=red!20
    },
  process/.style={
    rectangle,
    minimum width=2cm,
    minimum height=0.75cm,
    align=center,
    draw=black,
    fill=blue!10
    },
  data/.style={
    trapezium,
    trapezium left angle=70,
    trapezium right angle=-70,
    minimum width=2cm,
    minimum height=0.75cm,
    align=center,
    draw=black,
    fill=blue!10
    },
  decision/.style={
    diamond, 
    minimum width=0.75cm, 
    minimum height=0.75cm,
    align=center,
    draw=black,
    fill=green!20
    },
  arrow/.style={
    thick,->,>=stealth
    }
}

\begin{document}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\tiny
\begin{frame}
\frametitle{Weak interactions based system partitioning using binary LP}
\begin{columns}[c]

\column{.25\textwidth}
Given a \textbf{linear time invariant continuous time} controllable state space model defined by

\begin{equation} \label{eqn1}
\dot{x} = Ax + Bu
\end{equation}

where the matrix $A$ is the \textbf{state matrix} and the matrix $B$ is the 
\textbf{input matrix} respectively with the appropriate sizes for $N$ states 
and $M$ inputs, therefore, $x \in \mathbb{R}^{N}$ and $u \in 
\mathbb{R}^{M}$. Partitioning the system model (\ref{eqn1}) consists of 
decomposing the inputs as well as the states into groups representing 
subsystems. For a given number of partitions $P \in \llbracket 2;\min(N,M) 
\rrbracket$ and for any subsystem $p \in \llbracket 1;P \rrbracket$ the 
model of subsystem $p$ can be expressed as follows

\begin{equation} \label{eqn2}
\begin{aligned}
\dot{x}_{p} &= A_{pp} x_{p} + B_{pp} u_{p} \\
&+ \sum_{\substack{j=1 \\ j \neq p}}^{P} \big\{ A_{pj} x_{j} + B_{pj} u_{j}  \big\}
\end{aligned}
\end{equation}

with for all $p \in \llbracket 1;P \rrbracket$, $x_{p} \in \mathbb{R}^{N_{p}}$ and $u_{p} \in \mathbb{R}^{M_{p}}$.

\column{.75\textwidth}
\begin{center}
\begin{animateinline}[poster=first,controls,loop]{1} % 1 frames per sec
\multiframe{10}{iTime=1+1}{  % 10 frames
\begin{tikzpicture}[
  start chain=going below,
  every join/.style={arrow},
  node distance=0.4cm,
  scale=0.75,
  every node/.style={transform shape}]

% Nodes
\node (start) [startstop,on chain] {
$A =
\left( {\begin{array}{cccc}
    a_{11} & a_{12} & a_{13} & a_{14} \\
    a_{21} & a_{22} & a_{23} & a_{24} \\
    a_{31} & a_{32} & a_{33} & a_{34} \\
    a_{41} & a_{42} & a_{43} & a_{44} \\
\end{array} } \right)
$
$B =
\left( {\begin{array}{cccc}
    b_{11} & b_{12} & b_{13} & b_{14} \\
    b_{21} & b_{22} & b_{23} & b_{24} \\
    b_{31} & b_{32} & b_{33} & b_{34} \\
    b_{41} & b_{42} & b_{43} & b_{44} \\
\end{array} } \right)
$
$
P = 2
$
};
\node (in1) [process,on chain] {Interactions Minimization};
\ifthenelse{\iTime < 3}{ \node (out1) [data,on chain] {
    $
    \alpha =
    \left( {\begin{array}{cccc}
        ? & ? & ? & ? \\
        ? & ? & ? & ? \\
    \end{array} } \right)
    $
    $
    \beta =
    \left( {\begin{array}{cccc}
        ? & ? & ? & ? \\
        ? & ? & ? & ? \\
    \end{array} } \right)
    $};}{\ifthenelse{\iTime > 6}{\node (out1) [data,on chain] {
            $
            \alpha =
            \left( {\begin{array}{cccc}
                1 & 1 & 1 & 0 \\
                0 & 0 & 0 & 1 \\
            \end{array} } \right)
            $
            $
            \beta =
            \left( {\begin{array}{cccc}
                1 & 1 & 0 & 1 \\
                0 & 0 & 1 & 0 \\
            \end{array} } \right)
            $};}{\node (out1) [data,on chain] {
                    $
                    \alpha =
                    \left( {\begin{array}{cccc}
                        1 & 1 & 0 & 0 \\
                        0 & 0 & 1 & 1 \\
                    \end{array} } \right)
                    $
                    $
                    \beta =
                    \left( {\begin{array}{cccc}
                        1 & 1 & 0 & 0 \\
                        0 & 0 & 1 & 1 \\
                    \end{array} } \right)
                    $
};}}
\ifthenelse{\iTime < 4}{ \node (out2) [data,on chain] {
$A =
\left( {\begin{array}{cccc}
    a_{11} & a_{12} & a_{13} & a_{14} \\
    a_{21} & a_{22} & a_{23} & a_{24} \\
    a_{31} & a_{32} & a_{33} & a_{34} \\
    a_{41} & a_{42} & a_{43} & a_{44} \\
\end{array} } \right)
$
$B =
\left( {\begin{array}{cccc}
    b_{11} & b_{12} & b_{13} & b_{14} \\
    b_{21} & b_{22} & b_{23} & b_{24} \\
    b_{31} & b_{32} & b_{33} & b_{34} \\
    b_{41} & b_{42} & b_{43} & b_{44} \\
\end{array} } \right)
$
};}{\ifthenelse{\iTime > 7}{\node (out2) [data,on chain] {
$A =
\left( {\begin{array}{cccc}
    \textcolor{red}{a_{11}} & \textcolor{red}{a_{12}} & \textcolor{red}{a_{13}} & a_{14} \\
    \textcolor{red}{a_{21}} & \textcolor{red}{a_{22}} & \textcolor{red}{a_{23}} & a_{24} \\
    \textcolor{red}{a_{31}} & \textcolor{red}{a_{32}} & \textcolor{red}{a_{33}} & a_{34} \\
    a_{41} & a_{42} & a_{43} & \textcolor{blue}{a_{44}} \\
\end{array} } \right)
$
$B =
\left( {\begin{array}{cccc}
    \textcolor{red}{b_{11}} & \textcolor{red}{b_{12}} & b_{13} & \textcolor{red}{b_{14}} \\
    \textcolor{red}{b_{21}} & \textcolor{red}{b_{22}} & b_{23} & \textcolor{red}{b_{24}} \\
    \textcolor{red}{b_{31}} & \textcolor{red}{b_{32}} & b_{33} & \textcolor{red}{b_{34}} \\
    b_{41} & b_{42} & \textcolor{blue}{b_{43}} & b_{44} \\
\end{array} } \right)
$
};}{\node (out2) [data,on chain] {
$A =
\left( {\begin{array}{cccc}
    \textcolor{red}{a_{11}} & \textcolor{red}{a_{12}} & a_{13} & a_{14} \\
    \textcolor{red}{a_{21}} & \textcolor{red}{a_{22}} & a_{23} & a_{24} \\
    a_{31} & a_{32} & \textcolor{blue}{a_{33}} & \textcolor{blue}{a_{34}} \\
    a_{41} & a_{42} & \textcolor{blue}{a_{43}} & \textcolor{blue}{a_{44}} \\
\end{array} } \right)
$
$B =
\left( {\begin{array}{cccc}
    \textcolor{red}{b_{11}} & \textcolor{red}{b_{12}} & b_{13} & b_{14} \\
    \textcolor{red}{b_{21}} & \textcolor{red}{b_{22}} & b_{23} & b_{24} \\
    b_{31} & b_{32} & \textcolor{blue}{b_{33}} & \textcolor{blue}{b_{34}} \\
    b_{41} & b_{42} & \textcolor{blue}{b_{43}} & \textcolor{blue}{b_{44}} \\
\end{array} } \right)
$
};}}
\node (in2) [decision,on chain] {Controllability
\\
Check
};
\node (out3) [process,right of=out1,node distance=150pt] {Controllability Cuts};
\node (out4) [data,on chain] {Controllable Partitions};
% Draw
\draw[arrow] (start) -- node[right,xshift=5pt] {Inputs} (in1);
\draw[arrow] (in1) -- node[right,xshift=5pt] {Optimal Solution} (out1);
\draw[arrow] (out1) -- (out2);
\draw[arrow] (out2) -- (in2);
\draw[arrow] (in2.east) -| node[below,yshift=-5pt] {Not Controllable}  (out3.south);
\draw[arrow] (out3.north) |- node[above,yshift=5pt] {Cuts applied}  (in1.east);
\draw[arrow] (in2) -- node[right,xshift=5pt] {Controllable}(out4);
% Path
\begin{pgfonlayer}{background}
\ifthenelse{\iTime > 1}{
\path[draw,line width=5pt,-,blue!50] (start) edge node {} (in1);}{}

\ifthenelse{\iTime > 2}{
\path[draw,line width=5pt,-,red!50] (in1) edge node {} (out1);}{}

\ifthenelse{\iTime > 3}{
\path[draw,line width=5pt,-,red!50] (out1) edge node {} (out2);}{}

\ifthenelse{\iTime > 4 \AND \iTime < 6}{
\path[draw,line width=5pt,-,red!50] (out2) edge node {} (in2);
\path[draw,line width=5pt,-,red!50] (in2.east) -| (out3.south);}{}

\ifthenelse{\iTime > 5 \AND \iTime < 7}{
\path[draw,line width=5pt,-,red!50] (out3.north) |- (in1.east);}{}

\ifthenelse{\iTime > 6}{
\path[draw,line width=5pt,-,green!50] (in1) edge node {} (out1);}{}
\ifthenelse{\iTime > 7}{
\path[draw,line width=5pt,-,green!50] (out1) edge node {} (out2);}{}

\ifthenelse{\iTime > 8}{
\path[draw,line width=5pt,-,green!50] (out2) edge node {} (in2);}{}

\ifthenelse{\iTime > 9}{
\path[draw,line width=5pt,-,green!50] (in2) edge node {} (out4);}{}
\end{pgfonlayer}
\end{tikzpicture}
}  
\end{animateinline}
\end{center}
\end{columns}
\end{frame}
\end{document}
1
  • you can accept ow answer :-)
    – Zarko
    Sep 17 '17 at 12:13

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.