Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Background: I encounter some unexpected situations while overlaying a tikz matrix in Beamer. The latex code is given at the end (note: you can also read and copy it at ShareLatex:TikzMatrixOverlay). There are two tikz matrices and the first one without overlay is for comparison. The second matrix with overlay is not satisfying in the following ways:

  1. The |[red]| instruction for the (2,1) cell (second row, first column) does not work; notice that the |[red]| instruction for the (3,2) cell does work.
  2. I cannot replace the \only<4>{4444 & 4444 & 4444} \\ by \only<4>{4444 & 4444 & 4444 \\}. Otherwise, I will get the Missing \endgroup inserted error.
  3. If I use \only<4>{4444 & 4444 & 4444} \\ (as the source code indicated), an extra empty cell (in the fourth row) is shown during the overlay.

Notice that you can also see the comments in source code and the image below for the three problems. tikz-matrix-overlay

Problem: Therefore, my problem is how to fix these three problems. Specifically,

  1. To enable the |[red]| instruction for the (2,1) cell;
  2. To eliminate the extra empty cell (in the fourth row) during the overlay;
  3. To overlay the tikz matrix row by row.
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{frame}[fragile]{TikzMatrixOverlay}
  \begin{tikzpicture}

  % matrix without overlay for comparison
  \matrix [align = center, matrix of nodes, nodes = {text width = 16mm, draw}]
  {
    1 & 1 & 1 \\
    |[red]| 22 & 22 & 22 \\
    333 & |[red]| 333 & 333 \\
    4444 & 4444 & 4444 \\
  };

  % matrix with overlay
  \matrix [align = center, xshift = 6.0cm, matrix of nodes, nodes = {text width = 16mm,   draw}]
  {
    1 & 1 & 1 \\
    \only<2->{|[red]| 22 & 22 & 22 \\} % Problem 1: the |[red]| instruction does not work.
    \only<3->{333 & |[red]| 333 & 333 \\} % this |[red]| instruction does work.
    \only<4>{4444 & 4444 & 4444} \\   % Problem 2: it cannot be: \only<4>{4444 & 4444 &     4444 \\}
  };
  \end{tikzpicture}
\end{frame}

