我有以下示例代码,并且我希望所有列具有相同的宽度:
\documentclass[xcolor=table]{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{tabularx,booktabs}
\title{dd}
\author{david.moldes }
\date{June 2023}
\begin{document}
\end{table}
\maketitle
\section{Introduction}
\begin{table}\centering
\begin{tabular}{@{}lccccccc@{}}\toprule
& \multicolumn{7}{c}{\textbf{Levels}}\\
\cmidrule{2-8} \
\textbf{Factors} & \textit{1} & \textit{2} & \textit{3} & \textit{4} & \textit{5} & \textit{6} & \textit{7}\\ \midrule
\textit{A} & Water & ChCl:2Gly & ChCl:2Urea & ---
& --- & --- & --- \\
\textit{B}& 0 & 0.40 & 0.57 &0.69 & 0.82 & 0.86 & 0.91 \\
\bottomrule
\end{tabular}
\end{document}
我尝试使用这里给出的解释,如何在不固定表格宽度的情况下均匀分布列?,但在 beamer 文档中它不起作用。
答案1
不居中列
该tabularx
包提供了一种列类型X
,可以自动调整列的宽度到合适的宽度(最终使该类型的列的宽度相等)。
代码
\begin{tabularx}{1.5\textwidth}{@{} l *7{X} @{}} % I've had to do `1.5\textwidth` as a the width to suit my specific renderer. Adjust it according to your needs.
\toprule
& \multicolumn{7}{c}{\textbf{Levels}} \\
\cmidrule{2-8}
\textbf{Factors} & \textit{1} & \textit{2} & \textit{3} & \textit{4} & \textit{5} & \textit{6} & \textit{7} \\
\midrule
\textit{A} & Water & ChCl:2Gly & ChCl:2Urea & --- & --- & --- & --- \\
\textit{B} & 0 & 0.40 & 0.57 & 0.69 & 0.82 & 0.86 & 0.91 \\
\bottomrule
\end{tabularx}
图像
居中列
虽然每列类型X
都会自动左对齐。要让它们保持居中,请使用这答案(来自另一个 tex stackexchange 帖子)。
新代码
% \newcolumntype{Y}{>{\centering\arraybackslash}X} % in your preamble
\begin{tabularx}{1.5\textwidth}{@{} l *7{Y} @{}} % I've had to do `1.5\textwidth` as a the width to suit my specific renderer. Adjust it according to your needs.
\toprule
& \multicolumn{7}{c}{\textbf{Levels}} \\
\cmidrule{2-8}
\textbf{Factors} & \textit{1} & \textit{2} & \textit{3} & \textit{4} & \textit{5} & \textit{6} & \textit{7} \\
\midrule
\textit{A} & Water & ChCl:2Gly & ChCl:2Urea & --- & --- & --- & --- \\
\textit{B} & 0 & 0.40 & 0.57 & 0.69 & 0.82 & 0.86 & 0.91 \\
\bottomrule
\end{tabularx}
新图片
希望这就是您所寻找的!