Inserting an image properly in a table
Similar to the solution I provided in How to draw arrows between parts of an equation to show the Math Distributive Property (Multiplication)?, you can use \tikzmark
as defined in Adding a large brace next to a body of text. Each endpoint of the line is identified by \tikzmark
, and the \DrawLines
macro draws the line between each of the nodes.
You can adjust the shorten <=<size>
parameter to start the lines a little later which achieves a nice effect. Here is the result with shorten <=7pt
:
Curved lines can also be obtained by specifying in=<angle>
and out=<angle>
options. For example using \DrawLinesCurved
instead of DrawLines
you get:
Notes:
- This requires two runs. Once to determine the begin and end point of the lines, and the second to draw them.
- The colors of the 4 lines are passed as parameters to
\DrawLines
. - I used the
-latex
arrows here, but if arrows are not desired this option can be removed. - I used
\raisebox
to adjust the position of the text (as requested in the comments).
Code:
\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\usepackage{graphicx}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawLinesCurved}[4]{%
\begin{tikzpicture}[overlay,remember picture,-latex,shorten >=1pt,shorten <=1pt, thick]
\draw[out=80, in=180, #1] (a.north) to (b.north west);
\draw[out=40, in=180, #2] (a.north) to (c.north west);
\draw[out=320, in=180, #3] (a.north) to (d.north west);
\draw[out=275, in=180, #4] (a.north) to (e.north west);
\end{tikzpicture}
}
\newcommand{\DrawLines}[4]{%
\begin{tikzpicture}[overlay,remember picture,-latex,shorten >=1pt,shorten <=1pt, thick]
\draw[#1] (a.north) to (b.north west);
\draw[#2] (a.north) to (c.north west);
\draw[#3] (a.north) to (d.north west);
\draw[#4] (a.north) to (e.north west);
\end{tikzpicture}
}
\pagestyle{empty}
\begin{document}
\setlength{\extrarowheight}{5pt}
\raisebox{-12pt}{Foo\tikzmark{a}}\hspace*{1.0cm}
\begin{tabular}[c]{ c c c c c c }
& A & B & C & D \\ \cline {2-6}
\tikzmark{b}1 & foo A1 & foo B1 & foo C1 & foo D1 \\
\tikzmark{c}2 & foo A2 & foo B2 & foo C2 & foo D2 \\
\tikzmark{d}3 & foo A3 & foo B3 & foo C3 & foo D3 \\
\tikzmark{e}4 & foo A4 & foo B4 & foo C4 & foo D4 \\
\end{tabular}
\DrawLines{red}{blue}{green}{orange}
\end{document}
\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array,calc,pict2e}
\pagestyle{empty}
\begin{document}
\setlength{\extrarowheight}{5pt}
\setlength\unitlength{\baselineskip + \extrarowheight}
\begin{tabular}{ c c c c c c }
& & A & B & C & D \\ \cline {2-6}
\multirow{4}{*}{\begin{picture}(1,1)
\put(.5,.5){\makebox(0,0)[r]{foo }}
\put(.5,.5){\vector(2,3){1}}
\put(.5,.5){\vector(2,1){1}}
\put(.5,.5){\vector(2,-1){1}}
\put(.5,.5){\vector(2,-3){1}}
\end{picture}} & 1 & foo A1 & foo B1 & foo C1 & foo D1 \\
& 2 & foo A2 & foo B2 & foo C2 & foo D2 \\
& 3 & foo A3 & foo B3 & foo C3 & foo D3 \\
& 4 & foo A4 & foo B4 & foo C4 & foo D4 \\
\end{tabular}
\end{document}
Just to complement Peter's nice answer, I have used the matrix
library. This might be of use since you have already typed in the tabular format so I just copied and pasted it.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes] (footable) {
&A & B & C & D \\
1 & foo A1 & foo B1 & foo C1 & foo D1 \\
2 & foo A2 & foo B2 & foo C2 & foo D2 \\
3 & foo A3 & foo B3 & foo C3 & foo D3 \\
4 & foo A4 & foo B4 & foo C4 & foo D4 \\
};
\draw (footable-2-1.north west) -- (footable-2-5.north east);
\node (Foo) at ([xshift=-1cm]$(footable-3-1.west)!0.5!(footable-4-1.west)$) {Foo};
\foreach \x in {2,...,5}{
\draw (Foo) -- (footable-\x-1);%replace (Foo) with (Foo.east) to focus the arrow origins
}
\end{tikzpicture}
\end{document}
So, basically matrix
library allows us to type in the tabular data. Then we put a node of which coordinates are calculated by obtaining the middle point of 3rd and 4th rows and shifting it by -1cm
on the horizontal axis via calc
library. Then a foreach
argument draws the lines.