9

I'd like to insert those lines that from "Foo" point to the four different rows in the table.

enter image description here

I've tries inserting an image but it doesn't come out right (please ignore the bad quality of the lines and the table not resembling the one above):

enter image description here

Here's the minimal example:

\documentclass[12pt]{book}

\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}

\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}

\pagestyle{empty}

\begin{document}

    \setlength{\extrarowheight}{5pt}

    \begin{tabular}{ c c c c c c }

        & & A & B & C & D \\ \cline {2-6}
    \multirow{4}{*}{Foo \includegraphics[scale=0.5]{IMAGE HERE}}    & 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}

Obviously, if you know of a "Tex-native" way to do this without using images, which quality will inevitably go poor when inserted, that would be a perfect solution.

David Carlisle
  • 757,742
Alenanno
  • 37,338

3 Answers3

12

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.

enter image description here

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:

enter image description here

Curved lines can also be obtained by specifying in=<angle> and out=<angle> options. For example using \DrawLinesCurved instead of DrawLines you get:

enter image description here

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}

Peter Grill
  • 223,288
  • This is perfect! Just one thing: is it possible to make it so that "Foo" stays in the middle (when using multirow) so that the arrows come out symmetrical? :D It's just a "visual thing", as I think your answer pretty much does what I need. :D – Alenanno Feb 25 '12 at 00:24
  • @Alenanno: I placed the text Foo is outside of the table. The pacement of Foo has nothing to do with this solution. You can move it as desired and the arrows will adjust with respect to where it is placed. – Peter Grill Feb 25 '12 at 00:32
  • Ok, I'll try it and let you know. Another thing, is the color of the arrows editable? By the way, I +1'ed your answer. :) – Alenanno Feb 25 '12 at 00:38
  • @Alenanno: The color of the arrows is specified in the call to \DrawLines. I also updated the solution to show how to move the location of the \Foo text. – Peter Grill Feb 25 '12 at 01:00
  • Last thing and then I'll stop nagging you :D Can I choose to use lines instead of arrows? – Alenanno Feb 25 '12 at 01:27
  • @Alenanno: The arrow type is specified by -latex. You can just remove that option if you want just a line. – Peter Grill Feb 25 '12 at 01:28
  • I forgot to say: thanks again for the quick and complete answer! :) – Alenanno Feb 25 '12 at 02:06
  • Peter, I have a problem. How do you determine the position of that "Foo"? I mean, when you wrote \raisebox{-12pt}{Foo\tikzmark{a}}\hspace*{1.0cm}, was it by tries? Or you can determine that? For example: If we multiply that table by 6 times (always a single tabular environment), how do you know where to "locate" the first Foo, then the second one, the third one and so on? – Alenanno Feb 25 '12 at 12:46
  • I think that would depend on exactly what Foo is. I just moved it until it looked liked it was centered as that was not the main thrust of this problem. Since it is unrelated to the drawing of the lines, it seems to me that you should post a separate question with more details on how you want to determine the location. You can reference this question in your new question if it is relevant. – Peter Grill Feb 25 '12 at 17:47
  • Oh sure. I didn't mean to overcome the SE rules! :D I thought it was something simple enough to be explained in a comment, but if you suggest me to ask another question, I suppose it isn't. :P – Alenanno Feb 25 '12 at 18:50
7

enter image description here

\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}
David Carlisle
  • 757,742
4

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}

enter image description here

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.

David Carlisle
  • 757,742
percusse
  • 157,807
  • Thank you! I have accepted Peter's answer, but I upvoted yours too because it's interesting as well! :) – Alenanno Feb 25 '12 at 01:46