基于此在 tikz 中创建的表格环境,我正在寻找tabular
用 TikZ 包装一个简单的方法(可能可以使用 tcolorbox,但文档对我来说看起来像一座难以攀登的珠穆朗玛峰)。
我的问题
“长篇”tabular
分为 4 个部分(本例中实际更多)。每个部分将在一次讲座中进行分析。在 4 次讲座结束时,长篇tabular
将作为一个整体变得有意义。
我的问题
我应该怎么做(我感觉我做错了什么)才能只输入一次这个大表格,但实际上使用每个块(在示例中, \hline 之间分隔不均匀,但我想有更好的方法可以做到这一点),并使用与原始格式相同的格式?
低于
\documentclass{standalone}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{array}
\usepackage{environ}
\usepackage{tikz}
\usepackage{caption}
\newsavebox{\tablebox}
\definecolor{tablecolor}{named}{orange}
\NewEnviron{rndtable}[1]{%
\addtolength{\extrarowheight}{1ex}%
\rowcolors{2}{tablecolor!5}{tablecolor!20}%
\sffamily
\newcommand{\header}[1]{%
\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}
答案1
一种(可能不太优雅)方法是定义命令来显示块。这样,您可以让一个命令承载整个表(如下面的 MWE 所示),或者在每个小表中使用一个命令,然后通过收集这些命令来组成大表。
也可以看看这个问题。
\documentclass{article}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{array}
\usepackage{environ}
\usepackage{tikz}
\usepackage{caption}
\newsavebox{\tablebox}
\definecolor{tablecolor}{named}{orange}
\NewEnviron{rndtable}[1]{%
\addtolength{\extrarowheight}{1ex}%
\rowcolors{2}{tablecolor!5}{tablecolor!20}%
\sffamily
\newcommand{\header}[1]{%
\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}
编辑:如果你想给每个块命名,你可以使用例如etoolbox
的\ifstrequal
命令(手册第 3.6.3 节)。
\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}