\end{document}
share|improve this question
1  
Take a look at the approach described here and use the visible on<> style to always draw all elements, but hide those that should not yet be visible. –  Daniel Jan 13 at 6:36
1  
I suspect that the problem is that TikZ looks for the | character as the first token in a cell and only interprets |[red|| as a style in that case. So in the second matrix, it sees the \only and so figures that there aren't any style options. Try putting the style option before the \only. I think I would also confine \only to a cell-by-cell basis on that last row. –  Loop Space Jan 13 at 8:01
    
@AndrewStacey Thx. Putting the style option before the \only (i.e., |[red]| \only<2->{22 & 22 & 22 \\}) works for the (2,1) cell. However, what is the meaning of confining \only to a cell-by-cell basis? Do you mean \only<4>{4444} & \only<4>{4444} & \only<4>{4444} \\ ? This does not help. And how about the '\\ '?There is still an extra empty cell during the overlay (and it is red on the second slide). You can check the SharedLatex code here. –  hengxin Jan 13 at 8:47
add comment

2 Answers

up vote 7 down vote accepted

Very recently, yesterday, I sent to CTAN a new package called aobs-tikz which is designed exactly for these jobs. It is based on Daniel's method illustrated in Mindmap tikzpicture in beamer (reveal step by step) and it extends the styles I defined in Highlighting in Beamer using TikZ nodes.

A preview of its usage:

\documentclass{beamer}
\usepackage{lmodern,tikz}
\usetikzlibrary{overlay-beamer-styles,matrix}

\begin{document}

\begin{frame}[fragile]{TikzMatrixOverlay}
  \begin{tikzpicture}[
  background default draw=black, % define default behaviour
  background default text=black, % define default behaviour
  background default aspect={solid}, % define default behaviour
  highlight/.style={background draw=red, % define modified behaviour
    background text=red, % define modified behaviour
    background aspect=dashed,% define modified behaviour
    },
  ]

  % matrix without overlay for comparison
  \matrix [align = center, matrix of nodes, nodes = {text width = 16mm, draw},]
  {
    1 & 1 & 1 \\
    |[red]| 22 & 22 & 22 \\
    333 & |[red]| 333 & 333 \\
    4444 & 4444 & 4444 \\
  };

  % matrix with overlay
  \matrix [align = center, xshift = 6.0cm, matrix of nodes, nodes = {text width = 16mm,draw},
  row 4/.style={visible on=<3>}% original visible on style by Daniel
  ]{
    1 & 1 & 1 \\
    |[highlight, draw on=<1->, text on=<1->, aspect on=<3>]| 22 & 22 & 22 \\ % new styles
    333 & |[highlight, draw on=<2->, text on=<2->, aspect on=<3>]| 333 & 333 \\ % new styles
    4444 & 4444 & 4444\\
  };

  \end{tikzpicture}  
\end{frame}

\end{document}

enter image description here

share|improve this answer
    
The dashed path in the last overlay has been put only for demonstration purposes. –  Claudio Fiandrino Jan 13 at 11:35
    
Do you have to put the visible on key on every cell, or can you set it for a row at a time (perhaps using the relevant style on the matrix?)? Also, I strongly suspect that your final row is there all the time, something that might make a difference if wanting to draw an arrow to the lowest row. Nevertheless, it looks a nice package and one that could replace many of my own hacks! –  Loop Space Jan 13 at 12:12
    
@AndrewStacey: true, of course it also works with row 4/.style={visible on=<3>} (just too lazy for checking the correct name of the matrix style). True also the second point: as Daniel explained, the visible on key let the drawing to be present in each overlay. It sets its opacity to 0 for when the drawing needs to be invisible, otherwise to 1. Incidentally, this also prevents the "jumping effect". –  Claudio Fiandrino Jan 13 at 13:30
    
@ClaudioFiandrino What is the "jumping effect"? –  hengxin Jan 14 at 11:02
    
@hengxin: it happens when in different overlays the same element (can be text or pictures) moves. It has been discussed some times here; while answering Beamer: problematic use of \visible and \only in combination with TikZ to draw a graph, I collected the most relevant Q/A. –  Claudio Fiandrino Jan 14 at 11:14
add comment

The difficulty is that TikZ doesn't do any expansion when it tests to see how a cell starts, or if a cell exists. So all the \onlys are taken as meaning "Here's a new cell" even if they result in no content. Moreover, they are taken as the start of the cell and mean that the test for a style option, such as |[red]|, is taken to have failed. Also, since a TikZ matrix has to end with \\, anything after it is taken as a new row and this is why the empty square is there. One solution is to ensure that all the \onlys don't occur at the start of a cell. It's not what I would call a perfect solution, but it works in this situation. Essentially, we treat rows as starting with the \\ on the previous row. This ensures that when \\ is seen then the \only has already been expanded and all the issues softly and silently vanish away. (One has to be a bit careful with whitespace with this solution.)

\documentclass{beamer}
%\url{http://tex.stackexchange.com/q/153782/86}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{frame}[fragile]{TikzMatrixOverlay}
  \begin{tikzpicture}[baseline = 0pt]

  % matrix without overlay for comparison
  \matrix [anchor=north,align = center, matrix of nodes, nodes = {text width = 16mm, draw}]
  {
    1 & 1 & 1 \\
    |[red]| 22 & 22 & 22 \\
    333 & |[red]| 333 & 333 \\
    4444 & 4444 & 4444 \\
  };

  % matrix with overlay
  \matrix [anchor = north, align = center, xshift = 6.0cm, matrix of nodes, nodes = {text width = 16mm,   draw}]
  {
    1 & 1 & 1\only<2->{\\
    |[red]| 22 & 22 & 22}% Problem 1: the |[red]| instruction does not work.
    \only<3->{\\ 333 & |[red]| 333 & 333}% this |[red]| instruction does work.
    \only<4>{\\ 4444 & 4444 & 4444}\\   % Problem 2: it cannot be: \only<4>{4444 & 4444 &     4444 \\}
  };
  \end{tikzpicture}
\end{frame}

\end{document}
share|improve this answer
    
This trick works for me. Thx for your detailed explanation. I will accept your answer if no "perfect" solution appears in one or two days. –  hengxin Jan 13 at 9:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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