Tabularx 确定列宽度

Tabularx 确定列宽度

我的问题是第三列中的数据占用了太多空间!有什么方法可以强制该表为每列分配正确的宽度吗?

我正在使用 tabularx

\begin{table}
 \newcolumntype{C}{>{\centering\arraybackslash}X}
 \centering
 \captionsetup{justification=centering, hangindent=-0.2em}
 \caption{some caption}
\label{tab:somelabel}
\begin{tabularx}{1.5\textwidth}{|*{9}{C|}}              
    \hline
             \multirow{2}{*}{} & \multicolumn{2}{|c|}{Gesamtkosten} & \multicolumn{2}{|c|}{Länge eines Fahrzeugumlaufs} & \multicolumn{2}{|c|}{$\#$Elektrobusse} & \multicolumn{2}{|c|}{$H^V$} \\
    \cline{2-9}
    & \vspace{1pt} (K) & \vspace{1pt} (V) & \vspace{1pt} (K) & \vspace{1pt} (V) & \vspace{1pt} (K) & \vspace{1pt} (V) & \vspace{1pt} (K) & \vspace{1pt} (V)\\
    \hline
    \multicolumn{9}{|c|}{\textbf{some description}} \\
    \hline  
    \hspace{-9pt} I$1$~($90$)& $44.490.046$ & $37.214.853~(16~\%)$ & $6.224$ & $5.992~(4~\%)$ & $49$ & $15~(69~\%)$ & $77$ & $76~(1~\%)$\\
    \end{tabularx}
\end{table}

提前致谢!

答案1

由于您不想在单元格中允许换行,因此使用tabularx环境是没有意义的。我建议您改用环境tabular*。根据设计,列不会都具有相同的宽度。不过,这没关系,对吧?

我还想建议您让您的表格看起来更加开放,通过省略所有垂直规则并使用间距适当的水平规则(由包提供booktabs)。

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage[ngerman]{babel}
\usepackage{array,caption,rotating,booktabs}

\begin{document}
\begin{sidewaystable}
\setlength\tabcolsep{0pt}
\captionsetup{justification=centering, 
              hangindent=-0.2em,
              skip=0.333\baselineskip}
\caption{Some caption} \label{tab:somelabel} 
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
    l *{8}{c} }             
\toprule
& \multicolumn{2}{c}{Gesamtkosten} 
& \multicolumn{2}{c}{Länge eines Fahrzeugumlaufs} 
& \multicolumn{2}{c}{\# Elektrobusse} 
& \multicolumn{2}{c}{$H^V$} \\
\cmidrule{2-3} \cmidrule{4-5} \cmidrule{6-7} \cmidrule{8-9} 
    & (K) & (V) & (K) & (V) & (K) & (V) & (K) & (V)\\
    \midrule
    \multicolumn{9}{c}{\textbf{some description}} \\
    \midrule  
    I1 (90) & 44.490.046 & 37.214.853 (16\,\%) & 6.224  
    & 5.992 (4\,\%) & 49 & 15 (69\,\%) & 77 & 76 (1\,\%)\\
\bottomrule
\end{tabular*}
\end{sidewaystable}
\end{document}

答案2

可用宽度在 X 列中均匀分布,因此当这些列的自然宽度不均匀时,表格 X 可能是一个好主意。那么 L、C、R 或 J 列可能更好tabulary。此环境不会尝试占用所有允许的空间,并且在需要时,它会根据其最长条目的比例为每列分配空间。

但是,在这种情况下这还不够,因为您通过 \multicolumns许多列宽度进行修复,超过了 这种情况的最佳解决方案是使用列(需要包)\textwidth.允许在最长的标题中换行。m{}array

另一个缩小整个表格的建议是使用科学计数法来避免 7 位数的数字(真的需要吗?)。使用siunitx包可以自动完成此操作。但您可以将表格缩小约 20%,只允许在值和百分比之间换行。

当然,最好只使用 Mico 建议的水平 booktabs 规则。下图显示了对此 MWE 建议的修改:

\documentclass{article}
\usepackage{tabulary,array,booktabs,xcolor}
\usepackage[showframe]{geometry}
\makeatletter
\def\Gm@hrule{}\def\Gm@hruled{}
\def\Gm@vrule{\color{red!05}\vrule width 2pt height\textheight depth\z@}%
\makeatother
\usepackage{siunitx}
\sisetup{%
scientific-notation=false,
tight-spacing=true,
round-mode=places,
round-precision= 1}
\parindent0pt\parskip2em
\begin{document}

\verb|\begin{tabulary}{\textwidth}{*9C} ... \end{tabulary}|

\begin{tabulary}{\textwidth}{*9C}
\toprule & 
\multicolumn{2}{c}{Gesamtkosten} & 
\multicolumn{2}{c}{Länge eines Fahrzeugumlaufs} & 
\multicolumn{2}{c}{$\#$Elektrobusse} & 
\multicolumn{2}{c}{$H^V$} \\
\cmidrule(rl){2-3}\cmidrule(rl){4-5}\cmidrule(rl){6-7}\cmidrule(rl){8-9}
    & (K) & (V) &  (K) & (V) &  (K) & (V) & (K) & (V)\\
\cmidrule(rl){2-9}
\multicolumn{9}{c}{\textbf{some description}} \\
\cmidrule(rl){2-9}
I1 (90) &
\num{44490046} &
\num{37214853} (16\,\%) &
\num{6224} & 
\num{5992} (4\,\%) &
\num{49} & 
\num{15} (69\,\%) &
\num{77} & 
\num{76} (1\,\%)\\
\bottomrule
\end{tabulary}
\end{document}

姆韦

相关内容