我知道如何使用多列。例如,我可能会生成以下方案
-------------
| A |
-------------
| B |
-------------
| C | D | E |
-------------
和
\begin{array}{|c|c|c|}\hline
\multicolumn 3{|c|}{A}\\\hline
\multicolumn 3{|c|}{B}\\\hline
C & D & E\\\hline
\end{array}
现在,我想要生成以下内容,但我不知道如何生成:
-------------
| A |
-------------
| B | C | D |
-------------
| E | F | G |
-------------
| H | I |
-------------
| J | K |
-------------
有什么想法吗?我尝试过,\multicolumn 3{|c|c|}{Text}
但没有成功。
答案1
基于的解决方案tabularx
,因此您不必指定除了表格的总宽度之外的任何宽度:粗略地说,是通过 在内部\multicolumn{1.5}...
嵌套tabularx{X|X}
相关宽度的来获得的\multicolumn{3}{@{}c@{}
:
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\setlength\extrarowheight{2pt}
\begin{tabularx}{0.5\textwidth}{|*{3}{>{\centering\arraybackslash}X|}}
\hline
\multicolumn{3}{|c|}{A} \\
\hline
B & C & D \\
\hline
E & F & G \\
\hline
\multicolumn{3}{|@{}c@{}|}{\begin{tabularx}{\dimexpr 0.5\textwidth-2\arrayrulewidth}[t] {>{\centering\arraybackslash}X|>{\centering\arraybackslash}X}
H & I \\
\hline
J & K
\end{tabularx}} \\
\hline
\end{tabularx}
\end{document}
答案2
这是一个保证列宽比例为 2:3:6 的解决方案。要选择的主要长度参数是\mylen
。在下面的示例中,它设置为1cm
;您可以随意选择其他值。
\documentclass{article}
\usepackage{array} % for "\newcolumntype" macro
\usepackage{ragged2e} % for "\Centering" macro
% Create a centered version of "p" column type:
\newcolumntype{C}[1]{>{\Centering\arraybackslash}p{#1}}
% Basic length variable: "\mylen"
\newlength\mylen
\setlength\mylen{1cm} % <-- set length to suit your needs
\newlength\lengtha \setlength\lengtha{2\mylen}
\newlength\lengthb \setlength\lengthb{3\mylen}
%% Four user macros: \mcaa, \mcab, \mcba, \mcbb.
%% Use \mcaa and \mcba for the *first* cell in a given row;
%% use \mcab and \mcbb for the remaining cells in a row.
\newcommand\mcaa[1]{\multicolumn{2}{|C{\lengtha}|}{#1}}
\newcommand\mcab[1]{\multicolumn{2}{ C{\lengtha}|}{#1}}
\newcommand\mcba[1]{\multicolumn{3}{|C{\lengthb}|}{#1}}
\newcommand\mcbb[1]{\multicolumn{3}{ C{\lengthb}|}{#1}}
\begin{document}
\begin{table}
\centering
\setlength\extrarowheight{2pt} % for a more "open" look
\setlength\tabcolsep{0pt}
\begin{tabular}{*{6}{C{\mylen}}}
\hline
\multicolumn{6}{|c|}{A} \\ % row with single, very wide cell
\hline
\mcaa{B} & \mcab{C} & \mcab{D} \\ % row with 3 cells
\hline
\mcaa{E} & \mcab{F} & \mcab{G} \\ % row with 3 cells
\hline
\mcba{H} & \mcbb{I} \\ % row with 2 cells
\hline
\mcba{J} & \mcbb{K} \\ % row with 2 cells
\hline
\end{tabular}
\end{table}
\end{document}
答案3
您可以创建一个有 4 列的数组,并使用自定义设置它们的长度columntype
C
。
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}[1]{>{\hspace{0pt}\centering\arraybackslash}p{#1}}
\begin{document}
\(
\begin{array}{|*4{C{10mm}|}}\hline
\multicolumn{4}{|C{40mm}|}{A}\\ \hline
B & \multicolumn{2}{|C{13mm}|}{C} & D\\ \hline
E & \multicolumn{2}{|c|}{F} & G\\ \hline
\multicolumn{2}{|C{20mm}}{H} & \multicolumn{2}{|c|}{I}\\ \hline
\multicolumn{2}{|c}{J} & \multicolumn{2}{|c|}{K}\\ \hline
\end{array}
\)
\end{document}