4

I am looking for a way to separate particular columns (together with their headers) with a vertical line.

So far I have the following code and a table output:

\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\small 
\begin{tabular}{@{\extracolsep{1pt}} ccccccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & var-A-1 & var-A-2 & var-B-1 & var-B-2 & var-B-3 & var-C-1 & var-C-2 & var-C-2.1 \\ 
\hline \\[-1.8ex] 
1 & $0.2655$ & $0.8984$ & $0.206$ & $0.4977$ & $0.9347$ & $0.3861$ & $0.4821$ & $0.6685$ \\ 
2 & $0.3721$ & $0.9447$ & $0.1766$ & $0.7176$ & $0.2121$ & $0.0134$ & $0.5996$ & $0.7942$ \\ 
3 & $0.5729$ & $0.6608$ & $0.687$ & $0.9919$ & $0.6517$ & $0.3824$ & $0.4935$ & $0.1079$ \\ 
4 & $0.9082$ & $0.6291$ & $0.3841$ & $0.38$ & $0.1256$ & $0.8697$ & $0.1862$ & $0.7237$ \\ 
5 & $0.2017$ & $0.0618$ & $0.7698$ & $0.7774$ & $0.2672$ & $0.3403$ & $0.8274$ & $0.4113$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

enter image description here

and I would like to end up with something like this:

enter image description here

4
  • 2
    take that: cc|ccc|cccc Commented Oct 18, 2016 at 20:06
  • 1
    Awesome! :D Thank you. I would be happy to mark it as an answer. Commented Oct 18, 2016 at 20:08
  • 1
    0.38 should probably be 0.3800 Commented Oct 19, 2016 at 1:34
  • It was on the spot simulated data (straight from R statistical software), but thanks for your attention! :) Commented Oct 19, 2016 at 10:34

2 Answers 2

8

just place the separator char in the tabular column definition:

cc|ccc|cccc
12

Using vertical lines to provide visual structure to an otherwise little-organized table is a decidedly unsatisfactory way to go. Instead of using vertical rules, do organize the header material more clearly. The following example shows a possible approach; it uses no vertical rules, and it employs the line-drawing macros of the booktabs package instead of \hline. (By the way, I am assuming -- hoping, actually ... -- that the header cells of your real document are a bit less terse than "var-A", "var-B", etc.)

Also, do make sure that the numeric data are aligned on their decimal markers. I suggest you load the siunitx package and use the S column type to achieve this typographic objective.

enter image description here

\documentclass{article}
\usepackage{booktabs,siunitx,caption}
\captionsetup{skip=0.5\baselineskip}

\begin{document}

\begin{table}[!htbp] 
\caption{} 
\label{} 
\setlength\tabcolsep{0pt} % make LaTeX figure out width of inter-column spaces
\begin{tabular*}{\textwidth}{l @{\extracolsep{\fill}}
                            *{8}{S[table-format=1.4]}} 
\toprule
& \multicolumn{2}{c}{var-A} & \multicolumn{3}{c}{var-B} & \multicolumn{3}{c}{var-C} \\
\cmidrule{2-3} \cmidrule{4-6} \cmidrule{7-9}
& {1} & {2} & {1} & {2} & {3} & {1} & {2} & {2.1} \\ 
\midrule
1 & 0.2655 & 0.8984 & 0.206 & 0.4977 & 0.9347 & 0.3861 & 0.4821 & 0.6685 \\ 
2 & 0.3721 & 0.9447 & 0.1766 & 0.7176 & 0.2121 & 0.0134 & 0.5996 & 0.7942 \\ 
3 & 0.5729 & 0.6608 & 0.687 & 0.9919 & 0.6517 & 0.3824 & 0.4935 & 0.1079 \\ 
4 & 0.9082 & 0.6291 & 0.3841 & 0.38 & 0.1256 & 0.8697 & 0.1862 & 0.7237 \\ 
5 & 0.2017 & 0.0618 & 0.7698 & 0.7774 & 0.2672 & 0.3403 & 0.8274 & 0.4113 \\ 
\bottomrule
\end{tabular*} 
\end{table} 

\end{document}
2
  • Excellent advice. I'd further suggest emphasising the separation of the column groups by adding a little inter-column space between them Commented Oct 18, 2016 at 20:34
  • @Brent.Longborough - Thanks! :-) Indeed, if the table contains more than half a dozen or so rows of data, further visual structuring of the material would seem highly advisable.
    – Mico
    Commented Oct 18, 2016 at 20:35

You must log in to answer this question.

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