將表格條目右對齊

將表格條目右對齊

我想調整 a 的列table,使標題居中,其餘行左對齊,這是我自己管理的(見圖)。現在,所有行都有一個附加屬性(長度),我想將其推到每個單元格的最右側。我嘗試使用\hfil,但它們現在位於空間中心(未右對齊)。

我怎樣才能做到這一點?

MWE 補充道:

\documentclass{article}

\begin{document}
    \begin{table}
        \begin{tabular}{|c|l|} \hline
            Item & \multicolumn{1}{c|}{Entries \hfil (length)} \\ \hline
            1 & 1, 2, 3, 4, 5, 6, 7 \hfil (7) \\ \hline
            2 & 1, 2, 3, 4, 5, 6, 7, 8, 9 \hfil (9) \\ \hline
            3 & 1, 2, 3, 4 \hfil (4) \\ \hline
        \end{tabular}
    \end{table}
\end{document}

答案1

為什麼不使用3列呢?另外,您可以使用該booktabs套件:沒有垂直規則和行之間更好的垂直間距。我舉一個兩者的例子:

\documentclass{article}
\usepackage{booktabs}

\begin{document}

\begin{table}
    \begin{tabular}[b]{|c|lc|} \hline
        Item & \multicolumn{1}{c}{Entries} & (length) \\ \hline
        1 & 1, 2, 3, 4, 5, 6, 7 & (7) \\ \hline
        2 & 1, 2, 3, 4, 5, 6, 7, 8, 9 & (9) \\ \hline
        3 & 1, 2, 3, 4 & (4) \\ \hline
    \end{tabular}
\end{table}
\mbox{}%\vskip1cm%
\begin{table}[! h]
    \begin{tabular}[b]{@{}clc@{}}
        Item & \multicolumn{1}{c}{Entries} & (length) \\\addlinespace[0.5ex] \toprule
        1 & 1, 2, 3, 4, 5, 6, 7 & (7) \\ \addlinespace
        2 & 1, 2, 3, 4, 5, 6, 7, 8, 9 & (9) \\ \addlinespace
        3 & 1, 2, 3, 4 & (4) \\ \bottomrule
    \end{tabular}
\end{table}

\end{document} 

在此輸入影像描述

答案2

我將這樣做:

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{table}
\centering
  \begin{tabular}{clc}
   \toprule
    Item & \multicolumn{1}{c}{Entries} & (length) \\
   \midrule
    $1$  & $1,2,3,4,5,6,7$             & $(7)$    \\
    $2$  & $1,2,3,4,5,6,7,8,9$         & $(9)$    \\
    $3$  & $1,2,3,4$                   & $(4)$    \\
   \bottomrule
  \end{tabular}
\end{table}

\end{document}

輸出

(我現在看到,它與伯納德的最後一個解決方案類似,但在\toprule表格上方,\midrule在文本下方。)

相關內容