3

I try to connect two arbitrary tables with arrows. The code example below should demonstrate how: say we want to connect the "1" in A with the "5" in B, the "2" in A with the "7" in B and so on. My wish would be to have "curvy" arrows (as far as i know the tikz packages provides them) and not "straight" arrows.

Is it even possible to combine "ordinary" tabular elements with tikz nodes oder drawings? If yes, how? If not, is there a pure tikz way to solve that problem? Thanks for any advices or hints!

\documentclass[12pt,a4paper,twoside]{scrartcl}
\begin{document} 
\begin{center}

  \begin{tabular}{c c c c c c}
   $A$ & 1 & 2 & 3 & 4 & 
  \end{tabular}

  \vspace{15mm}

  \begin{tabular}{c c c c c c c c c c }
   $B$ & \fbox{5} & 6 & \fbox{7} & 8 & \fbox{9} & 10 & \fbox{11} & 12 &  
  \end{tabular}

\end{center}
\end{document}
Daniel F
  • 539
  • 6
  • 14

3 Answers3

6

Do you mean something like this:

enter image description here

Note:

Code:

\documentclass{article}

\usepackage{tikz} \usetikzlibrary{calc}

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

\newcommand*{\DrawArrow}[3][]{% % #1 = draw options % #2 = left point % #3 = right point \begin{tikzpicture}[overlay,remember picture] \draw [very thick, -stealth, #1] ($(#2)+(0.25em,-0.3ex)$) to ($(#3)+(0.25em,2.5ex)$); \end{tikzpicture}% }%

\begin{document} \begin{center}

\begin{tabular}{c c c c c c} $A$ & \tikzmark{topA}1 & \tikzmark{topB}2 & \tikzmark{topC}3 & \tikzmark{topD}4 & \end{tabular}

\vspace{15mm}

\begin{tabular}{c c c c c c c c c c } $B$ & \fbox{\tikzmark{bottomA}5} & 6 & \fbox{\tikzmark{bottomB}7} & 8 & \fbox{\tikzmark{bottomC}9} & 10 & \fbox{\tikzmark{bottomD}11} & 12 &
\end{tabular} \DrawArrow[red, out=-90, in=90]{topA}{bottomA} \DrawArrow[olive, out=-90, in=90]{topB}{bottomB} \DrawArrow[blue, out=-90, in=90]{topC}{bottomC} \DrawArrow[brown, out=-90, in=90]{topD}{bottomD} \end{center} \end{document}

Peter Grill
  • 223,288
  • Wow! That's exactly my desired representation. Thank you very much for the quick response and the understandable code style! – Daniel F Mar 22 '13 at 22:05
  • 1
    Wait... I wasn't done... Solution now updated with a better macro interface. – Peter Grill Mar 22 '13 at 22:11
  • The vertical line passing through the center of one row is not aligned with that of the other row. In other words, 2 and 8 should be in the same column. :D – kiss my armpit Mar 22 '13 at 22:36
  • @Karl'sstudents: Not sure I understand. I am just connecting existing text, not adjusting it. The positions can be adjusted as needed. – Peter Grill Mar 22 '13 at 22:40
  • I meant, 2 and 8 should be in the same column. In other words, 8 should be right below 2 such that 3 and 9 are connected with a straight line. By the way, it is not crucial issue. :-) – kiss my armpit Mar 22 '13 at 22:44
  • @Karl'sstudents: OK, but am going to leave that alone as that is not the thrust of this question. – Peter Grill Mar 22 '13 at 23:16
  • i've chosen an "easy as possible" mapping - my mapping here is more complicated but that doesn't matter, it's all about the programming procedure. Thank you two for your effort! – Daniel F Mar 22 '13 at 23:35
4

With PSTricks.

enter image description here

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{pst-node}
\psset
{
    mnode=r,
    rowsep=2cm,
    colsep=0.5cm,
}

\def\c{[mnode=circle] }


\begin{document}
\offinterlineskip
\begin{psmatrix}
    & & A & 1 & 2 & 3 & 4\\
    B & \c 5 & 6 & \c 7 & 8 & \c 9 & 10 & \c 11 & 12
    \foreach \i/\j in {4/2,5/4,6/6,7/8}
        {\nccurve[angleA=-90,angleB=90,nodesep=3pt]{->}{1,\i}{2,\j}}
\end{psmatrix}
\end{document}

Warning:

  • mnode for \psframebox is not available out of the box. We can circumvent it by wrapping each item with \psframebox manually, but it will take more keystrokes!
  • mnode=circle will use \pscirclebox to wrap the item. Unfortunately, \pscirclebox has no option to adjust its radius. It results in the size of each circle will vary depending on the size of the contained item. Only framesep is available to adjust the border. Sad!
Moriambar
  • 11,466
3

Here's a very short solution using the tikz-cd package:

\documentclass{standalone}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[out=-90, in=90, column sep=small, row sep=15mm]
    & & A & 1 \arrow{dll} & 2 \arrow{dl} & 3 \arrow{d} & 4 \arrow{dr} \\
    B & \fbox{5} & 6 & \fbox{7} & 8 & \fbox{9} & 10 & \fbox{11} & 12
\end{tikzcd}
\end{document}

The result is this:

enter image description here

Note that the table entries created by this method will automatically be in math mode. Any entries for which this behavior is undesired should have their text enclosed in a \text{} command.

Moriambar
  • 11,466