以下代码生成了我想要的表格类型,但它确实将几乎所有空间都分配给了第一个单元格。我希望它们均匀分割,而不提供确切的单元格宽度,因为每次我有不同数量的列时我都必须重新计算。
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{tabularx} % for 'tabularx' environment and 'X' column type
\usepackage{ragged2e} % for '\RaggedRight' macro (allows hyphenation)
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}
\usepackage{booktabs} % for \toprule, \midrule, and \bottomrule macros
\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{@{} Y c c c @{}} % use 'Y' for first column
\toprule
1 & 2 & 3 \\
\midrule
1 & \begin{tabular}{l} 2 \\ 3\end{tabular} & \begin{tabular}{l} 1 \\ 2\end{tabular} \\
1 & \begin{tabular}{l} 2 \\ 3\end{tabular} & \begin{tabular}{l} 1 \\ 2\end{tabular} \\
1 & \begin{tabular}{l} 2 \\ 3\end{tabular} & \begin{tabular}{l} 1 \\ 2\end{tabular} \\
1 & \begin{tabular}{l} 2 \\ 3\end{tabular} & \begin{tabular}{l} 1 \\ 2\end{tabular} \\
\bottomrule
\end{tabularx}
\caption{Nonlinear Model Results}
\label{table:nonlin}
\end{table}
\end{document}
答案1
您必须X
在希望尽可能宽的每一列上使用类型列。可用空间在这些列之间平均分配。
因此,对于居中X
类型的列,您可以定义\newcolumntype{Z}{>{\centering\arraybackslash}X}
。我还为内部\begin{tabular}...\end{tabular}
构造定义了一个快捷方式宏。
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{tabularx} % for 'tabularx' environment and 'X' column type
\usepackage{ragged2e} % for '\RaggedRight' macro (allows hyphenation)
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}
\newcolumntype{Z}{>{\centering\arraybackslash}X}
\usepackage{booktabs} % for \toprule, \midrule, and \bottomrule macros
\newcommand\CELL[2][l]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}
\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{@{} Y Z Z Z @{}} % use 'Y' for first column
\toprule
1 & 2 & 3 \\
\midrule
1 & \CELL{2\\3} & \CELL{1\\2} \\
1 & \CELL{2\\3} & \CELL{1\\2} \\
1 & \CELL{2\\3} & \CELL{1\\2} \\
1 & \CELL{2\\3} & \CELL{1\\2} \\
\bottomrule
\end{tabularx}
\caption{Nonlinear Model Results}
\label{table:nonlin}
\end{table}
\end{document}
答案2
\documentclass[a4paper]{article}
\usepackage{caption}
\usepackage{tabularx}
\usepackage{ragged2e}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{Y}{>{\RaggedRight}X}
\newcolumntype{Z}{>{\Centering}X}
\usepackage{booktabs}
\let\CELL\shortstack
\begin{document}
\begin{table}
\caption{Nonlinear Model Results} \label{table:nonlin}
\begin{tabularx}{\textwidth}{@{} Y Z Z @{}}\toprule
1 & 2 & 3 \\\midrule
1 & \CELL{2\\3} & \CELL{1\\2} \\
1 & \CELL{2\\3} & \CELL{1\\2} \\
1 & \CELL{2\\3} & \CELL{1\\2} \\
1 & \CELL{2\\3} & \CELL{1\\2} \\\bottomrule
\end{tabularx}
\end{table}
\end{document}
表格的标题应位于表格上方: