我正在使用 latex 创建双列文档。我使用选项\columnwidth
自动调整表格以适应列宽,如下图所示。第二列Value
太宽。如何设置两列宽度相等?
这是我的源代码。
\begin{table}
\centering
\caption{Simulation parameters}
\begin{tabular}{l|l}
\hline
Key & Value \\
\hline
simulation duration & 12 hours \\
update interval & 1s \\
time-to-live & 12 hours \\
buffer size & infinite \\
message interval & 20s \\
\hline
\end{tabular}
\end{table}
\begin{table}
\centering
\caption{Simulation parameters}
\begin{tabularx}{\columnwidth}{l|l}
\hline
Key & Value \\
\hline
simulation duration & 12 hours \\
update interval & 1s \\
time-to-live & 12 hours \\
buffer size & infinite \\
message interval & 20s \\
\hline
\end{tabularx}
\label{table: simulation parameters}
\end{table}
答案1
编辑
@Mico 指出了这个问题的一个更好的解决方案,因为tabularx
已经带有列类型X
,它会自动执行您想要的操作:
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{table}
\caption{Simulation parameters}
\begin{tabularx}{\columnwidth}{X|X}
\hline
Key & Value \\
\hline
simulation duration & 12 hours \\
update interval & 1s \\
time-to-live & 12 hours \\
buffer size & infinite \\
message interval & 20s \\
\hline
\end{tabularx}
\label{table: simulation parameters}
\end{table}
\end{document}
由于您已经知道表格的总宽度,因此您可以将列的宽度定义为\columnwidth
\documentclass{article}
\newlength\mylength
\setlength\mylength{\dimexpr.5\columnwidth-2\tabcolsep-0.5\arrayrulewidth\relax}
\begin{document}
\begin{table}
%% \centering % not needed
\caption{Simulation parameters}
\begin{tabular}{p{\mylength}|p{\mylength}}
\hline
Key & Value \\
\hline
simulation duration & 12 hours \\
update interval & 1s \\
time-to-live & 12 hours \\
buffer size & infinite \\
message interval & 20s \\
\hline
\end{tabular}
\label{table: simulation parameters}
\end{table}
\end{document}