我正在尝试在多列中绘制一个矩形,并希望它跨越单元格的宽度并与在标准单元格中绘制的类似矩形保持一致。我正在努力确定单元格的正确长度。
最终,我希望能够在表格的每个单元格中插入一个“文本框”,并让宽度和高度自动填充空间
我的MWE
\documentclass[a4paper]{report}
\usepackage{tabularx}
\usepackage{xparse}
\usepackage{multirow}
\usepackage{tikz}
\newcolumntype{H}{@{}m{0pt}@{}}
\newcommand{\tskip}{&}
\DeclareDocumentCommand{\tr}{ O{\hline} O{1mm} }{%
\tabularnewline[#2]#1
}
\DeclareExpandableDocumentCommand{\td}{ s O{1} m }{%
\IfBooleanTF#1%
{\multicolumn{#2}{X}{#3}\tskip}
{\multicolumn{#2}{X|}{#3}\tskip}
}
\DeclareExpandableDocumentCommand{\tf}{ s O{1} m }{%
\IfBooleanTF#1%
{\multicolumn{1}{X}{\multirow{#2}{*}{#3}}\tskip}
{\multicolumn{1}{X|}{\multirow{#2}{*}{#3}}\tskip}
}
\newcommand{\textbox}[1]{%
\begin{tikzpicture}
\node [anchor=south west, fill=white,inner sep=1pt] at (0,0.5) {{\scriptsize\scshape#1}};
\fill [anchor=south west, lightgray] (0,0) rectangle (\linewidth,0.5);
\end{tikzpicture}
}
\begin{document}
\begin{tabularx}{\textwidth}{ X X X X X H }%
\hline
\td[3]{\textbox{Foo}} \td{\textbox{Number 2}} \td*{Bah} \tr[]
\hline
\td{bah} \td{bah} \td[2]{bah} \td*{bah} \tr
\tf[2]{foo} \td{foo} \td{foo} \td{foo} \td*{foo} \tr[\cline{2-5}]
\td[1]{} \td{foo} \td{foo} \td{foo} \td*{foo} \tr
\td{foo} \td{foo} \td{foo} \td{foo} \td*{foo} \tr[]
\hline
\end{tabularx}
\end{document}
答案1
tabularx
将 X 列的宽度存储在 中\TX@col@width
。使用它来计算 3 个 X 列加上 4 个 的宽度\tabcolsep
。
\documentclass[a4paper]{report}
\usepackage{tabularx}
\usepackage{xparse}
\usepackage{multirow}
\usepackage{tikz}
\usepackage{color}
\newcolumntype{H}{@{}m{0pt}@{}}
\newcommand{\tskip}{&}
\DeclareDocumentCommand{\tr}{ O{\hline} O{1mm} }{%
\tabularnewline[#2]#1
}
\DeclareExpandableDocumentCommand{\td}{ s O{1} m }{%
\IfBooleanTF#1%
{\multicolumn{#2}{X}{#3}\tskip}
{\multicolumn{#2}{X|}{#3}\tskip}
}
\DeclareExpandableDocumentCommand{\tf}{ s O{1} m }{%
\IfBooleanTF#1%
{\multicolumn{1}{X}{\multirow{#2}{*}{#3}}\tskip}
{\multicolumn{1}{X|}{\multirow{#2}{*}{#3}}\tskip}
}
\newcommand{\textbox}[1]{%
\begin{tikzpicture}
\node [anchor=south west, fill=white,inner sep=1pt] at (0,0.5) {{\scriptsize\scshape#1}};
\fill [anchor=south west, lightgray] (0,0) rectangle (\linewidth,0.5);
\end{tikzpicture}
}
\makeatletter
\newcommand\mywidth{\TX@col@width}
\makeatother
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{ X X X X X H }%
\hline
\td[3]{\textbox{Foo}} \td{\textbox{Number 2}} \td*{Bah} \tr[]
\hline
\td{bah} \td{bah} \td[2]{bah} \td*{bah} \tr
\tf[2]{foo} \td{foo} \td{foo} \td{foo} \td*{foo} \tr[\cline{2-5}]
\td[1]{} \td{foo} \td{foo} \td{foo} \td*{foo} \tr
\td{foo} \td{foo} \td{foo} \td{foo} \td*{foo} \tr[]
\hline
\end{tabularx} \\[5pt]
\begin{tabularx}{\textwidth}{ X X X X X H }%
\multicolumn{3}{l}{%
\colorbox{blue}{%
\parbox[t]{\dimexpr 3\mywidth+4\tabcolsep\relax}{FOO}%
}%
}\\
\hline
\td[3]{\textbox{Foo}} \td{\textbox{Number 2}} \td*{Bah} \tr[]
\hline
\td{bah} \td{bah} \td[2]{bah} \td*{bah} \tr
\tf[2]{foo} \td{foo} \td{foo} \td{foo} \td*{foo} \tr[\cline{2-5}]
\td[1]{} \td{foo} \td{foo} \td{foo} \td*{foo} \tr
\td{foo} \td{foo} \td{foo} \td{foo} \td*{foo} \tr[]
\hline
\end{tabularx}
\end{document}