1

I am struggling with a table in LaTeX, where I want to draw lines inside the table environment. My goal is something like this where lines connect values from the first to values from the second column:

enter image description here

I will probably have to use TikZ but I couldn't find anything similar. Best I could find actually is in this one without any progress. Is it even possible to do something like that?

Update: MWE

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{threeparttable}
\usepackage{multirow}

\begin{document} \begin{table} \begin{threeparttable} \begin{tabular}{ccccc} 2018 & \multirow{2}{}1 & 5 & 10 & 15 \ 2019 & \multirow{2}{}2 & 6 & 11 & 16 \ 2020 & \multirow{2}{}3 & 7 & 12 & 17 \ 2021 & \multirow{2}{}4 & 8 & 13 & 18 \ 2022 & & 9 & 14 & 19 \ \end{tabular} \end{threeparttable}
\end{table} \end{document}

Thanks

akis
  • 13
  • Welcome to TSE. What did you try? – José Carlos Santos Dec 25 '22 at 23:14
  • One simple solution that comes to my mind is using tikzmark. Please post your table into a complete MWE and we''l help you out. – SebGlav Dec 25 '22 at 23:35
  • I am sort of trying to understand the syntax of TikZ @SebGlav. I just updated my question above with a minimal example. – akis Dec 26 '22 at 00:07
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – hpekristiansen Dec 26 '22 at 00:27

2 Answers2

3
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{threeparttable}
\usepackage{multirow}
\begin{document}
\begin{threeparttable} 
\begin{tabular}{ccccc} 
2018\tikzmark{18} &  \multirow{2}{*}{\tikzmark{1819}1} & 5 & 10 & 15 \\
2019\tikzmark{19} &  \multirow{2}{*}{\tikzmark{1920}2} & 6 & 11 & 16 \\
2020\tikzmark{20} &  \multirow{2}{*}{\tikzmark{2021}3} & 7 & 12 & 17 \\
2021\tikzmark{21} &  \multirow{2}{*}{\tikzmark{2122}4} & 8 & 13 & 18 \\
2022\tikzmark{22} &                                    & 9 & 14 & 19 \\
\end{tabular}
\end{threeparttable}
\begin{tikzpicture}[remember picture, overlay]
\draw[transform canvas={yshift=0.6ex}] 
([xshift=1pt]pic cs:18) -- ([xshift=-1pt]pic cs:1819) --
([xshift=1pt]pic cs:19) -- ([xshift=-1pt]pic cs:1920) --
([xshift=1pt]pic cs:20) -- ([xshift=-1pt]pic cs:2021) --
([xshift=1pt]pic cs:21) -- ([xshift=-1pt]pic cs:2122) --
([xshift=1pt]pic cs:22);    
\end{tikzpicture}
\end{document}

Table with lines connecting cells

2

With a TikZmatrix:

\documentclass[10pt,a4paper]{article}
%\usepackage[utf8]{inputenc}<--- no more needed in up-to-date distribution
\usepackage[T1]{fontenc}
\usepackage{threeparttable}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document} \begin{table}\centering \begin{threeparttable} \begin{tikzpicture} \matrix[matrix of nodes, row sep=-4pt, column sep=4pt, row 1/.style={font=\bfseries, text height=8pt, text depth=4pt}] (mymatr) {
Year & Header 1 (something) & Header 2 & Header 3 & Header 4 \[10pt] 2018 & & 5 & 10 & 15 \ & 1 \ 2019 & & 6 & 11 & 16 \ & 2 \ 2020 & & 7 & 12 & 17 \ & 3 \ 2021 & & 8 & 13 & 18 \ & 4 \ 2022 & & 9 & 14 & 19 \ }; \foreach \ind [evaluate=\ind as \indpre using int(\ind-1), evaluate=\ind as \indpost using int(\ind+1) ] in {3, 5, 7, 9} { \draw (mymatr-\indpre-1.east) -- (mymatr-\ind-2.west); \draw (mymatr-\indpost-1.east) -- (mymatr-\ind-2.west); } % hlines \draw[very thick] (mymatr.north west) -- (mymatr.north east); \draw[shorten >=-4pt, shorten <=-4pt] (mymatr-1-1.south west) -- (mymatr-1-5.south east); \draw[very thick] (mymatr.south west) -- (mymatr.south east); \end{tikzpicture} \begin{tablenotes}[para]\small Note: I added the headers only to show how to manage them with a Ti\emph{k}Zmatrix. \end{tablenotes} \end{threeparttable}
\end{table} \end{document}

enter image description here

CarLaTeX
  • 62,716
  • This is really great. Is the usage of multirow wrong? I see you got the same result without it. – akis Dec 26 '22 at 09:29
  • @akis No, it is not wrong in an ordinary table, but it is not used in a TikZmatrix. Thank you for accepting my answer! – CarLaTeX Dec 26 '22 at 17:04