# Setting the position of an element using Tikz

Recently I've asked a question about "Inserting an image properly in a table", and I got nice answers. I decided to adopt the accepted one but now I have another problem. I thought it was a minor issue, but I decided to ask another question after being directed by the answerer.

Previously I asked about making a table like this (image) and everything worked perfectly.

But my aim, which I didn't specify in that question was to have that "in series". Basically, now I'd like to repeat that 6 times, but using two lines only. Something like this (The grey areas are for helping you distinguish the divisions):

Unfortunately, this is what I get:

My question is: Do I need to set the position of those "Foo" manually by trying and trying again (for infinite times) until I get them right? Isn't there a more precise way, so to speak?

The code (for the last example) is the following:

\documentclass[12pt]{book}

\usepackage[utf8]{inputenc}
\usepackage[a4paper,landscape]{geometry}
\usepackage{graphicx}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newcommand{\DrawLines}[2]{%
\begin{tikzpicture}[overlay,remember picture,shorten >=1pt,shorten <=2pt, thick]
\draw[#1] (a.north) to (b.north west);
\draw[#2] (a.north) to (c.north west);
\draw[#1] (d.north) to (e.north west);
\draw[#2] (d.north) to (f.north west);
\draw[#1] (g.north) to (h.north west);
\draw[#2] (g.north) to (i.north west);
\draw[#1] (l.north) to (m.north west);
\draw[#2] (l.north) to (n.north west);
\draw[#1] (o.north) to (p.north west);
\draw[#2] (o.north) to (q.north west);
\draw[#1] (r.north) to (s.north west);
\draw[#2] (r.north) to (t.north west);
\end{tikzpicture}
}

\pagestyle{empty}
\begin{document}

\setlength{\extrarowheight}{5pt}
\raisebox{12pt}{Foo\tikzmark{a}}\hspace*{1.0cm}
\raisebox{24pt}{Foo\tikzmark{d}}\hspace*{1.0cm}
\raisebox{-10pt}{Foo\tikzmark{g}}\hspace*{1.0cm}
\raisebox{-48pt}{Foo\tikzmark{l}}\hspace*{1.0cm}
\raisebox{0pt}{Foo\tikzmark{o}}\hspace*{1.0cm}
\raisebox{-48pt}{Foo\tikzmark{r}}\hspace*{1.0cm}

\begin{tabular}{ r l l l l l }
& 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{e}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{f}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\tikzmark{h}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{i}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\tikzmark{m}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{n}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\tikzmark{p}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{q}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\tikzmark{s}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{t}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\end{tabular}
\DrawLines{black}{black}
\end{document}

-
By the way, I just realized I didn't fix the columns properly! I can do it by myself, should I edit them before someone answers? –  Alenanno Feb 25 '12 at 19:50
Fix it in your code; I've fixed it in mine already. –  Alan Munn Feb 25 '12 at 20:03
@AlanMunn Fixed! :) –  Alenanno Feb 25 '12 at 20:08

It should be noted that the positions of the text in question is not at all related to TikZ.

The multirow solution provided by @AlanMunn is probably a simpler approach for the placement of the Foo text, another approach would be to do a separate table for the foo columns.

However, now that I know how you actually want to use it, I would also recommend changing the syntax of \DrawLines to take 4 parameters:

\DrawLines[<color>]{<left point>}{<right point 1>}{<right point 2>}


where the first parameter is now optional (defaults to black if not specified). The other three parameters specify the start point and the two end points. With this syntax as you add more rows you won't need to adjust the \DrawLines macro, and you can choose the styles separately for each point.

The name given to a \tikxmark is arbitrary (a period will cause problems), so I have used Foo<letter> to label the foo points. You should use something that is more meaningful.

The default behavior is the first case below, the others are just to illustrate some of the options:

## Code:

\documentclass[12pt]{book}

\usepackage[utf8]{inputenc}
\usepackage[a4paper,landscape]{geometry}
\usepackage{graphicx}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newcommand{\DrawLines}[4][]{%
\begin{tikzpicture}[overlay,remember picture,shorten >=1pt,shorten <=1pt, thick]
\draw[black,#1] (#2.north) to (#3.north west);
\draw[black,#1] (#2.north) to (#4.north west);
\end{tikzpicture}
}

\pagestyle{empty}
\begin{document}

\setlength{\extrarowheight}{5pt}
\noindent
\begin{tabular}{ r}
\\\\
Foo\tikzmark{Fooa}\\\\
Foo\tikzmark{Foob}\\\\
Foo\tikzmark{Fooc}\\\\
Foo\tikzmark{Food}\\\\
Foo\tikzmark{Fooe}\\\\
Foo\tikzmark{Foof}\\\\
\end{tabular}
%
\hspace*{1.0cm}%
%
\begin{tabular}{ r l l l l l }
& A & B & C & D \\ \cline {2-6}
\tikzmark{a}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{b}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\tikzmark{c}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{d}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\tikzmark{e}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{f}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\tikzmark{g}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{h}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\tikzmark{i}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{j}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\tikzmark{k}1 & foo A1 & foo B1 & foo C1 & foo D1  \\
\tikzmark{l}2 & foo A2 & foo B2 & foo C2 & foo D2  \\
\end{tabular}
%
\DrawLines{Fooa}{a}{b}% default behavior
\DrawLines[red,  thick         ]{Foob}{c}{d}
\DrawLines[blue, thick, ->     ]{Fooc}{e}{f}
\DrawLines[green, thick, -latex]{Food}{g}{h}
\DrawLines[brown, dashed       ]{Fooe}{i}{j}
\DrawLines[thick, dotted       ]{Foof}{k}{l}
\end{document}

-

I would use the multirow package for placing the initial text that spans two rows.

\documentclass[12pt]{book}

\usepackage[utf8]{inputenc}
\usepackage[a4paper,landscape]{geometry}
\usepackage{graphicx}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\usepackage{multirow}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newcommand{\DrawLines}[2]{%
\begin{tikzpicture}[overlay,remember picture,shorten >=1pt,shorten <=2pt, thick]
\draw[#1] (a.north) to (b.north west);
\draw[#2] (a.north) to (c.north west);
\draw[#1] (d.north) to (e.north west);
\draw[#2] (d.north) to (f.north west);
\draw[#1] (g.north) to (h.north west);
\draw[#2] (g.north) to (i.north west);
\draw[#1] (l.north) to (m.north west);
\draw[#2] (l.north) to (n.north west);
\draw[#1] (o.north) to (p.north west);
\draw[#2] (o.north) to (q.north west);
\draw[#1] (r.north) to (s.north west);
\draw[#2] (r.north) to (t.north west);
\end{tikzpicture}
}

\pagestyle{empty}
\begin{document}

\begin{tabular}{p{.75in}lllll}

&  &  A  &  B  &  C  &  D  \\
\multirow{2}{*}{foo\tikzmark{a}} &     \tikzmark{b}1  &  foo A1  &  foo B1  &  foo C1  &  foo D1  \\
&     \tikzmark{c}2  &  foo A2  &  foo B2  &  foo C2  &  foo D2  \\
\multirow{2}{*}{foo\tikzmark{d}} &     \tikzmark{e}1  &  foo A1  &  foo B1  &  foo C1  &  foo D1  \\
&     \tikzmark{f}2  &  foo A2  &  foo B2  &  foo C2  &  foo D2  \\
\multirow{2}{*}{foo\tikzmark{g}} &     \tikzmark{h}1  &  foo A1  &  foo B1  &  foo C1  &  foo D1  \\
&     \tikzmark{i}2  &  foo A2  &  foo B2  &  foo C2  &  foo D2  \\
\multirow{2}{*}{foo\tikzmark{l}} &     \tikzmark{m}1  &  foo A1  &  foo B1  &  foo C1  &  foo D1  \\
&    \tikzmark{n}2  &  foo A2  &  foo B2  &  foo C2  &  foo D2  \\
\multirow{2}{*}{foo\tikzmark{o}} &     \tikzmark{p}1  &  foo A1  &  foo B1  &  foo C1  &  foo D1  \\
&     \tikzmark{q}2  &  foo A2  &  foo B2  &  foo C2  &  foo D2  \\
\multirow{2}{*}{foo\tikzmark{r}} &     \tikzmark{s}1  &  foo A1  &  foo B1  &  foo C1  &  foo D1  \\
&     \tikzmark{t}2  &  foo A2  &  foo B2  &  foo C2  &  foo D2  \\

\end{tabular}

\DrawLines{black}{black}
\end{document}


-
You know what? I tried using multirow, but I didn't succeed in making it work. Perhaps I gave up too soon! :P –  Alenanno Feb 25 '12 at 20:15
Alan, I accepted the other answer because it seemed a more complete solution, but I upvoted your answer because it was useful too. :) –  Alenanno Feb 26 '12 at 16:52

Since Alan and Peter has already answered the question quite nicely, I can put my matrix solution just for the fun of it (Yes, again! :P)

\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  \\
1 & foo A1 & foo B1 & foo C1 & foo D1  \\
2 & foo A2 & foo B2 & foo C2 & foo D2  \\
1 & foo A1 & foo B1 & foo C1 & foo D1  \\
2 & foo A2 & foo B2 & foo C2 & foo D2  \\
1 & foo A1 & foo B1 & foo C1 & foo D1  \\
2 & foo A2 & foo B2 & foo C2 & foo D2  \\
1 & foo A1 & foo B1 & foo C1 & foo D1  \\
2 & foo A2 & foo B2 & foo C2 & foo D2  \\
1 & foo A1 & foo B1 & foo C1 & foo D1  \\
2 & foo A2 & foo B2 & foo C2 & foo D2  \\
};
\draw (footable-2-1.north west) -- (footable-2-5.north east);
\foreach \x in {2,4,...,12}{
\pgfmathtruncatemacro{\xinc}{\x+1}
\node (Foo\x)  at ([xshift=-15mm]$(footable-\x-1.west)!0.5!(footable-\xinc-1.west)$) {Foo};%*
\draw (Foo\x.east) -- (footable-\x-1.west);
\draw (Foo\x.east) -- (footable-\xinc-1.west);
};
\end{tikzpicture}
\end{document}


Replace {Foo} with {\Large Foo} for larger node text at the line marked with %*. Also, you can include Peter's beautiful style choices in the foreach loop to furnish the lines further.

-