[Tex/LaTex] Add extra space only between two specific columns of a tabular without adding extra columns

spacingtables

I am trying to move two columns of a tabular apart and running into difficulty. I tried adding \hspace between the columns with the @{...} syntax, but multicolumn seems to ignore that this spacing is supposed to be between columns as can be seen from the first tabular in the following MWE:

\documentclass{article}
\begin{document}

\begin{tabular}{ll@{\hspace{3cm}}rr}
  \multicolumn{2}{r}{Spans} & \multicolumn{2}{l}{Spans} \\
  \cline{1-2} \cline{3-4}
  X & Y & X & Y
\end{tabular}

\begin{tabular}{ll@{\extracolsep{3cm}}r@{\extracolsep{0pt}}r}
  \multicolumn{2}{r}{Spans} & \multicolumn{2}{l}{Spans} \\
  \cline{1-2} \cline{3-4}
  X & Y & X & Y
\end{tabular}
\end{document}

The multicolumn is aligned right and it goes all the way over to where column 3 starts. Likewise, the \cline goes all the way over too.

I can achieve roughly what I want with \extracolsep, but the problem here is that I have to set it back to zero manually, otherwise, I get loads of extra space between columns 3 and 4 too.

multicolumns break inter column specifications

Is there a way that gets me the simplicity of just setting the one thing (à la the top example, but actually behaves how I expect with multicolumns and clines (à la the bottom example)?

I'd like to do this without adding superfluous columns to my tables. That is, my table currently has four columns each with something in the cells. I can achieve what I want by adding a fifth column in the middle and have the spacing there, and change all the corresponding \clines to have the new numbers. I find this solution inelegant, and difficult to implement for an already existing large table…

Best Answer

Using the array package you can specify a command to be executed before each element with >{\command} and after each element <{command}. So, the following should close to what you want.

\documentclass{article}
\usepackage{array}

\newcolumntype{R}{@{\extracolsep{3cm}}r@{\extracolsep{0pt}}}%

\begin{document}
\begin{tabular}{llRr}
  \multicolumn{2}{r}{Spans} & \multicolumn{2}{l}{Spans} \\
  \cline{1-2} \cline{3-4}
  X & Y & X & Y
\end{tabular}
\end{document}