[Tex/LaTex] Setting the position of an element using Tikz

positioningtablestikz-pgf

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):

enter image description here

Unfortunately, this is what I get:

enter image description here

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}

Best Answer

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:

enter image description here

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}