# How to increase the gap between only 2 columns in a tabular without skewing the centering?

### The Problem

How to adjust the gap between just two columns in a tabular environment while automatically retaining the centering of headings?

I know the length columnsep is used to adjust the separation between all the columns in a tabular but I just want to increase the gap between two while keeping the correct centering of the column headers and footers.

I have seen this answer and this answer. The answer to the latter breaks row coloring and I couldn't work out how to save then restore the column gap correctly. The answer to the former uses a manually inserted, fixed \hspace* to force one heading to center which is presumably the result of a manual calculation or trial and error. I am seriously hoping to have LaTeX or TeX calculate the centering because the headings and footers will differ between tables and there will be a lot of tables in the finished document. I have included my attempts at utilizing those answers below.

### Requirements

I believe that vertical lines in tables are an abomination unto Nuggan and costs you 42 reputation points, so whitespace between columns is the way forward.

I wish to avoid having a dummy column because I'll be copying and pasting a lot of data into a lot of tables. For the same reason I wish to avoid having to manually calculate an \hspace* for at least one heading in every table. Some, probably many, tables will have more logical groups of columns.

For many tables, one or more rows of data will be colored. For this example I have colored the first line of the heading for simplicity. Row coloring must still work without creating a vertical gap between columns. A white gap probably wouldn't matter for headings but it will matter for the rows of data. All the columns apply to the same piece of data occupying an entire row. The gap is to visually separate logical groups of columns.

The full document uses LuaLaTeX with fontspec. I tried XeTeX for a while but had a meltdown with the fonts especially for math. Fixing the tables in LuaLaTeX seemed a much smaller problem.

MWE

\PassOptionsToPackage{table,usenames,svgnames}{xcolor}

\documentclass[oneside,11pt]{article}
\usepackage{array,booktabs,xcolor}
\usepackage[mode=math]{siunitx}

\setlength{\columnsep}{2em}
\newlength{\interclasscolspace}\setlength{\interclasscolspace}{48pt}
\newcommand{\clscolsp}{\hspace*{\interclasscolspace}}

\begin{document}
\begin{table}[!htp]
\centering
\caption[Basic table]{The basic table with equal distances between
all columns. How to increase the gap between column 1C and 2A?
Extra~1 and Extra~2 should remain centered under their respective
B~columns and First Group and Second Group should remain centered
over their three columns.}
\label{basic}
\small
\begin{tabular}{c*{6}{S[table-format=1.3]}}
\toprule
\rowcolor{lightgray}
& \multicolumn{3}{c}{\textbf{First Group}} &
\multicolumn{3}{c}{\textbf{Second Group}}\\
{\textbf{Rank}} &
{\textbf{1A}} &
{\textbf{1B}} &
{\textbf{1C}} &
{\textbf{2A}} &
{\textbf{2B}} &
{\textbf{2C}}\\
\midrule
\enspace1 & 0.657 & 0.913 & 0.733 & 0.830 & 0.387 & 0.893\\
\enspace2 & 0.343 & 0.537 & 0.655 & 0.690 & 0.471 & 0.333\\
\enspace9 & 0.783 & 0.885 & 0.015 & 0.306 & 0.643 & 0.263\\
10 & 0.161 & 0.708 & 0.386 & 0.257 & 0.074 & 0.336\\
\midrule
& \multicolumn{3}{c}{\textbf{Extra~1}} &
\multicolumn{3}{c}{\textbf{Extra~2}}\\
\bottomrule
\end{tabular}
\end{table}

\begin{table}[!htp]
\centering
\caption[exclaim table]{This table uses ! with
\texttt{\textbackslash{}qquad}. It creates the coloring and column
spacing correctly but breaks the centering of the First Group
\label{exclaim-command}
\small
\begin{tabular}{c*{3}{S[table-format=1.3]}%
*{2}{S[table-format=1.3]}}
\toprule
\rowcolor{lightgray}
& \multicolumn{3}{c}{\textbf{First Group}} &
\multicolumn{3}{c}{\textbf{Second Group}}\\
{\textbf{Rank}} &
{\textbf{1A}} &
{\textbf{1B}} &
{\textbf{1C}} &
{\textbf{2A}} &
{\textbf{2B}} &
{\textbf{2C}}\\
\midrule
\enspace1 & 0.657 & 0.913 & 0.733 & 0.830 & 0.387 & 0.893\\
\enspace2 & 0.343 & 0.537 & 0.655 & 0.690 & 0.471 & 0.333\\
\enspace9 & 0.783 & 0.885 & 0.015 & 0.306 & 0.643 & 0.263\\
10 & 0.161 & 0.708 & 0.386 & 0.257 & 0.074 & 0.336\\
\midrule
& \multicolumn{3}{c}{\textbf{Extra~1}} &
\multicolumn{3}{c}{\textbf{Extra~2}}\\
\bottomrule
\end{tabular}
\end{table}

\begin{table}[!htp]
\centering
\caption[at-command table]{This table uses @ with
\texttt{\textbackslash{}extracolsep}. It breaks the row coloring,
all of the centering and the gap between 2B and 2C. Ideally it
should save the initial extracolsep before changing it then
restore it but I don't know how.}
\label{at-command}
\small
\begin{tabular}{c*{3}{S[table-format=1.3]}%
@{\extracolsep{3em}}S[table-format=1.3]%
@{\extracolsep{1em}}S[table-format=1.3]%
S[table-format=1.3]}
\toprule
\rowcolor{lightgray}
& \multicolumn{3}{c}{\textbf{First Group}} &
\multicolumn{3}{c}{\textbf{Second Group}}\\
{\textbf{Rank}} &
{\textbf{1A}} &
{\textbf{1B}} &
{\textbf{1C}} &
{\textbf{2A}} &
{\textbf{2B}} &
{\textbf{2C}}\\
\midrule
\enspace1 & 0.657 & 0.913 & 0.733 & 0.830 & 0.387 & 0.893\\
\enspace2 & 0.343 & 0.537 & 0.655 & 0.690 & 0.471 & 0.333\\
\enspace9 & 0.783 & 0.885 & 0.015 & 0.306 & 0.643 & 0.263\\
10 & 0.161 & 0.708 & 0.386 & 0.257 & 0.074 & 0.336\\
\midrule
& \multicolumn{3}{c}{\textbf{Extra~1}} &
\multicolumn{3}{c}{\textbf{Extra~2}}\\
\bottomrule
\end{tabular}
\end{table}
\end{document}


### Other attempts

I have tried using > and < but they create one Overfull \hbox message for every table row plus they complete mess up the centering. I have also tried adding \hspace and \hspace* to the column headings but that also messes up the centering.

### Bonus material

If there any optimizations I can make to the LaTeX code please let me know. Having \enspace before single digits to make them right align to double digits in a centered column seems a likely candidate.

### Some system details

• LuaHBTeX, Version 1.12.0 (MiKTeX 2.9.7440 64-bit)
• array 2019/08/31 v2.4l
• booktabs 2020/01/12 v1.61803398
• fontspec 2020/02/21 v2.7i
• xcolor 2016/05/11 v2.12

# Update

Using @Bernard's solution but with a colored row:

\PassOptionsToPackage{table,svgnames}{xcolor}

\documentclass[oneside,11pt]{article}
\usepackage{array,booktabs,xcolor}
\usepackage[mode=math]{siunitx}

\setlength{\columnsep}{2em}

\begin{document}
\begin{table}[!htp]
\centering\setlength{\extrarowheight}{2pt}
\arrayrulecolor{LightSlateGray}
\caption[exclaim table]{This table uses ! with
\texttt{\textbackslash{}qquad}. It creates the colouring and column
spacing correctly and compensates the centring of the First Group
\label{exclaim-command}
\small
\begin{tabular}{S[table-format=2.0]*{3}{S[table-format=1.3]}%
*{2}{S[table-format=1.3]}}
\toprule
\multicolumn{3}{c}{\textbf{Second Group}}\\
{\textbf{Rank}} &
{\textbf{1A}} &
{\textbf{1B}} &
{\textbf{1C}} &
{\textbf{2A}} &
{\textbf{2B}} &
{\textbf{2C}}\\
\midrule
1 & 0.657 & 0.913 & 0.733 & 0.830 & 0.387 & 0.893\\
\rowcolor{LightSlateGray!25!Lavender}%{Gainsboro!70!Lavender}
2 & 0.343 & 0.537 & 0.655 & 0.690 & 0.471 & 0.333\\
9 & 0.783 & 0.885 & 0.015 & 0.306 & 0.643 & 0.263\\
10 & 0.161 & 0.708 & 0.386 & 0.257 & 0.074 & 0.336\\
\midrule
\multicolumn{3}{
c}{\textbf{Extra~2}}\\
\bottomrule
\end{tabular}
\end{table}
\end{document}


You can add an empty column between 'First Group' and 'Second Group'.

\PassOptionsToPackage{table,usenames,svgnames}{xcolor}
\documentclass[oneside,11pt]{article}
\usepackage{array,booktabs,xcolor}
\usepackage[mode=math]{siunitx}

\begin{document}
\small
\begin{tabular}{S[table-format=2.0]*{3}{S[table-format=1.3]}c*{3}{S[table-format=1.3]}}
\toprule
\rowcolor{lightgray}
& \multicolumn{3}{c}{\textbf{First Group}} &&
\multicolumn{3}{c}{\textbf{Second Group}}\\
{\textbf{Rank}} &
{\textbf{1A}} &
{\textbf{1B}} &
{\textbf{1C}} &&
{\textbf{2A}} &
{\textbf{2B}} &
{\textbf{2C}}\\
\midrule
1 & 0.657 & 0.913 & 0.733 && 0.830 & 0.387 & 0.893\\
2 & 0.343 & 0.537 & 0.655 && 0.690 & 0.471 & 0.333\\
9 & 0.783 & 0.885 & 0.015 && 0.306 & 0.643 & 0.263\\
10 & 0.161 & 0.708 & 0.386 && 0.257 & 0.074 & 0.336\\
\midrule
& \multicolumn{3}{c}{\textbf{Extra~1}} &&
\multicolumn{3}{c}{\textbf{Extra~2}}\\
\bottomrule
\end{tabular}

\end{document}


If you want the gray color going until the \toprule, you should use nicematrix (≥ 4.0, 2020-05-08).

\PassOptionsToPackage{table,usenames,svgnames}{xcolor}
\documentclass[oneside,11pt]{article}
\usepackage{nicematrix}
\usepackage{array,booktabs,xcolor}
\usepackage[mode=math]{siunitx}

\begin{document}
\small
\begin{NiceTabular}{S[table-format=2.0]*{3}{S[table-format=1.3]}C*{3}{S[table-format=1.3]}}%
[code-before = \rowcolor{lightgray}{1}]
\toprule
& \multicolumn{3}{C}{\textbf{First Group}} &&
\multicolumn{3}{C}{\textbf{Second Group}}\\
{\textbf{Rank}} &
{\textbf{1A}} &
{\textbf{1B}} &
{\textbf{1C}} &&
{\textbf{2A}} &
{\textbf{2B}} &
{\textbf{2C}}\\
\midrule
1 & 0.657 & 0.913 & 0.733 && 0.830 & 0.387 & 0.893\\
2 & 0.343 & 0.537 & 0.655 && 0.690 & 0.471 & 0.333\\
9 & 0.783 & 0.885 & 0.015 && 0.306 & 0.643 & 0.263\\
10 & 0.161 & 0.708 & 0.386 && 0.257 & 0.074 & 0.336\\
\midrule
& \multicolumn{3}{C}{\textbf{Extra~1}} &&
\multicolumn{3}{C}{\textbf{Extra~2}}\\
\bottomrule
\end{NiceTabular}

\end{document}


• Thanks. As I said in my question I'd already tried and dismissed the extra column technique because of the amount of tables I'll be making. It's unfortunate because it works the best for one table. I feel the answer for me will use !. +1 for the tip on coloring the header. For these tables I'll be coloring rows not headers but I have saved your code for the headers in case I ever do :) Jun 6 '20 at 21:14
• I've just come across the pgfplotstable package. So if I can get that to add the empty column automatically then all my table layout problem will be solved. Jun 6 '20 at 21:33

You perfectly may use you second solution, if you compensate for the centring from within the multicolumns. I also suggest some modifications to the layout: personally, I would remove the \toprule, which doesn't look so nice, from my point of view, in the neighbourhood of a coloured row, especially with the small spacing. I also think the rules should be greyed to match the coloured row.

I also simplified the code for the first column: using the S type (with another number format) makes unnecessary adding \enspaces. Last: the usenames option for xcolor can be removed as it is the default.

\PassOptionsToPackage{table,svgnames}{xcolor}

\documentclass[oneside,11pt]{article}
\usepackage{array, booktabs, xcolor, caption}
\usepackage[mode=math]{siunitx}

\setlength{\columnsep}{2em}
\newlength{\interclasscolspace}\setlength{\interclasscolspace}{48pt}
\newcommand{\clscolsp}{\hspace*{\interclasscolspace}}

\begin{document}

\begin{table}[!htp]
\centering\setlength{\extrarowheight}{2pt}
\arrayrulecolor{LightSlateGray}
\caption[exclaim table]{This table uses ! with
\texttt{\textbackslash{}qquad}. It creates the colouring and column
spacing correctly and compensates the centring of the First Group
\label{exclaim-command}
\small
\begin{tabular}{S[table-format=2.0]*{3}{S[table-format=1.3]}%
*{2}{S[table-format=1.3]}}
\rowcolor{LightSlateGray!25!Lavender}%{Gainsboro!70!Lavender}
\multicolumn{3}{c}{\textbf{Second Group}}\\
{\textbf{Rank}} &
{\textbf{1A}} &
{\textbf{1B}} &
{\textbf{1C}} &
{\textbf{2A}} &
{\textbf{2B}} &
{\textbf{2C}}\\
\midrule
1 & 0.657 & 0.913 & 0.733 & 0.830 & 0.387 & 0.893\\
2 & 0.343 & 0.537 & 0.655 & 0.690 & 0.471 & 0.333\\
9 & 0.783 & 0.885 & 0.015 & 0.306 & 0.643 & 0.263\\
10 & 0.161 & 0.708 & 0.386 & 0.257 & 0.074 & 0.336\\
\midrule
\multicolumn{3}{
c}{\textbf{Extra~2}}\\
\bottomrule
\end{tabular}
\end{table}

\end{document}


Edit:

For the new case posted, you can use this hack:

\begin{tabular}{S[table-format=2.0]*{3}{S[table-format=1.3]}%
*{2}{S[table-format=1.3]}}
\toprule
\multicolumn{3}{c}{\textbf{Second Group}}\\
{\textbf{Rank}} &
{\textbf{1A}} &
{\textbf{1B}} &
{\textbf{1C}} &
{\textbf{2A}} &
{\textbf{2B}} &
{\textbf{2C}}\\
\midrule
1 & 0.657 & 0.913 & 0.733 & 0.830 & 0.387 & 0.893\\
\rowcolor{LightSlateGray!25!Lavender}%
2 & 0.343 & 0.537 & 0.655 {\color{LightSlateGray!25!Lavender}\rlap{\rule[-2\jot]{2.6em}{6\jot}}}& 0.690 & 0.471 & 0.333\\
9 & 0.783 & 0.885 & 0.015 & 0.306 & 0.643 & 0.263\\
10 & 0.161 & 0.708 & 0.386 & 0.257 & 0.074 & 0.336\\
\midrule
\multicolumn{3}{
c}{\textbf{Extra~2}}\\
\bottomrule
\end{tabular}


• Thank you @Bernard. Multiple +1s for the extra touches in your answer. The S type for the first column is excellent. I agree with you about the colors and horizontal rules. In the real tables it will be just a few rows of actual data that are colored, never the headers. I colored the headers in my question only to show that some solutions break the coloring. Unfortunately trying a colored row with your solution puts a big white gap between the groups. Do you have any suggestions? I am using caption in my main document, I just wanted to keep the MWE minimal. Not seen \null in LaTeX before. Jun 6 '20 at 22:58
• Probably, something in your real code interacts with the code for my solution. Could you post a minimal code reproducing the problem? Jun 6 '20 at 23:03
• I've updated my original question to show a graphic of making a row colored using your \qquad solution. I've put the whole LaTeX code there too. If it works for you it must be a difference in LaTeX versions or package versions. I've put my specs at the bottom of my question. Thanks again. Jun 6 '20 at 23:27
• I also have the problem. My solution used the fact that we have \multicolumns, which is no more the case in normal rows. Here's a dirty hack: add, just after the entry in the last cell before the gap (0.655) this code: {\color{LightSlateGray!25!Lavender}\rlap{\rule[-2\jot]{2.6em}{6\jot}}} (of course the numerical values might have to be adjusted in other cases). Jun 6 '20 at 23:31
• \jot is a length used in the multilined amsmath environment, to separate lines. Note the values used for the height and depth give you control on the height of the coloured background. As to \rlap, it is a plain TeX command: it inserts its contents on the right as though it had 0 length, so doesn't modify the alignment of the number before. There exists a similar \llap command, and math versions are defined by mathtools under the names of \mathrlap and \mathllap. Jun 6 '20 at 23:52

With the latest version of nicematrix (v. 4.3 2020/06/12), you can acheive the desired result directly:

\documentclass{article}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{siunitx}

\begin{document}
\small
[ code-before = \rowcolor[gray]{0.9}{4}]
\toprule
& \Block{1-3}{\textbf{First Group}} & & &
\Block{1-3}{\textbf{Second Group}}\\
{\textbf{Rank}} &
{\textbf{1A}} &
{\textbf{1B}} &
{\textbf{1C}} &
{\textbf{2A}} &
{\textbf{2B}} &
{\textbf{2C}}\\
\midrule
1 & 0.657 & 0.913 & 0.733 & 0.830 & 0.387 & 0.893\\
2 & 0.343 & 0.537 & 0.655 & 0.690 & 0.471 & 0.333\\
9 & 0.783 & 0.885 & 0.015 & 0.306 & 0.643 & 0.263\\
10 & 0.161 & 0.708 & 0.386 & 0.257 & 0.074 & 0.336\\
\midrule
& \Block{1-3}{\textbf{Extra~1}} & & &
\Block{1-3}{\textbf{Extra~2}}\\
\bottomrule
\end{NiceTabular}

\end{document}


You need two compilations.

• Excellent, thanks @F.Pantigny. I hadn't heard of nicematrix before you mentioned it. Jun 14 '20 at 23:00

This version answers my original question. It was inspired by the answers from @f-pantigny and @Bernard for this question, plus answers to different questions by @Jake and @percusse, with some general tips from answers by @Qrrbrbirlbel and @ChristianFeuersänger.

If you don't need colored rows, then @Bernard's answer using ! is for you. If you do need colored rows but don't need to center some multicolumn text then >{\hspace*{...}} or <{\hspace*{...}} will work.

If you need colored rows and centered multicolumns or have a lot of tables to produce then sit comfortably and read on.

The cleanest way I could find to add one inter-column space without breaking the ability to color a data row was to add an extra column. That was my very first attempt at a solution. However for the number of tables I have to create manually adding empty columns would be at best inadvisable and at worst impracticable. So I posted a question here to hopefully elicit a better way assuming I'd still have to make the tables manually from copying and pasting and LaTeXing the data. @f-pantigny's answer forced me to consider a way of automating table production. That has led me to learn about pgfplotstable. I may be the only LaTeX user who'd never heard of it until now.

I have now spent a few hours using pgfplotstable and have solved my problem in a way better than I'd originally hoped. It adds the blank column automatically and loads the data directly so the amount of manual work and subsequent probability of error are massively reduced. Improvements to my first attempt at coding a pgfplotstable are welcome.

The only thing I haven't figured out is how to read the values passed to highlighted row iterator/.list from a macro or ideally from a file.

# MWE

\PassOptionsToPackage{table,svgnames}{xcolor}

\documentclass[oneside,11pt]{article}
\usepackage{array,booktabs,caption,xcolor}
\usepackage[mode=math]{siunitx}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
% Set defaults, though they can be overriden for individual columns.
\pgfplotstableset{precision=3,fixed,fixed zerofill}

% I'm not sure changing this length affects pgfplotstables.
\setlength{\columnsep}{2em}

\begin{document}

% Create the example data, although in reality data are loaded from files
0.807   0.425   0.742   0.094   0.124   0.205
0.703   0.800   0.655   0.161   0.959   0.977
0.985   0.516   0.999   0.908   0.158   0.084
0.109   0.478   0.637   0.239   0.075   0.075
0.063   0.117   0.206   0.053   0.166   0.435
0.815   0.899   0.483   0.923   0.005   0.744
0.322   0.451   0.499   0.066   0.789   0.248
0.221   0.302   0.736   0.637   0.118   0.949
0.283   0.657   0.373   0.901   0.106   0.255
0.217   0.199   0.624   0.929   0.205   0.239
0.778   0.155   0.603   0.219   0.538   0.125
0.496   0.064   0.786   0.116   0.402   0.460

\begin{table}[!htp]
\centering\setlength{\extrarowheight}{2pt} % @Bernard
\caption[pgfplotstable table]{Table constructed using
pgfplotstable.}
\label{pgfplotstable1}
\small
\pgfplotstabletypeset[
assign column name/.code=\pgfkeyssetvalue{/pgfplots/table/column name}{\textbf{#1}},
before row={
\toprule
& \multicolumn{3}{c}{\textbf{First Group}} && % spacer column
\multicolumn{3}{c}{\textbf{Second Group}}\\
},
after row={\midrule}
},
% @percusse
highlighted row iterator/.style={every row no #1/.style={
before row={
\rowcolor{LightSlateGray!25!Lavender} % @Bernard
}
}
},
% rows to color, would like to get the values from a macro or ideally a file
highlighted row iterator/.list={1,7}, % @percusse
every last row/.style={
after row={
\midrule
& \multicolumn{3}{c}{\textbf{Extra~1}} && % spacer column
\multicolumn{3}{c}{\textbf{Extra~2}}\\
\bottomrule
}
},
columns/rank/.style={assign column name=Rank,%
% stop siunitx complaining about exponents
numeric as string type,
column type={S[table-format=2.0]}
},
columns/0/.style={assign column name=1A},
columns/1/.style={assign column name=1B},
columns/2/.style={assign column name=1C},
columns/3/.style={assign column name=2A},
columns/4/.style={assign column name=2B},
columns/5/.style={assign column name=2C},
columns/blank/.style={string type,
assign column name={}
},
create on use/rank/.style={create col/expr={\pgfplotstablerow+1} % @Jake
},
create on use/blank/.style={create col/set={\hspace*{2em}} % size of spacer column
},
columns={rank,0,1,2,blank,3,4,5}
]\loadedtable  % In production {filename.txt} would be used
\end{table}
\end{document}


Some system details

• LuaHBTeX, Version 1.12.0 (MiKTeX 2.9.7440 64-bit)
• array 2019/08/31 v2.4l
• booktabs 2020/01/12 v1.61803398
• fontspec 2020/02/21 v2.7i
• pgfplotstable 2020/02/29 v1.17
• xcolor 2016/05/11 v2.12