First of all, as you're using matrix of nodes
, you don't need to specify node
s manually. each node
is only as big as it needs to be, so one (far from elegent or automatic way) is to specify minimum height
s and minimum width
s:
Code
\documentclass{beamer}
\usepackage{txfonts}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{frame}[fragile]{sample failure}
\begin{minipage}{\linewidth}
\begin{center}
\begin{tikzpicture}
\matrix
[ matrix of nodes,
row sep = 0.1cm,
every even row/.style = {nodes={fill=blue!50}},
every odd row/.style = {nodes={fill=red!50}},
nodes={anchor=west},
column 1/.style = {nodes={minimum width=2.8cm}},
column 2/.style = {nodes={minimum width=1.7cm}},
column 3/.style = {nodes={minimum width=4.5cm}},
row 1/.style={nodes={minimum height=1.2cm}},
row 2/.style={nodes={minimum height=1.8cm}},
row 3/.style={nodes={minimum height=1.2cm}},
row 4/.style={nodes={minimum height=0.8cm}},
]
{ blah1 & $\sum_{i=1}^n i $ & \begin{tabular}{c}line1 \\ line2 \end{tabular} \\
blah & $\sum_{i=1}^n \frac{a_i}{b_i}$ & \begin{tabular}{c}expalination 2\\ exp 3 \\ exp 4\end{tabular} \\
blah and more & $\log ^{d+4} \frac{M}{\epsilon}$ & \begin{tabular}{c}no idea\\ and more \end{tabular} \\
blah4 & $\log ^{d-1} \frac{M}{\epsilon}$ & \begin{tabular}{c}blah blah and more info\end{tabular} \\
};
\end{tikzpicture}
\end{center}
\end{minipage}
\end{frame}
\end{document}
Output
![enter image description here](https://i.stack.imgur.com/qGvkz.png)
Edit 1: here's a version requiring much less tweaking: it determines heights and width automatically. You only need to manually pass the matrix name, the number of rows and colums and the colors you'd like to use to \colorfillrows
. I borrowed the [on background layer]
from Ignasi, thanks for that:
Code
\documentclass{beamer}
\usepackage{txfonts}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,backgrounds}
\usepackage{xifthen}
\newcommand{\getdim}[1]% name of node
{ \path (#1.south west);
\pgfgetlastxy{\xsw}{\ysw}
\path (#1.north east);
\pgfgetlastxy{\xne}{\yne}
\pgfmathsetlengthmacro{\nodewidth}{\xne-\xsw}
\pgfmathsetlengthmacro{\nodeheight}{\yne-\ysw}
\xdef\widthofnode{\nodewidth}
\xdef\widthofnode{\nodeheight}
}
\newcommand{\colorfillrows}[5]% 1:matrixname, 2:numrows, 3:numcols, 4:oddcolor, 5:evencolor
{ \xdef\maxwidth{0}
\foreach \myrow in {1,...,#2}
{ \getdim{#1-\myrow-#3}
\pgfmathsetmacro{\biggerwidth}{\nodewidth>\maxwidth ? \nodewidth : \maxwidth}
\xdef\maxwidth{\biggerwidth}
}
\foreach \myrow in {1,...,#2}
{ \xdef\maxheight{0}
\xdef\myfit{}
\foreach \mycol in {1,...,#3}
{ \getdim{#1-\myrow-\mycol}
\pgfmathsetmacro{\biggerheight}{\nodeheight>\maxheight ? \nodeheight : \maxheight}
\xdef\maxheight{\biggerheight}
\xdef\myfit{\myfit(#1-\myrow-\mycol)}
}
\node[right,minimum width=\maxwidth,inner sep=0] (extra-\myrow) at (#1-\myrow-#3.west) {};
\begin{scope}[on background layer]
\pgfmathtruncatemacro{\oddrow}{mod(\myrow,2)}
\ifthenelse{\oddrow=1}{\xdef\mycolor{#4}}{\xdef\mycolor{#5}}
\node[inner sep=2pt,fit=\myfit(extra-\myrow),fill=\mycolor,inner sep=0pt] {};
\end{scope}
}
}
\begin{document}
\begin{frame}[fragile]{sample almost automatic success}
\begin{minipage}{\linewidth}
\begin{center}
\begin{tikzpicture}
\matrix (mydata)
[ matrix of nodes,
row sep = 0.1cm,
nodes={anchor=west},
]
{ blah1 & $\sum_{i=1}^n i $ & \begin{tabular}{c}line1 \\ line2 \end{tabular} \\
blah & $\sum_{i=1}^n \frac{a_i}{b_i}$ & \begin{tabular}{c}expalination 2\\ exp 3 \\ exp 4\end{tabular} \\
blah and more & $\log ^{d+4} \frac{M}{\epsilon}$ & \begin{tabular}{c}no idea\\ and more \end{tabular} \\
blah4 & $\log ^{d-1} \frac{M}{\epsilon}$ & \begin{tabular}{c}blah blah and more info\end{tabular} \\
};
\colorfillrows{mydata}{4}{3}{orange!55}{blue!30!cyan}% 1:matrixname, 2:numrows, 3:numcols, 4:oddcolor, 5:evencolor
\end{tikzpicture}
\end{center}
\end{minipage}
\end{frame}
\end{document}
Output
![enter image description here](https://i.stack.imgur.com/i3mjp.png)
I am definitely unfamiliar with both beamer
and tikz
(do not quite get what the \only
are supposed to do) but perhaps this could go in the direction you want:
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{chains}
\newcounter{count}
% helper macro:
\long\def\GobToSemiColon #1;{}
\newcommand\myPicture{
\begin{tikzpicture}
\begin{scope}[start chain = going below]
\ifnum\value{count}<1 \expandafter\GobToSemiColon\fi
\ifnum\value{count}>3 \expandafter\GobToSemiColon\fi
\node[draw, rectangle, on chain] {display only when counter is between
1 and 3};
\ifnum\value{count}>-1 \expandafter\GobToSemiColon\fi
\node[draw, rectangle, on chain] {display only when counter is
negative};
\ifnum\value{count}<100 \expandafter\GobToSemiColon\fi
\ifnum\value{count}>200 \expandafter\GobToSemiColon\fi
\node[draw, rectangle, on chain] {display only if counter is between
100 and 200};
\ifnum\value{count}<3 \expandafter\GobToSemiColon\fi
\ifnum\value{count}>20 \expandafter\GobToSemiColon\fi
\node[draw, circle, on chain] {only when counter is in the range 3 to 20};
\end{scope}
\end{tikzpicture}
}
\begin{document}
\begin{frame}
\only{\setcounter{count}{-3}\myPicture}
\only{\setcounter{count}{105}\myPicture}
\only{\setcounter{count}{39}\myPicture}
\only{\setcounter{count}{2}\myPicture}
\only{\setcounter{count}{5}\myPicture}
\end{frame}
\end{document}
![beamer](https://i.stack.imgur.com/h1oev.png)
Best Answer
You can use the
/.list
feature John Kormylo mentioned in his answer to repeat a style for different inputs.