[Tex/LaTex] Aligning tikz nodes to tabular cells

tablestikz-pgf

I would like to make a Zwicky Box by using tabular combined with an overlaying tikzpicture. How can I (semi-)automatically align nodes to the vertical and horizontal center of their corresponding cells?

I understand it would probably work with a matrix which is aligned to the top left corner of the tabular and whose row and column seperation somehow relate to the sizes of the cells (which all have fixed width and height).

Example of what a Zwicky Box could look like

Example of how diamond nodes could look like, made in Excel

So my questions are:

  1. how do I align a tikzpicture exactly with the upper left corner of a tabular?
  2. how do I calculate seperation sizes of columns and rows by knowledge of cell width and height of my tabular environment?
  3. how do I tell a node in which cell of a matrix it shall be placed and that it shall be aligned to the cell's v/h center?
  4. should I actually use a tikzpicture overlaying a tabular? Or would it be better to drop the tabular and put everything into the matrix?

Best Answer

I would do it with a simple tabular using a few tikzmark macros.

Code

\documentclass[convert=false]{standalone}
\usepackage{tikz,booktabs,array}
\makeatletter
\newcommand{\markZwicky}[1][]{\pgfutil@ifnextchar({\mark@Zwicky{#1}}{\mark@Zwicky{#1}()}}
\def\mark@Zwicky#1(#2)#3{%
   \tikz[every Zwicky picture,#1]{%
     \node[every Zwicky node,draw=none,inner sep=+\z@,outer sep=+\z@] {#3};
     \def\tikz@Mark@name{#2}%
     \ifx\tikz@Mark@name\pgfutil@empty\else
       \tikzset{every Zwicky node/.append style={name={#2}}}%
     \fi
     \node[every Zwicky node,overlay] {\phantom{#3}};
   }%
}
\newcommand{\tikzZwicky}[1][]{%
  \def\tikz@Zwicky@args{#1}%
  \let\tikz@Zwicky@list\pgfutil@gobble
  \let\tikz@Zwicky@first\pgfutil@empty
  \pgfutil@ifnextchar(\tikz@Zwicky@collect\tikz@Zwicky@finish
}
\def\tikz@Zwicky@collect(#1){%
  \ifx\tikz@Zwicky@first\pgfutil@empty
    \edef\tikz@Zwicky@first{#1}%
  \else
    \edef\tikz@Zwicky@list{\tikz@Zwicky@list,#1}%
  \fi
  \pgfutil@ifnextchar(\tikz@Zwicky@collect\tikz@Zwicky@finish
}
\def\tikz@Zwicky@finish{%
  \tikz[remember picture,overlay]
    \draw[every Zwicky connector,/expanded=\tikz@Zwicky@args]
      (\tikz@Zwicky@first) [/expanded={@Zwicky@list/.list={\tikz@Zwicky@list}}] [every Zwicky connect finish/.try];
}
\pgfkeys{/expanded/.code={\edef\pgfkeys@temp{{#1}}\expandafter\pgfkeysalso\pgfkeys@temp}}
\makeatother
\tikzset{
  @Zwicky@list/.style={insert path={to[every Zwicky connector how/.try] (#1)}},
  every Zwicky picture/.style={
    baseline,
    remember picture,
  },
  every Zwicky node/.style={
    remember picture,
    anchor=base,
    inner sep=+2pt
  },
  every Zwicky connector/.style={
    ultra thick,
    red!80!black,
    draw opacity=.5,
    line cap=round,
    line join=round
  }
}
\begin{document}
\begin{tabular}{>{\bfseries}lcccc}
    \toprule
                &                                       \multicolumn{4}{c}{\bfseries Configurations}                                        \\ \cmidrule{2-5}
    Parameters  &   \bfseries Cookie A    &        \bfseries Cookie B         &      \bfseries Cookie C      &      \bfseries Cookie D      \\ \midrule
    Texture     &         Smooth          &               Soft                &   \markZwicky(1-3){Chunky}   &             Soft             \\
    Consistency & \markZwicky(2-1){Chewy} &               Goowy               &           Crunchy            &            Chewy             \\
    Size        &         Medium          &               Small               &            Large             &    \markZwicky(3-4){Huge}    \\
    Base        &          Plain          &               Plain               & \markZwicky(4-3){Chocholate} &           Oatmeal            \\
    Topping     &          Sugar          &               None                &            Icing             & \markZwicky(5-4){Chocholate} \\
    Stuffing    &       Chocholate        & \markZwicky(6-2){Dates and Harda} &          Macadamia           &          Chocholate          \\ \bottomrule
\end{tabular}
\tikzZwicky(1-3)(2-1.east)(3-4.west)(4-3.east)(5-4.west)(6-2)
\tikzZwicky[blue](2-1.east)(5-4.west)(3-4.west)(1-3.center)(4-3.center)(6-2)
\end{document}

Output

enter image description here