# Re-use part of tabular as a (smaller) tabular

Based on this tabular-environment-created-in-tikz, I was looking to wrap a simple tabular with TikZ (probably tcolorbox could be used, but the documentation looks like an Everest to climb for me).

My problematic

The "long" tabular is separated in 4 blocks (for this example, more in reality). Each block will be analysed in a lecture. At the end of the 4 lectures the long tabular will make sense as a whole.

My question

What should I do (I have the feeling I took things on the wrong foot) to type this big table only once, but actually use each block (poorly separated between \hline in the example but I imagine there's a far better way to do this), with the same format as the original ?

MWE below

\documentclass{standalone}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{array}
\usepackage{environ}
\usepackage{tikz}

\usepackage{caption}

\newsavebox{\tablebox}
\definecolor{tablecolor}{named}{orange}

\NewEnviron{rndtable}[1]{%
\rowcolors{2}{tablecolor!5}{tablecolor!20}%
\sffamily
\multicolumn{1}{l}{%
\cellcolor{tablecolor}%
\color{white}%
\bfseries##1%
}%
}%
\savebox{\tablebox}{%
\begin{tabular}{#1}%
\BODY
\end{tabular}%
}%
%
\begin{tikzpicture}
\begin{scope}
\clip[rounded corners=1ex]
(0,-\dp\tablebox) rectangle (\wd\tablebox,\ht\tablebox);
\node at (0,-\dp\tablebox) [anchor=south west,inner sep=0pt]
{\usebox{\tablebox}};
\end{scope}
\draw[tablecolor,very thick,rounded corners=1ex]
(0,-\dp\tablebox) rectangle (\wd\tablebox,\ht\tablebox);
\end{tikzpicture}%
}

\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{document}

\begin{rndtable}{r P{5cm}} %
\multicolumn{2}{l}{Block 2} \\ \hline
Block 1 Field 1 & Description that can be long, very long, even longer\\
Block 1 Field 2 & Description \\
Block 1 Field 3 & Description \\
Block 1 Field 4 & Description \\
\hline
Block 2 Field 1 & Description that can be long, very long, even longer\\
Block 2 Field 2 & Description \\
Block 2 Field 3 & Description \\
Block 2 Field 4 & Description \\
\hline
Block 3 Field 1 & Description that can be long, very long, even longer\\
Block 3 Field 2 & Description \\
Block 3 Field 3 & Description \\
Block 3 Field 4 & Description \\
\hline
Block 4 Field 1 & Description that can be long, very long, even longer\\
Block 4 Field 2 & Description \\
Block 4 Field 3 & Description \\
Block 4 Field 4 & Description \\
\hline
\end{rndtable}

\end{document}


One—perhaps unelegant—approach would be to define commands to display the blocks. With this, you could have one command carry the whole table (as in the MWE below), or have one command in each small table and then make the big one by collecting those commands.

\documentclass{article}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{array}
\usepackage{environ}
\usepackage{tikz}

\usepackage{caption}

\newsavebox{\tablebox}
\definecolor{tablecolor}{named}{orange}

\NewEnviron{rndtable}[1]{%
\rowcolors{2}{tablecolor!5}{tablecolor!20}%
\sffamily
\multicolumn{1}{l}{%
\cellcolor{tablecolor}%
\color{white}%
\bfseries##1%
}%
}%
\savebox{\tablebox}{%
\begin{tabular}{#1}%
\BODY
\end{tabular}%
}%
%
\begin{tikzpicture}
\begin{scope}
\clip[rounded corners=1ex]
(0,-\dp\tablebox) rectangle (\wd\tablebox,\ht\tablebox);
\node at (0,-\dp\tablebox) [anchor=south west,inner sep=0pt]
{\usebox{\tablebox}};
\end{scope}
\draw[tablecolor,very thick,rounded corners=1ex]
(0,-\dp\tablebox) rectangle (\wd\tablebox,\ht\tablebox);
\end{tikzpicture}%
}

\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{document}

\newcommand{\block}[1]{
\ifcase#1\or% https://tex.stackexchange.com/questions/17676/conditional-cases-expression
\hline
Block 1 Field 1 & Description that can be long, very long, even longer\\
Block 1 Field 2 & Description \\
Block 1 Field 3 & Description \\
Block 1 Field 4 & Description \\
\or
\hline
Block 2 Field 1 & Description that can be long, very long, even longer\\
Block 2 Field 2 & Description \\
Block 2 Field 3 & Description \\
Block 2 Field 4 & Description \\
\or
\hline
Block 3 Field 1 & Description that can be long, very long, even longer\\
Block 3 Field 2 & Description \\
Block 3 Field 3 & Description \\
Block 3 Field 4 & Description \\
\or
\hline
Block 4 Field 1 & Description that can be long, very long, even longer\\
Block 4 Field 2 & Description \\
Block 4 Field 3 & Description \\
Block 4 Field 4 & Description \\
\else
block #1 not defined
\fi
}

\begin{rndtable}{r P{5cm}} %
\multicolumn{2}{l}{All Blocks} \\ \hline
\block{1}
\block{2}
\block{3}
\block{4}
\hline
\end{rndtable}

\vspace{\baselineskip}

\begin{rndtable}{r P{5cm}} %
\multicolumn{2}{l}{Block 2} \\ \hline
\block{2}
\hline
\end{rndtable}

\end{document}


Edit: If you want to give each block a name instead, you can use e.g. etoolbox's \ifstrequal command (§3.6.3 of the manual).

\newcommand{\block}[1]{
\ifstrequal{#1}{block1}{%
Block 1 Field 1 & Description that can be long, very long, even longer\\
Block 1 Field 2 & Description \\
Block 1 Field 3 & Description \\
Block 1 Field 4 & Description
}{}\ifstrequal{#1}{block2}{%
Block 2 Field 1 & Description that can be long, very long, even longer\\
Block 2 Field 2 & Description \\
Block 2 Field 3 & Description \\
Block 2 Field 4 & Description
}{}\ifstrequal{#1}{block3}{%
Block 3 Field 1 & Description that can be long, very long, even longer\\
Block 3 Field 2 & Description \\
Block 3 Field 3 & Description \\
Block 3 Field 4 & Description
}{}\ifstrequal{#1}{block4}{%
Block 4 Field 1 & Description that can be long, very long, even longer\\
Block 4 Field 2 & Description \\
Block 4 Field 3 & Description \\
Block 4 Field 4 & Description
}
}

\begin{rndtable}{r P{5cm}} %
\multicolumn{2}{l}{All Blocks} \\ \hline
\block{block1} \\ \hline
\block{block2} \\ \hline
\block{block3} \\ \hline
\block{block4} \\
\end{rndtable}

\begin{rndtable}{r P{5cm}} %
\multicolumn{2}{l}{Block 2} \\ \hline
\block{block2} \\
\hline
\end{rndtable}

• Thank you ! I was testing your code and it works well. I was thinking of a very very long table with 20 blocks (dunno if it's a good idea or not) and I could pick bits and pieces. With that many subblocks, may I ask you if there would be a way of better handling (naming ?) the blocks and their title ? like block{nameBlock1}{this is the header of the block} ?
– JeT
Apr 23, 2020 at 23:50
• @Julien-ElieTaieb Absolutely! See my edited answer for one possible approach. Apr 24, 2020 at 0:37
• @Julien-ElieTaieb Hm, I'm not sure I know what you mean. I also know next to nothing about tikzexternalize, so I can't really help there either. If the issue is including complex Tikz pictures, this might help; if it's the time it takes for the table blocks themselves to compile (i.e. wanting to be able to compile a block once and then reuse it arbitrarily many times without extra compilation time), I'm afraid I don't know of a good answer at the moment. Apr 24, 2020 at 10:53
• Merci for your anwer. I am currently investigating where it blocks on my side and get back to you.
– JeT
Apr 24, 2020 at 11:02
• Finally found the reason for the bug... I use Mindmaps and i set globally \tikzset{every node/.style={concept, execute at begin node=\hskip0pt}} to avoid indentation... it messed up everything. All good now.
– JeT
Apr 24, 2020 at 14:04