5

I made the following table

enter image description here

in LaTex with the following code:

\documentclass{article}

\begin{document}

\begin{table}[ph]
\protect\caption{Genotype-by-Environment Means}
\centering{}%
\begin{tabular}{l|cccccc|c}
\hline 
 & \multicolumn{6}{c|}{Environment} & \tabularnewline
\cline{2-7} 
Genotype & $1$ & $2$ & $\ldots$ & $j$ & $\ldots$ & $e$ & Mean\tabularnewline
\hline 
$1$ & $\overline{Y}_{11.}$ & $\overline{Y}_{12.}$ & $\ldots$ & $\overline{Y}_{1j.}$ & $\ldots$ & $\overline{Y}_{1e.}$ & $\overline{Y}_{1..}$\tabularnewline
$2$ & $\overline{Y}_{21.}$ & $\overline{Y}_{22.}$ & $\ldots$ & $\overline{Y}_{2j.}$ & $\ldots$ & $\overline{Y}_{2e.}$ & $\overline{Y}_{2..}$\tabularnewline
$\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\vdots$\tabularnewline
$i$ & $\overline{Y}_{i1.}$ & $\overline{Y}_{i2.}$ & $\ldots$ & $\overline{Y}_{ij.}$ & $\ldots$ & $\overline{Y}_{ie.}$ & $\overline{Y}_{i..}$\tabularnewline
$\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\vdots$\tabularnewline
$g$ & $\overline{Y}_{g1.}$ & $\overline{Y}_{g2.}$ & $\ldots$ & $\overline{Y}_{gj.}$ & $\ldots$ & $\overline{Y}_{ge.}$ & $\overline{Y}_{g..}$\tabularnewline
\hline 
Mean & $\overline{Y}_{.1.}$ & $\overline{Y}_{.2.}$ & $\ldots$ & $\overline{Y}_{.j.}$ & $\ldots$ & $\overline{Y}_{.e.}$ & $\overline{Y}_{...}$\tabularnewline
\hline 
\end{tabular}
\end{table}

\end{document}

Now I want to make some drawing on this table as below:

enter image description here

I used tikz for this and the code is:

\documentclass{article} % article standalone 
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{ 
table/.style={
  matrix of math nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,draw=black,text width=3.3em,align=center},
  text depth=0.25ex,
  text height=2ex,
  nodes in empty cells
  }
}

\begin{document}

\begin{table}
\begin{tikzpicture}
\matrix (m) [table] {
    %& K=3 & K=4 & K=5 & K=6 & K=7 & K=8 & K=9 & K=10 & K=11 & K=12\\
    \hline
 1  &  \overline{Y}_{11.}  &  \overline{Y}_{12.}  &  \ldots  &  \overline{Y}_{1j.}  &  \ldots  &  \overline{Y}_{1e.}  &  \overline{Y}_{1..} \\
 2  &  \overline{Y}_{21.}  &  \overline{Y}_{22.}  &  \ldots  &  \overline{Y}_{2j.}  &  \ldots  &  \overline{Y}_{2e.}  &  \overline{Y}_{2..} \\
 \vdots  &  \vdots  &  \vdots  &  \ddots  &  \vdots  &  \ddots  &  \vdots  &  \vdots \\
 i  &  \overline{Y}_{i1.}  &  \overline{Y}_{i2.}  &  \ldots  &  \overline{Y}_{ij.}  &  \ldots  &  \overline{Y}_{ie.}  &  \overline{Y}_{i..} \\
 \vdots  &  \vdots  &  \vdots  &  \ddots  &  \vdots  &  \ddots  &  \vdots  &  \vdots \\
 g  &  \overline{Y}_{g1.}  &  \overline{Y}_{g2.}  &  \ldots  &  \overline{Y}_{gj.}  &  \ldots  &  \overline{Y}_{ge.}  &  \overline{Y}_{g..} \\
\textrm{Mean} &  \overline{Y}_{.1.}  &  \overline{Y}_{.2.}  &  \ldots  &  \overline{Y}_{.j.}  &  \ldots  &  \overline{Y}_{.e.}  &  \overline{Y}_{...} \\
};
\begin{scope}[shorten >= 10pt,shorten <= 10pt]
\draw[red, thick] (m-4-2.west) -- (m-4-8.east);
\end{scope}
\end{tikzpicture}
\end{table}

\end{document}

enter image description here

Any help will be highly appreciated. Thanks


Edited


I want to use this table in beamer. On first frame the table without marker (red in hand made figure) and one second frame the table with marker (red in hand made figure). So what is the best approach. Thanks

MYaseen208
  • 8,587

2 Answers2

5

You are almost there. Use the 4 corners which can be identified as north west north east south east and south west of a cell. Simply select the corresponding cell with care.

enter image description here

Code

\documentclass{article} % article standalone 
\usepackage[papersize={15cm,6cm}]{geometry}

\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\tikzset{ 
table/.style={
  matrix of math nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,draw=black,text width=3.3em,align=center},
  text depth=0.25ex,
  text height=2ex,
  nodes in empty cells
  }
}
\thispagestyle{empty}
\begin{document}

\begin{table}
\begin{tikzpicture}
\matrix (m) [table] {
    %& K=3 & K=4 & K=5 & K=6 & K=7 & K=8 & K=9 & K=10 & K=11 & K=12\\
    \hline
 1  &  \overline{Y}_{11.}  &  \overline{Y}_{12.}  &  \ldots  &  \overline{Y}_{1j.}  &  \ldots  &  \overline{Y}_{1e.}  &  \overline{Y}_{1..} \\
 2  &  \overline{Y}_{21.}  &  \overline{Y}_{22.}  &  \ldots  &  \overline{Y}_{2j.}  &  \ldots  &  \overline{Y}_{2e.}  &  \overline{Y}_{2..} \\
 \vdots  &  \vdots  &  \vdots  &  \ddots  &  \vdots  &  \ddots  &  \vdots  &  \vdots \\
 i  &  \overline{Y}_{i1.}  &  \overline{Y}_{i2.}  &  \ldots  &  \overline{Y}_{ij.}  &  \ldots  &  \overline{Y}_{ie.}  &  \overline{Y}_{i..} \\
 \vdots  &  \vdots  &  \vdots  &  \ddots  &  \vdots  &  \ddots  &  \vdots  &  \vdots \\
 g  &  \overline{Y}_{g1.}  &  \overline{Y}_{g2.}  &  \ldots  &  \overline{Y}_{gj.}  &  \ldots  &  \overline{Y}_{ge.}  &  \overline{Y}_{g..} \\
\textrm{Mean} &  \overline{Y}_{.1.}  &  \overline{Y}_{.2.}  &  \ldots  &  \overline{Y}_{.j.}  &  \ldots  &  \overline{Y}_{.e.}  &  \overline{Y}_{...} \\
};
\begin{scope}[shorten >= 10pt,shorten <= 10pt]
\draw[red, thick] (m-4-2.north west) -- (m-4-8.north east) -- (m-4-8.south east) -- (m-4-2.south west) -- cycle;
\end{scope}
\end{tikzpicture}
\end{table}
\end{document}

Edit: The OP's intention was to draw on the first figure. So this is a solution where 4 tikzmarks defined by a command called tikzmark are assigned to the ith-row and a line with a style is drawn to box the ith row.

enter image description here

Code

\documentclass{article}
\usepackage[papersize={15cm,10cm}]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\tikzset{line/.style ={draw, rounded corners=2pt, line width=1pt}}

\newcommand\tikzmark[1]{%
\tikz[remember picture]  \node[inner sep=0,outer sep=0] (#1){};%
}
\thispagestyle{empty}
\begin{document}

\begin{table}[ph]
\protect\caption{Genotype-by-Environment Means}
\centering{}%
\begin{tabular}{l|cccccc|c}
\hline 
 & \multicolumn{6}{c|}{Environment} & \tabularnewline
\cline{2-7} 
Genotype & $1$ & $2$ & $\ldots$ & $j$ & $\ldots$ & $e$ & Mean\tabularnewline
\hline 
$1$ & $\overline{Y}_{11.}$ & $\overline{Y}_{12.}$ & $\ldots$ & $\overline{Y}_{1j.}$ & $\ldots$ & $\overline{Y}_{1e.}$ & $\overline{Y}_{1..}$\tabularnewline
$2$ & $\overline{Y}_{21.}$ & $\overline{Y}_{22.}$ & $\ldots$ & $\overline{Y}_{2j.}$ & $\ldots$ & $\overline{Y}_{2e.}$ & $\overline{Y}_{2..}$\tabularnewline
$\vdots$& \tikzmark{a} $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\vdots$\tikzmark{b}\tabularnewline
$i$ & \tikzmark{d}$\overline{Y}_{i1.}$ & $\overline{Y}_{i2.}$ & $\ldots$ & $\overline{Y}_{ij.}$ & $\ldots$ & $\overline{Y}_{ie.}$ & $\overline{Y}_{i..}\tikzmark{c}$\tabularnewline
$\vdots$&  $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\vdots$\tabularnewline
$g$ & $\overline{Y}_{g1.}$ & $\overline{Y}_{g2.}$ & $\ldots$ & $\overline{Y}_{gj.}$ & $\ldots$ & $\overline{Y}_{ge.}$ & $\overline{Y}_{g..}$\tabularnewline
\hline 
Mean & $\overline{Y}_{.1.}$ & $\overline{Y}_{.2.}$ & $\ldots$ & $\overline{Y}_{.j.}$ & $\ldots$ & $\overline{Y}_{.e.}$ & $\overline{Y}_{...}$\tabularnewline
\hline 
\end{tabular}
\end{table}
\begin{tikzpicture}[remember picture,overlay]
\draw [red,line]($(a)+(-2.2ex,0)$)--($(b)+(2.5ex,0)$)--($(c)+(0.9ex,-1ex)$) -- ($(d)+(-0.9ex,-1ex)$)--cycle;
\end{tikzpicture}
\end{document}
Tobi
  • 56,353
Jesse
  • 29,686
  • Thanks @Jesse for your answer. I actually want to get the table exactly like figure 1 with box on i-th row. Any help will be highly appreciated. Thanks – MYaseen208 Feb 14 '14 at 12:17
  • @MYaseen208 -- Thank you for your patient. My second attempt is shown/appended for your reference. – Jesse Feb 14 '14 at 13:28
  • For jesse and @Tobi: I got one issue with your code. For different compilations Red box changes its place. – MYaseen208 Feb 14 '14 at 13:44
  • @MYaseen208 -- Try using article class to compile. To save space, I use small page size area shown on the second line. ie, under normal/usual condition and test. – Jesse Feb 14 '14 at 13:51
5

Same idea, but using the tikzmark library (this allows you to easily place some filling color in the background if desired); I also used booktabs to give a more professional look to the table. The overlay is simply achieved using the fact that \draw is overlay aware. The code needs three runs to stabilize:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{booktabs}
\usetikzlibrary{tikzmark}

\newcommand<>\FramedBox[3][]{%
\draw#4[red!80!black,rounded corners,fill=red!10,#1] 
  ([xshift=-1ex,yshift=3ex]pic cs:#2) 
    rectangle 
  ([xshift=1ex,yshift=-1ex]pic cs:#3);
}

\begin{document}

\begin{frame}
\begin{tikzpicture}[remember picture,overlay]
\FramedBox<2>{starta}{enda}
\FramedBox<3>{startc}{endc}
\FramedBox<4>[draw=cyan,fill=cyan!20]{startb}{endb}
\end{tikzpicture}

\begin{table}
\caption{Genotype-by-Environment Means}
\centering{}%
\begin{tabular}{lccccccc}
\toprule 
 & \multicolumn{6}{c}{Environment} & 
\tabularnewline
\cmidrule{2-7} 
Genotype & $1$ & $2$ & $\ldots$ & $j$ & $\ldots$ & $e$ & Mean
\tabularnewline
\midrule
$1$ & $\overline{Y}_{11.}$ & $\overline{Y}_{12.}$ & $\ldots$ 
  & \tikzmark{startb}$\overline{Y}_{1j.}$ & $\ldots$ & $\overline{Y}_{1e.}$ & $\overline{Y}_{1..}$
\tabularnewline
$2$ & $\overline{Y}_{21.}$ & $\overline{Y}_{22.}$ & $\ldots$ 
  & $\overline{Y}_{2j.}$ & $\ldots$ & $\overline{Y}_{2e.}$ & $\overline{Y}_{2..}$
\tabularnewline
$\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ 
  & $\vdots$ & $\vdots$
\tabularnewline
$i$ & \tikzmark{starta}$\overline{Y}_{i1.}$ & $\overline{Y}_{i2.}$ 
  & $\ldots$ & $\overline{Y}_{ij.}$ & $\ldots$ & $\overline{Y}_{ie.}$ 
  & $\overline{Y}_{i..}$ \tikzmark{enda} 
\tabularnewline
$\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ 
  & $\vdots$ & $\vdots$
\tabularnewline
$g$ & $\overline{Y}_{g1.}$ & $\overline{Y}_{g2.}$ & $\ldots$ 
  & $\overline{Y}_{gj.}$ & $\ldots$ & $\overline{Y}_{ge.}$ & $\overline{Y}_{g..}$
\tabularnewline
\midrule
Mean & $\overline{Y}_{.1.}$ & \tikzmark{startc}$\overline{Y}_{.2.}$\tikzmark{endc} & $\ldots$ 
  & $\overline{Y}_{.j.}$\tikzmark{endb} & $\ldots$ & $\overline{Y}_{.e.}$ & $\overline{Y}_{...}$
\tabularnewline
\bottomrule 
\end{tabular}
\end{table}

\end{frame}

\end{document}

enter image description here

Using \tikzmark you place two marks for each frame; one where the frame should begin and the other one where it should end. Then you can simply use the command \FramedBox inside a tikzpicture environment to draw the frames; the syntax for the command is:

\FrameBox[<options for the frame>]{<start mark>}{<end mark>}

\FramedBox is overlay aware, as the example code illustrates.

Gonzalo Medina
  • 505,128
  • (+1): Thanks @Gonzalo for your nice answer. Could you help me too if I have to select also another cell too. Thanks – MYaseen208 Mar 02 '14 at 06:08
  • @MYaseen208 Which other cell? – Gonzalo Medina Mar 02 '14 at 06:08
  • Any other cell. For example, in last row second column element. – MYaseen208 Mar 02 '14 at 06:13
  • @MYaseen208 Sure. Please see my updated answer. – Gonzalo Medina Mar 02 '14 at 06:19
  • Simply awesome. Thanks a lot @Gonzalo. Much appreciated. Will award the bounty. – MYaseen208 Mar 02 '14 at 06:34
  • @MYaseen208 You're welcome! Glad I could help. – Gonzalo Medina Mar 02 '14 at 06:37
  • 1
    @MYaseen208 I did some modifications to the code that could be of ineterest for you. – Gonzalo Medina Mar 03 '14 at 02:45
  • Thanks @Gonzalo for your help. Would you elaborate which modifications you have made? Could you help me to get the same table with boxes in article documentclass. If you think I can ask separate question. Thanks again for your help. – MYaseen208 Mar 03 '14 at 13:42
  • 1
    @MYaseen208 I changed the shifts used for the start and end marks in the definition of \FramedBox; as they were originally defined they produced the wrong result for vertical boxes. Now they are corrected. Regarding your question with the article document class, do you need the filling for the frames or just to draw the borders? – Gonzalo Medina Mar 03 '14 at 16:13
  • I think boxes with borders are sufficient. Thanks – MYaseen208 Mar 03 '14 at 16:29
  • @MYaseen208 this is the \FramedBox definition for article document class: \newcommand\FramedBox[3][]{ \draw[red!80!black,rounded corners,#1] ([xshift=-1ex,yshift=3ex]pic cs:#2) rectangle ([xshift=1ex,yshift=-1ex]pic cs:#3); } the marks are placed in exactly the same way using \tikzmar{<mark>} and this is how you would draw the boxes (without overlay specifications): \begin{tikzpicture}[remember picture,overlay] \FramedBox{starta}{enda} \FramedBox{startc}{endc} \FramedBox[draw=cyan]{startb}{endb} \end{tikzpicture} – Gonzalo Medina Mar 03 '14 at 16:35
  • Thanks again @Gonzalo for your help. Your given code is working but in big document sometimes the boxes are not appearing on correct places. Any solution. Thanks – MYaseen208 Mar 03 '14 at 16:51
  • @MYaseen208 Try adding the \begin{tikzpicture}...\end{tikzpicture} part inside the table environment. If this doesn't solve the problem, please compose a minimal document illustrating the problem and open a fresh new question as a follow-up to this one. – Gonzalo Medina Mar 03 '14 at 17:06
  • Hello @Gonzalo, I've asked the follow up question here. – MYaseen208 Mar 04 '14 at 12:53