如何減少表格中列之間的間距?

如何減少表格中列之間的間距?

還有其他類似的問題,都有不同的答案,但我找不到一個可以實現我想要的目標。在下表中,數字之間的間距太寬。我怎麼才能把它縮小一點?

\newcommand{\testmode}[2] {
    \begin{tabular}{c}
        \textbf{#1} \\ \textbf{d=#2}
    \end{tabular}
}

\begin{table}[ht]
\begin{tabular}{l | c c c | c c c |}
\cline{2-7}
          & \multicolumn{3}{c|}{\textbf{Single}} & \multicolumn{3}{c|}{\textbf{Double}} \\
\cline{2-7}
          & \testmode{Scalar}{1} & \testmode{SSE-4}{4} & \testmode{AVX-2}{8} & \testmode{Scalar}{1} & \testmode{SSE-4}{2} & \testmode{AVX-2}{4} \\
\hline
\multicolumn{1}{|c|}{\textbf{Eytzinger}                        } &     209.86 &     370.18 &     411.25 &     180.85 &     245.37 &     262.61 \\
\multicolumn{1}{|c|}{\textbf{Classic Offset}                    } &     213.28 &     363.35 &     476.10 &     209.04 &     247.02 &     208.63 \\
\hline
\end{tabular}
\caption{Throughput in millions of searches per second with vector $X$ of size 15}
\label{tab:results0}
\end{table}

答案1

tabular在您的範例中,您使用 a inside將列間隔加倍tabular。注意:LaTeX 在\tabcolsep每列之前和之後新增了距離。在您的情況下,您在內部表格列之前和之後\testmode以及外部表格列之前和之後都有這樣的距離。為了避免這種情況,您應該@{}在內部表格的第一列之前和內部表格的最後一列之後新增:

\documentclass{article}

\begin{document}
\newcommand{\testmode}[2] {%
    \begin{tabular}{@{}c@{}}% Avoid doubling \tabcolsep
        \textbf{#1} \\ \textbf{d=#2}
    \end{tabular}%
}

\begin{table}[ht]
\begin{tabular}{l | c c c | c c c |}
\cline{2-7}
          & \multicolumn{3}{c|}{\textbf{Single}} & \multicolumn{3}{c|}{\textbf{Double}} \\
\cline{2-7}
          & \testmode{Scalar}{1} & \testmode{SSE-4}{4} & \testmode{AVX-2}{8} & \testmode{Scalar}{1} & \testmode{SSE-4}{2} & \testmode{AVX-2}{4} \\
\hline
\multicolumn{1}{|c|}{\textbf{Eytzinger}                        } &     209.86 &     370.18 &     411.25 &     180.85 &     245.37 &     262.61 \\
\multicolumn{1}{|c|}{\textbf{Classic Offset}                    } &     213.28 &     363.35 &     476.10 &     209.04 &     247.02 &     208.63 \\
\hline
\end{tabular}
\caption{Throughput in millions of searches per second with vector $X$ of size 15}
\label{tab:results0}
\end{table}
\end{document}

在此輸入影像描述

解釋更多:預設情況下,LaTeX\hskip\tabcolsep在開始列之前和完成列之後會新增。因此,您可以獲得\tabcolsep第一列之前、最後一列之後以及兩列之間的距離2\tabcolsep。如果使用新增@{…}參數的程式碼@而不是距離。所以@{}只需消除距離即可。

附加說明:我建議避免表格中的垂直規則(請參閱手冊包裹booktabs)並使用包裹siunitx對於數字列。我不會將第一個表格行的元素居中,而是將它們左對齊。因為d=…表頭中有類似數學的內容,所以您應該將其設定為數學模式。在這種情況下,我也不會將其設定為粗體(例如使用\boldmath),因為數學中的字體屬性主要具有語義(例如,粗體通常用於向量或集合)。

答案2

我不記得在哪裡找到這個(歡迎編輯參考),但是在語句之前直接添加以下內容\begin{tabular}可以讓您按給定值調整列間距:

\addtolength{\tabcolsep}{-0.4em}

您可以在這裡使用任何單位,但在上面的範例中我使用em,它是相對於當前字體大小的。

答案3

五年後...

現在可以使用表包tabularray來定義列分隔定義colsep選項。與「經典」表格相比,表格的程式碼更短,單元格中的文字垂直間距更好:

\documentclass{article}
\usepackage{tabularray}

\begin{document}
    \begin{table}[ht]
\begin{tblr}{hline{4,Z} = {0.8pt}, vline{1} = {4-Z}{solid},
              colsep = {4pt},  % <--- reduce tabcolsep from 6pt to 4pt
             colspec = {Q[l, font=\bfseries] | *{3}{X[c]} | *{3}{X[c]} |},
            row{1-3} = {font=\bfseries}
            }
    \cline[0.8pt]{2-Z}
    &   \SetCell[c=3]{c}  Single
        &   &   &   \SetCell[c=3]{c}  Double   
                    &   &                               \\
\cline{2-7}
    & Scalar & SSE-4 & AVX-2 & Scalar & SSE-4 & AVX     \\
    & D=1    & D=4   & D=8   & D=1    & D=4   & D=8     \\  
Eytzinger
    &209.86 &370.18 &411.25 & 180.85 & 245.37 & 262.61  \\
Classic Offset
    &213.28 &363.35 &476.10 & 209.04 & 247.02 & 208.63  \\
\end{tblr}
\caption{Throughput in millions of searches per second with vector $X$ of size 15}
\label{tab:results0}
    \end{table}
\end{document}

在此輸入影像描述

答案4

以下是如何使用{NiceTabular}of來完成該表格nicematrix

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\begin{table}[ht]
\setlength{\tabcolsep}{4pt}
\renewcommand{\arraystretch}{1.4}
\begin{NiceTabular}{>{\bfseries}l*{6}{X[c]}}[hvlines,corners=NW]
\RowStyle[bold,nb-rows=3]{}
    &   \Block{1-3}{Single}
        &   &   &   \Block{1-3}{Double} \\
    & Scalar & SSE-4 & AVX-2 & Scalar & SSE-4 & AVX     \\
    & D=1    & D=4   & D=8   & D=1    & D=4   & D=8     \\  
Eytzinger
    &209.86 &370.18 &411.25 & 180.85 & 245.37 & 262.61  \\
Classic Offset
    &213.28 &363.35 &476.10 & 209.04 & 247.02 & 208.63  \\
\end{NiceTabular}
\caption{Throughput in millions of searches per second with vector $X$ of size 15}
\label{tab:results0}
\end{table}
\end{document}

您需要多次編譯(因為nicematrix在背景使用 PGF/Tikz 節點)。

上述程式碼的輸出

相關內容