6

There are other similar questions, all with different answers, but I could not find one which achieves what I want. In the table below, the spacing between the numbers is too wide. How can I shrink it a bit?

\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}
3
  • You can adjust \tabcolsep or write something like @{\hskip.05cm} between the column definitions. But to really help you it would be greatly appreciated to see a MWE.
    – TeXnician
    Jun 30, 2017 at 6:01
  • Please always add a minimal but working example (MWE) instead of only a code snippet. A compilable examples makes it easier to reproduce your problem without need for any speculation about additional needed or related code. So please help us to help you. Jun 30, 2017 at 6:13
  • @TeXnician and @Jürgen: I think, the main problem is doubling \tabcolsep with the inner tabular (see my answer) and therefore no need to reduce the column separation of the outer tabular. Jun 30, 2017 at 6:27

4 Answers 4

5

In your example you have doubled the column separation using a tabular inside a tabular. Note: LaTeX adds a distance of \tabcolsep before and after each column. In your case you have such a distance before and after the column of the inner tabular of \testmode and before and after the column of the outer tabular. To avoid this, you should add @{} before the first column of the inner tabular and after the last column of the inner tabular:

\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}

enter image description here

To explain some more: By default LaTeX adds \hskip\tabcolsep before it starts a column and after it has finished a column. So you have the distance \tabcolsep before the first and after the last column and 2\tabcolsep between two columns. If you use @{…} the code of the argument of @ is added instead of the distance. So @{} just removes the distance.

Additional note: I would recommend to avoid vertical rules in tables (see the manual of package booktabs) and to use package siunitx for number columns. And I would not center the elements of the first table row but left-align them. And because d=… in the table head is something like math, you should set it in math mode. In this case I also would not make it bold (using, e.g., \boldmath), because font attributes in math have mostly a semantic (e.g, bold is often used for vectors or sets).

2
  • 3
    +1. I would also change \textbf{d=#2} to $\mathbf{d=#2}$ in the definition of the \testmode macro.
    – Mico
    Jun 30, 2017 at 6:16
  • Thank you! Now I understand. In reality I do not even need the nested table, I can just have two rows!
    – Fabio
    Jun 30, 2017 at 6:55
9

I can't remember where I found this (edits with a reference are welcome), but adding the following directly before your \begin{tabular} statement can allow you to adjust the column spacing by a given value:

\addtolength{\tabcolsep}{-0.4em}

You can use any unit here, but in the example above I use em, which is relative to the current font size.

0

After five years ...

Now is available table package tabularray which for defining of column separation define colsep option. In comparison with "classic" table's code is shorter and text in cells better vertical spaced:

\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}

enter image description here

0

Here is how you can do that tabular with {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}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .