10

I am trying to reproduce a table that has this sort of structure:

enter image description here

The closest I am able to get is something like this:

enter image description here

With this code:

\documentclass[margin=10pt]{standalone}

\usepackage{multicol}
\usepackage{multirow}

\begin{document}

\begin{tabular}{|c|c|}
\hline
\multicolumn{1}{|l|}{Type 1} & \multicolumn{1}{l|}{Type 2} \\ \hline
\multirow{3}{*}{Data 1}      & \multirow{4}{*}{Data 5}     \\
                             &                             \\
                             &                             \\ \cline{1-1}
\multirow{3}{*}{Data 2}      &                             \\ \cline{2-2} 
                             & \multirow{4}{*}{Data 6}     \\
                             &                             \\ \cline{1-1}
\multirow{3}{*}{Data 3}      &                             \\
                             &                             \\ \cline{2-2} 
                             & \multirow{4}{*}{Data 7}     \\ \cline{1-1}
\multirow{3}{*}{Data 4}      &                             \\
                             &                             \\
                             &                             \\ \hline
\end{tabular}

\end{document}

I want the cells to be the same size but off-centered.

EDIT: All of these answers are great resources for constructing tables! I also found the use of a website called Tables Generator which ultimately helped me with my main project.

1
  • 1
    Stay far away from these terrible online table generators. They usually insert a lot of unnecessary code in the table which will make it nearly impossible to get a good looking result. Commented May 7 at 14:52

4 Answers 4

13

You're almost there, just use \multicolumn{1}{c}{} for the cells without vertical borders (and use \mutilrows of proper number of rows).

\documentclass[margin=10pt]{standalone}

\usepackage{multicol}
\usepackage{multirow}
\usepackage{array}
\renewcommand{\arraystretch}{1.5}

\begin{document}

\begin{tabular}{|c|c|}
\hline
\multicolumn{1}{|l|}{Type 1} & \multicolumn{1}{l|}{Type 2} \\\hline
\multirow{2}{*}{Data 1}      & \multicolumn{1}{c}{}        \\\cline{2-2}
                             & \multirow{2}{*}{Data 5}     \\\cline{1-1}
\multirow{2}{*}{Data 2}      &                             \\\cline{2-2} 
                             & \multirow{2}{*}{Data 6}     \\\cline{1-1}
\multirow{2}{*}{Data 3}      &                             \\\cline{2-2} 
                             & \multirow{2}{*}{Data 7}     \\\cline{1-1}
\multirow{2}{*}{Data 4}      &                             \\\cline{2-2}
                             & \multicolumn{1}{c}{}        \\\cline{1-1}
\end{tabular}
\end{document}

enter image description here

9

Proof of concept. With the actual data it can be improved.

The idea is to use \valign that's the “vertical” counterpart of \halign on which tabular is based.

\documentclass{article}

\newcommand{\cell}[1]{\hbox{\framebox[6em]{\vphantom{$\Bigg|$}#1}}}

\begin{document}

\begingroup\lineskip=-\fboxrule
\valign{#\cr
  \cell{Type 1}
  \cell{Data1}
  \cell{Data2}
  \cell{Data3}
  \cell{Data4}\cr\noalign{\kern-\fboxrule}
  \cell{Type 2}
  \vfill
  \cell{Data5}
  \cell{Data6}
  \cell{Data7}
  \vfill\cr
}\endgroup

\end{document}

enter image description here

4

I would use a tikzpicture. The main problem is guessing how wide to make the columns.

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix (T) [matrix of nodes,
  every node/.style={draw, text width=2cm},
  column sep=-0.4pt]
{Type 1 & Type 2 \\};
\begin{scope}[every node/.style={draw, text width=2cm, minimum height=2\baselineskip}]
  \node[below=-0.4pt] (D1) at (T-1-1.south) {Data 1};
  \node[right=-0.4pt] (D5) at (D1.south east) {Data 5};
  \node[below=-0.4pt] (D2) at (D1.south) {Data 2};
  \node[below=-0.4pt] (D6) at (D5.south) {Data 6};
\end{scope}
\end{tikzpicture}

\end{document}

demo

3

With the library chains

we can use two chains (one per type), and position them with at(5em,2ex+0.5\pgflinewidth) for example.

Or use one chain for types and another for data. With D/.style = {on chain=D}, the nodes are marked with D-1, D-2,...

With T/.style = {on chain=T,font=\bfseries,fill=yellow!50}, the nodes are marked with T-1, T-2,... We can add the styles \bfseries, color...

\documentclass[border=5mm]{standalone}
%https://tex.stackexchange.com/questions/717374/how-to-create-rows-that-are-off-centered-from-others
\usepackage{tikz}
\usetikzlibrary {chains}
\begin{document}
\begin{tikzpicture}[
  start chain=T going right,
  start chain=D going below,
  node distance=0mm,
  every node/.style={draw,minimum width=5em,minimum height=4ex},
  T/.style = {on chain=T,font=\bfseries,fill=yellow!50},
  D/.style = {on chain=D}
  ]
%
  \node [T] {Type1};% T-1
  \node [D,below=1ex of T-1] {Data1};
  \node [D] {Data2};
  \node [D] {Data3};
  \node [D] {Data4};
%
  \node [T] {Type2};% T-2
  \node [D,below=3ex of T-2]  {Data5};
  \node [D] {Data6};
  \node [D] {Data7};
  \end{tikzpicture}


\begin{tikzpicture}[
  start chain=1 going below,
  start chain=2 going below,
  node distance=0mm,
  every node/.style={draw,minimum width=5em,minimum height=4ex}]
%
  \node [on chain] {Type1};
  \node [on chain,below=1ex] {Data1};
  \node [on chain] {Data2};
  \node [on chain] {Data3};
  \node [on chain] {Data4};
%
  \node [on chain=2] at(5em,2ex+0.5\pgflinewidth) {Type2};
  \node [on chain=2,below=3ex]  {Data5};
  \node [on chain=2] {Data6};
  \node [on chain=2] {Data7};
  \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .