我想在 latex 中绘制以下多列表格。标题居中对齐,其他文本左对齐。如何操作?此处所有字段(如 One、Two 等)可能包含任意数量的文本。
我目前的尝试如下。我还想:
(a)第二行和第三行根据第一列的文本在中间分开。
(b)除标题外,所有文字均应左对齐
(c)二和三之间的直线
\documentclass{article}
\usepackage{blindtext}
\begin{document}
\begin{tabular}{|p{5cm}|c|c|c|}
\hline
\textbf{A} & \textbf{B} & \textbf{C} & \textbf{D}\\
\hline
\blindtext & \begin{tabular}{@{}c@{}}Two \\ Three\end{tabular} & Four & Six
\end{tabular}
\end{document}
答案1
\documentclass{article}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{blindtext}
\begin{document}
\begin{tabular}{|p{5cm}|l|l|l|}
\hline
\multicolumn{1}{|c}{\textbf{A} }& \multicolumn{1}{|c|}{\textbf{B} }& \multicolumn{1}{c}{\textbf{C} }& \multicolumn{1}{|c|}{\textbf{D} }\\
\hline
\multirow{2}{*}{One} & Two & Four & \multirow{2}{*}{Six} \\ \cline{2-3}
& Three & Five & \\ \hline
Seven& Eight & Nine & Ten \\ \hline
\end{tabular}
\end{document}
答案2
我对您的要求的解释如下:(a)“A”列的宽度应为“B”列的一半,并且它们一起应跨越可用宽度的前半部分;(b)“C”列和“D”列应等宽并跨越后半部分;(c)所有单元格的内容都应顶部对齐;(d)中间一行单元格的高度由单元格“一”和“六”中较高的一个决定。
如果这个解释是正确的,那么下面的设置可能就是你想要的。在我看来,唯一的缺点是,分隔单元格二/四和单元格三/五的水平线不会自动平衡;根据单元格二和单元格四的内容,\phantom
可能需要一条指令,如下面的代码所示。
\documentclass{article}
\usepackage{array,ragged2e}
\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\newlength\tablengtho
\newlength\tablengtha
\newlength\tablengthb
\newlength\tablengthcd
% Calculare widths of columns A, B, and C/D
\setlength\tablengtho{\dimexpr\textwidth-8\tabcolsep-5\arrayrulewidth\relax}
\setlength\tablengtha{\dimexpr0.6667\tablengtho/4\relax}
\setlength\tablengthb{\dimexpr1.3333\tablengtho/4\relax}
\setlength\tablengthcd{\dimexpr\tablengtho/4\relax}
\begin{document}
\noindent
\begin{tabular}{| P{\tablengtha} | P{\tablengthb} |
P{\tablengthcd} | P{\tablengthcd} |}
\hline
\multicolumn{1}{|c|}{\textbf{A}} & \multicolumn{1}{c|}{\textbf{B}} &
\multicolumn{1}{c|}{\textbf{C}} & \multicolumn{1}{c|}{\textbf{D}} \\
\hline
One One One One One One One One One &
\multicolumn{1}{@{}c@{}|}{%
\begin{tabular}[t]{P{\tablengthb}}
Two Two Two Two Two Two Two \\
\hline
Three Three Three Three Three \\
\end{tabular}} &
\multicolumn{1}{@{}c@{}|}{%
\begin{tabular}[t]{P{\tablengthcd}}
Four Four Four \phantom{Four} \\ % \phantom{Four} needed for balance
\hline
Five Five Five \\
\end{tabular}} &
Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six Six \\
\hline
Seven & Eight & Nine & Ten \\
\hline
\end{tabular}
\end{document}
答案3
我尝试这样做stables – 简化的 Plain TEX 表格通常将文本置于每个单元格的中心。我使用了 4 个宏,每行一个,以控制行的水平大小,并提供一种方法来控制每个单元格中文本的水平和垂直放置。
\documentclass{article}
\input stables.tex
%the width of each column is controlled by the "\hsize=" in each of the four macros
%for each macro the first variable controls horizontal placement of the text, the second
%takes the text itself and the third controls vertical placement of the text.
\def\AA#1#2#3{\hphantom{\hspace{#1}}%
\vtop{\hsize=1.2in\vskip#3 #2}\hphantom{\hspace{1pt}}}
\def\BB#1#2#3{\hphantom{\hspace{#1}}%
\vtop{\hsize=2.2in\vskip#3 #2}\hphantom{\hspace{1pt}}}
\def\CC#1#2#3{\hphantom{\hspace{#1}}%
\vtop{\hsize=1in\vskip#3 #2}\hphantom{\hspace{1pt}}}
\def\DD#1#2#3{\hphantom{\hspace{#1}}%
\vtop{\hsize=1.4in\vskip#3 #2}\hphantom{\hspace{1pt}}}
\begin{document}
\begintable
A|B|C|D\elt
\multirow4{\AA{-.1cm}{One}{-.9cm}}|\BB{-.6cm}{Two}{-.2cm} |\CC{-.8cm}%
{Four}{-.2cm}|\multirow4{\DD{-.1cm}{Six}{-.9cm}}\elspec
|||\el
|||\el
|\trule|\trule| \el
|\BB{-.6cm}{Three}{-.4cm} |\CC{-.8cm}{Five}{-.4cm}|\el
|||\elt
\AA{-.9cm}{Seven}{-.2cm}|\BB{-.9cm}{Eight}{-.2cm}|\CC{-.8cm}{Nine}%
{-.2cm}|\DD{-1cm}{Ten}{-.2cm}
\endtable
\end{document}
这是我的结果:
答案4
最好使用 booktabs 包。规则在行之间提供一些空间。
\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}
\begin{tabular}{p{5cm}|l|l|l}
\toprule
\multicolumn{1}{c}{\textbf{A} }& \multicolumn{1}{|c}{\textbf{B} }& \multicolumn{1}{|c}{\textbf{C} }& \multicolumn{1}{|c}{\textbf{D} }\\
\midrule
\multirow{2}{*}{One} & Two & Four & \multirow{2}{*}{Six} \\ \cmidrule{2-3}
& Three & Five & \\ \midrule
Seven& Eight & Nine & Ten \\ \bottomrule
\end{tabular}
\end{document}