Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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...

share|improve this question

4 Answers 4

up vote 4 down vote accepted

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}
share|improve this answer
    
This is exactly the same solution as the other answer. And suffers from exactly the same problem: I don't want an extra column. –  Seamus Aug 2 '11 at 10:15
    
Oppss. I cut and pasted from the wrong place, and didn't realize that the other solution had the array version. I think the newcolumntype as recomended by @Danie is the best. But not sure why it needs three changes. I have updated by solution and only required to change to use a R column type instead of @{\extracolsep{3cm}}r@{\extracolsep{0pt}} –  Peter Grill Aug 2 '11 at 17:20
    
Hah. That's clever. I'm just learning about newcoulmntypes... –  Seamus Aug 2 '11 at 17:27

Maybe you can use the trim option of booktabs's \cmidrule to trim the cline. Add the additional space also to the \multicolumn.

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}

\newlength\tbspace
\setlength\tbspace{3cm}
\newcolumntype{L}{l<{\hspace{\tbspace}}}

\begin{document}
    \begin{tabular}{lLrr}
      \multicolumn{2}{L}{Spans} & \multicolumn{2}{r}{Spans} \\
      \cmidrule(r{\tbspace}){1-2} \cmidrule{3-4}
      X & Y & X & Y
    \end{tabular}
\end{document}

enter image description here

share|improve this answer
    
This a better solution, but it still involves three changes! Is there a reason I can't do what I want with just a simple change to the tabular specification? It seems implausible to me that there isn't an easier way! –  Seamus Aug 2 '11 at 13:35

Well, the following seems to work but needs some size adjustments and I am not good at it to do it properly. As required there are no column additions, but I really don't know why it works either. I tend to hack things rather than to code them. Probably one of the gurus here can tell us how to make things robust.

\documentclass{article}
 \usepackage{tabularx}


 \begin{document}

 \begin{tabular*}{\textwidth}{c @{\extracolsep{1cm}}c  c c}
  \multicolumn{2}{c}{Spans} &\multicolumn{2}{c}{Spans} \\
  \cline{1-2} \cline{3-4}
  X & Y & X & Y\\
    XZZZZZZ &YSSSSSS & XZ & RANDOMLETTERCOMBO
\end{tabular*}

\end{document}

Here is a snapshot with a few more letters.

enter image description here

EDIT: I have realized that vertical lines between columns look quite bad and needs more tweaks if you insist using them.

share|improve this answer
    
This is strange: the \extracolsep appears to be in the wrong place! It's after the first column, but the space appears between the second and third. Could this be to do with the multicolumn? –  Seamus Aug 2 '11 at 14:02
    
Yes strange but it works and as I said, I don't know why. –  percusse Aug 2 '11 at 14:04
    
A totally independent solution in the wrong direction, would be opening your file in WinEdt and switching to BLOCK mode from LINE mode. There you can select the & characters from any column as a block and paste it vertically to create easy empty columns.... Just in case of emergency :P –  percusse Aug 2 '11 at 14:11

I would add in one additional column that is used for spacing purposes, and then use the array package to insert a space as required.

\documentclass{article}
\usepackage{array}
\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@{}>{\hspace*{3cm}}crr}
  \multicolumn{2}{r}{Spans} & & \multicolumn{2}{l}{Spans} \\
  \cline{1-2} \cline{4-5}
  X & Y & & X & Y
\end{tabular}
\end{document}​

Spreading columns of specific width

share|improve this answer
    
I don't want to put a new column in. This means a whole bunch of extra &s... And it makes the table code harder to read. –  Seamus Jul 27 '11 at 19:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.