5

Problematic

At the end (of a class), this is the full graph I want to display.

tikz

But during the class, i'll present things step by step with only nodes and arrows in the "case #" area (and like an epiphany at the end, the different blocks are all connected). enter image description here enter image description here enter image description here enter image description here

My problem

On case 1 for instance. First, I want to display only nodes and arrows strictly in the red area (not the arrows leading to other nodes, nor these other nodes outside the area). But if I do a simple \ifBlock1true, I, of course, get in trouble with the arrows coming from 3-2 and 4-3.

For each case with \newif I'll get trouble with arrow i'll finally need to display in the full graph.

Edit 1

As you can see, @Schrodingerscat's great answer recreated the equivalent to visible on outside beamer.

As discussed in comments, inserting these graphs block by block as a single graph in an article or book will leave white space, white shadow of the nodes not yet displayed. (solution 1) .I was willing to have individual blocks i could call with an \def\Block{i} \input{Maingraph}. Then place each block in a figure (centered) in an article or book. Which is solution 2.

MWE below

\documentclass[tikz]{standalone}

\usetikzlibrary{
    backgrounds,
    fit,
    matrix,
    positioning,
}

\def\Couleur{blue}

\begin{document}


\tikzset{
fleche/.style={->,>=latex,very thick, color=\Couleur!30!white},
mkt/.style={anchor=south ,minimum width=100pt,minimum height=25pt},
logo/.style={anchor=south},
noeud/.style={minimum width=80pt,minimum height=20pt},
central/.style={minimum width=50pt,minimum height=20pt},
bro/.style={font=\tiny,minimum width=40pt,  minimum height=10pt},
background/.style={rectangle,rounded corners,inner sep=0.2cm,rounded corners=5mm,opacity=0.7,fill=#1!30},
}


\begin{tikzpicture}[   matrix of nodes,
      row sep = 0.5cm,
      column sep = 0.6cm,
      every node/.style={rectangle,rounded corners,text centered,draw=\Couleur!50},
      row 1/.style={nodes={mkt}},
      row 2/.style={nodes={bro}},
      row 3/.style={nodes={central}},
      row 4/.style={nodes={noeud}},
      row 5/.style={nodes={bro}},
    ]
\matrix (orga) 
{       1-1         &           &   1-3             &           \\  %[1cm] %level 1
        2-1     &           &   2-3     &           \\  %level 2
                    & 3-2   &                       &           \\  %level 3
        4-1     &           &   4-3             &           \\ [-0.2cm] %level 4
};

%------------------ Flèches -------------------------
% Left
    \draw[fleche] (orga-1-1) - - (orga-2-1) ;
    \draw[fleche] (orga-2-1) - - (orga-4-1) ;

% Right
    \draw[fleche] (orga-1-3) - - (orga-2-3) ;
    \draw[fleche] (orga-2-3) - - (orga-4-3) ;

% South
    \draw[fleche] (orga-4-3) - - (orga-4-1) ;
    \draw[fleche] (orga-4-1.south east) - - (orga-4-3.south west) ;

% From South
%Gauche
    %Up
    \draw[fleche] (orga-3-2.north west) - - (orga-1-1.south east);
    %Center
    \draw[fleche] (orga-2-1.south east) - - (orga-3-2.north west) ;
    %Down
    \draw[fleche] (orga-4-1.north east) - - (orga-3-2.south west) ;
    \draw[fleche] (orga-3-2.south west) - - (orga-4-1.north east);
%Droit
    %Up
    \draw[fleche] (orga-3-2.north east) - - (orga-1-3.south west);
    %Center
    \draw[fleche] (orga-2-3.south west) - - (orga-3-2.north east) ;
    %Down
    \draw[fleche] (orga-4-3.north west) - - (orga-3-2.south east) ;
    \draw[fleche] (orga-3-2.south east) - - (orga-4-3.north west);


   \begin{pgfonlayer}{background}
%        \node [background={red},
%                    fit=(orga-1-1) (orga-4-1),
%                    label=above:{Case 1}] {};
%        \node [background={blue},
%                    fit=(orga-1-3) (orga-4-3),
%                    label=above:{Case 2}] {};
%        \node [background={green},
%                    fit=(orga-1-1) (orga-1-3),
%                    label=right:{Case 3}] {};
%        \node [background={orange},
%                    fit=(orga-4-1) (orga-4-3),
%                    label=right:{Case 4}] {};
    \end{pgfonlayer}

\end{tikzpicture}

\end{document}
2
  • 2
    Are you using a beamer presentation or is this going to be standalone only (i.e. also no standalone frame)? If it is a beamer presentation, then you only need the beamer-overlay-styles library, same applies to the standalone frame.
    – user194703
    May 6, 2020 at 21:45
  • @Schrödinger'scat in beamer I was thinking of the "visible" on style. But I'd like to have independent standalone indeed.
    – JeT
    May 6, 2020 at 21:47

1 Answer 1

4

This is a poor-cat's version of the nice visible on style that ships with the beamer-overlay-styles library. You can assign each element a class, which controls its visibility. So class={1,3} means that the element shows up on the first and third overlay. If you do a beamer presentation, you can load the beamer-overlay-styles library, and forget about all this class stuff and use visible on=<1,3> instead. However, you may still be interested in the recycle bounding box style in case that you not only change the visibility but have different elements on different slides, as it happens with \pause and \ifnums.

\documentclass[tikz]{standalone}
\makeatletter
%membership test    
\pgfmathdeclarefunction{memberQ}{2}{%
  \begingroup%
    \edef\pgfutil@tmpb{0}%
    \edef\pgfutil@tmpa{#2}%
    \expandafter\pgfmath@member@i#1\pgfmath@token@stop
    \edef\pgfmathresult{\pgfutil@tmpb}%
    \pgfmath@smuggleone\pgfmathresult%
  \endgroup}
\def\pgfmath@member@i#1{%
    \ifx\pgfmath@token@stop#1%
    \else
      \edef\pgfutil@tmpc{#1}%
      \ifx\pgfutil@tmpc\pgfutil@tmpa\relax%
      \gdef\pgfutil@tmpb{1}%
      \fi%
      \expandafter\pgfmath@member@i
    \fi}  
\tikzset{recycle bounding box/.style={%
execute at end picture={%
\immediate\write\@mainaux{\xdef\string\figbb@#1{%
(\the\pgf@picminx,\the\pgf@picminy) rectangle (\the\pgf@picmaxx,\the\pgf@picmaxy)}\relax}},
execute at begin picture={%
\ifcsname figbb@#1\endcsname
 \edef\figbb{\csname figbb@#1\endcsname}
 \path \figbb;
\fi}}}  
\makeatother
\usetikzlibrary{
    backgrounds,
    fit,
    matrix,
    positioning,
}

\def\Couleur{blue}

\begin{document}


\tikzset{
fleche/.style={->,>=latex,very thick, color=\Couleur!30!white},
mkt/.style={anchor=south ,minimum width=100pt,minimum height=25pt},
logo/.style={anchor=south},
noeud/.style={minimum width=80pt,minimum height=20pt},
central/.style={minimum width=50pt,minimum height=20pt},
bro/.style={font=\tiny,minimum width=40pt,  minimum height=10pt},
background/.style={rectangle,rounded corners,inner sep=0.2cm,rounded corners=5mm,opacity=0.7,fill=#1!30},
class/.code={\pgfmathtruncatemacro\itest{memberQ("{#1}",\X)||(\X==5)}%
      \ifnum\itest=1
       \tikzset{opacity=1}
      \else
       \tikzset{opacity=0}
      \fi
      }}

\foreach \X in {1,...,5} {%
\begin{tikzpicture}%[recycle bounding box=A] %if you have size-changing animations
\matrix[matrix of nodes,
      row sep = 0.5cm,
      column sep = 0.6cm,
      every node/.style={rectangle,rounded corners,text centered,draw=\Couleur!50},
      row 1/.style={nodes={mkt}},
      row 2/.style={nodes={bro}},
      row 3/.style={nodes={central}},
      row 4/.style={nodes={noeud}},
      row 5/.style={nodes={bro}},
      ampersand replacement=\&] (orga) 
{       |[class={1,3}]| 1-1         \&           \&  |[class={2,3}]| 1-3             \&           \\  %[1cm] %level 1
        |[class={1}]| 2-1     \&           \& |[class={2}]|  2-3     \&           \\  %level 2
                    \& |[class={5}]| 3-2   \&                       \&           \\  %level 3
        |[class={1,4}]| 4-1     \&           \& |[class={2,4}]|  4-3             \&           \\ [-0.2cm] %level 4
};

%------------------ Flèches -------------------------
% Left
    \draw[fleche,class={1}] (orga-1-1) -- (orga-2-1) ;
    \draw[fleche,class={1}] (orga-2-1) -- (orga-4-1) ;

% Right
    \draw[fleche,class={2}] (orga-1-3) -- (orga-2-3) ;
    \draw[fleche,class={2}] (orga-2-3) -- (orga-4-3) ;

% South
    \draw[fleche,class={4}] (orga-4-3) -- (orga-4-1) ;
    \draw[fleche,class={4}] (orga-4-1.south east) - - (orga-4-3.south west) ;

% From South
%Gauche
\ifnum\X=5
    %Up
    \draw[fleche] (orga-3-2.north west) - - (orga-1-1.south east);
    %Center
    \draw[fleche] (orga-2-1.south east) - - (orga-3-2.north west) ;
    %Down
    \draw[fleche] (orga-4-1.north east) - - (orga-3-2.south west) ;
    \draw[fleche] (orga-3-2.south west) - - (orga-4-1.north east);
%Droit
    %Up
    \draw[fleche] (orga-3-2.north east) - - (orga-1-3.south west);
    %Center
    \draw[fleche] (orga-2-3.south west) - - (orga-3-2.north east) ;
    %Down
    \draw[fleche] (orga-4-3.north west) - - (orga-3-2.south east) ;
    \draw[fleche] (orga-3-2.south east) - - (orga-4-3.north west);


%    \begin{pgfonlayer}{background}
%        \node [background={red},
%                    fit=(orga-1-1) (orga-4-1),
%                    label=above:{Case 1}] {};
%        \node [background={blue},
%                    fit=(orga-1-3) (orga-4-3),
%                    label=above:{Case 2}] {};
%        \node [background={green},
%                    fit=(orga-1-1) (orga-1-3),
%                    label=right:{Case 3}] {};
%        \node [background={orange},
%                    fit=(orga-4-1) (orga-4-3),
%                    label=right:{Case 4}] {};
%     \end{pgfonlayer}
\fi

\end{tikzpicture}}

\end{document}

enter image description here

A slight modification allows you to shrink the "overlays" to their respective size: draw things in the overlay mode and only select the bounding box to include things that you really want.

\documentclass[tikz]{standalone}
\makeatletter
%membership test    
\pgfmathdeclarefunction{memberQ}{2}{%
  \begingroup%
    \edef\pgfutil@tmpb{0}%
    \edef\pgfutil@tmpa{#2}%
    \expandafter\pgfmath@member@i#1\pgfmath@token@stop
    \edef\pgfmathresult{\pgfutil@tmpb}%
    \pgfmath@smuggleone\pgfmathresult%
  \endgroup}
\def\pgfmath@member@i#1{%
    \ifx\pgfmath@token@stop#1%
    \else
      \edef\pgfutil@tmpc{#1}%
      \ifx\pgfutil@tmpc\pgfutil@tmpa\relax%
      \gdef\pgfutil@tmpb{1}%
      \fi%
      \expandafter\pgfmath@member@i
    \fi}  
\tikzset{recycle bounding box/.style={%
execute at end picture={%
\immediate\write\@mainaux{\xdef\string\figbb@#1{%
(\the\pgf@picminx,\the\pgf@picminy) rectangle (\the\pgf@picmaxx,\the\pgf@picmaxy)}\relax}},
execute at begin picture={%
\ifcsname figbb@#1\endcsname
 \edef\figbb{\csname figbb@#1\endcsname}
 \path \figbb;
\fi}}}  
\tikzset{ 
reuse path/.code={\pgfsyssoftpath@setcurrentpath{#1}} 
} 

\makeatother
\usetikzlibrary{
    backgrounds,
    fit,
    matrix,
    positioning,
}

\def\Couleur{blue}

\begin{document}


\tikzset{
fleche/.style={->,>=latex,very thick, color=\Couleur!30!white},
mkt/.style={anchor=south ,minimum width=100pt,minimum height=25pt},
logo/.style={anchor=south},
noeud/.style={minimum width=80pt,minimum height=20pt},
central/.style={minimum width=50pt,minimum height=20pt},
bro/.style={font=\tiny,minimum width=40pt,  minimum height=10pt},
background/.style={rectangle,rounded corners,inner sep=0.2cm,rounded corners=5mm,opacity=0.7,fill=#1!30},
class/.code={\pgfmathtruncatemacro\itest{memberQ("{#1}",\X)||(\X==5)}%
      \ifnum\itest=1
       \tikzset{opacity=1,overlay=false}
      \else
       \tikzset{opacity=0,overlay=true}
      \fi
      }}

\foreach \X in {1,...,5} {%
\begin{tikzpicture}%[recycle bounding box=A] %if you have size-changing animations
\matrix[matrix of nodes,overlay,
      row sep = 0.5cm,
      column sep = 0.6cm,
      every node/.style={rectangle,rounded corners,text centered,draw=\Couleur!50},
      row 1/.style={nodes={mkt}},
      row 2/.style={nodes={bro}},
      row 3/.style={nodes={central}},
      row 4/.style={nodes={noeud}},
      row 5/.style={nodes={bro}},
      ampersand replacement=\&] (orga) 
{       |[class={1,3}]| 1-1         \& |[class={3},noeud,opacity=0]| 2-3         \&  |[class={2,3}]| 1-3\\  %[1cm] %level 1
        |[class={1}]| 2-1     \&           \& |[class={2}]|  2-3\\  %level 2
                    \& |[class={5}]| 3-2   \&                      \\  %level 3
        |[class={1,4}]| 4-1     \&  |[class={4},noeud,opacity=0]| 2-3         \& |[class={2,4}]|  4-3\\[-0.2cm] %level 4
};

%------------------ Flèches -------------------------
% Left
    \draw[fleche,class={1}] (orga-1-1) -- (orga-2-1) ;
    \draw[fleche,class={1}] (orga-2-1) -- (orga-4-1) ;

% Right
    \draw[fleche,class={2}] (orga-1-3) -- (orga-2-3) ;
    \draw[fleche,class={2}] (orga-2-3) -- (orga-4-3) ;

% South
    \draw[fleche,class={4}] (orga-4-3) -- (orga-4-1) ;
    \draw[fleche,class={4}] (orga-4-1.south east) - - (orga-4-3.south west) ;

% From South
%Gauche
\ifnum\X=5
    %Up
    \draw[fleche] (orga-3-2.north west) - - (orga-1-1.south east);
    %Center
    \draw[fleche] (orga-2-1.south east) - - (orga-3-2.north west) ;
    %Down
    \draw[fleche] (orga-4-1.north east) - - (orga-3-2.south west) ;
    \draw[fleche] (orga-3-2.south west) - - (orga-4-1.north east);
%Droit
    %Up
    \draw[fleche] (orga-3-2.north east) - - (orga-1-3.south west);
    %Center
    \draw[fleche] (orga-2-3.south west) - - (orga-3-2.north east) ;
    %Down
    \draw[fleche] (orga-4-3.north west) - - (orga-3-2.south east) ;
    \draw[fleche] (orga-3-2.south east) - - (orga-4-3.north west);


%    \begin{pgfonlayer}{background}
%        \node [background={red},
%                    fit=(orga-1-1) (orga-4-1),
%                    label=above:{Case 1}] {};
%        \node [background={blue},
%                    fit=(orga-1-3) (orga-4-3),
%                    label=above:{Case 2}] {};
%        \node [background={green},
%                    fit=(orga-1-1) (orga-1-3),
%                    label=right:{Case 3}] {};
%        \node [background={orange},
%                    fit=(orga-4-1) (orga-4-3),
%                    label=right:{Case 4}] {};
%     \end{pgfonlayer}
\fi
\begin{scope}[overlay]
\node[fit=(orga-1-1) (orga-4-1)] (F1){};
\node[fit=(orga-1-3) (orga-4-3)] (F2){};
\node[fit=(orga-1-1) (orga-1-3)] (F3){};
\node[fit=(orga-4-1) (orga-4-3)] (F4){};
\end{scope}
\ifnum\X<5
\path[use as bounding box] (F\X.south west) rectangle (F\X.north east);
\else
\path[use as bounding box] (orga.south west) rectangle (orga.north east);
\fi
\end{tikzpicture}}

\end{document}

enter image description here

16
  • 2
    @Julien-ElieTaieb Thanks for the explanations, IMHO this makes the question very different from what is written above, at least from how I interpret it. I will leave the answer for others, it may still be useful for some.
    – user194703
    May 6, 2020 at 23:07
  • 2
    @Julien-ElieTaieb Thanks! There is no need to accept an answer that does not solve the question. I had just solved a different problem. I do believe that it should be possible to achieve what you want, but obviously in a matrix (or in any framework that uses relative positioning) this is a bit more tricky. However, you get almost what you want if you replace \tikzset{opacity=0} by \tikzset{opacity=0,overlay} in the class code.
    – user194703
    May 6, 2020 at 23:16
  • 2
    @Julien-ElieTaieb I added a version that fixes the bounding boxes as you describe,
    – user194703
    May 6, 2020 at 23:31
  • 2
    @Julien-ElieTaieb Being a cat helps.
    – user194703
    May 6, 2020 at 23:36
  • 2
    @Julien-ElieTaieb That's a very timely thought: marmots.org/the-marmot-emerge
    – user194703
    May 6, 2020 at 23:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .