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 have written this code for the table shown bellow on the left but I like to make some modifications so it looks like the table on the right. Can you help me?

\begin{table}[t]\footnotesize\renewcommand{\arraystretch}{1.2}\addtolength{\tabcolsep}{-1pt}
\begin{center}
\begin{tabular}{ c || c | c || c | c ||}
\hline
{\bf Types} & \multicolumn{2}{c|}{\bf Line A} & \multicolumn{2}{c|}{\bf Line B} \\\hline\hline
 & a  & b & a  & b \\\hline
x & d  & 2 & 76  & 2 \\\hline
y & 1 & 3 & 74 & 3 \\\hline\hline
z & \multicolumn{2}{c|}{4a} &\multicolumn{2}{c|}{4a} \\
\hline  
\end{tabular}
\caption{ll}
\label{table:senspec}
\end{center}
\end{table}

enter image description here

share|improve this question
    
Can you please complete your code to a full copy/paste compilable form such that people who would like to help you don't bother with completing your document? –  percusse Sep 27 '12 at 22:36

3 Answers 3

up vote 5 down vote accepted

I was able to get to the following. It still needs improvement as the borders for first and last row do not align properly with the borders of other rows. Also width of two columns under Line A do not match.

enter image description here

\documentclass{article}

\begin{document}

\begin{table}[t]
\centering
\begin{tabular}{| c |@{}c@{\,}| c | c |@{}c@{\,}| c | c |}
    \hline
        \textbf{Types} & \multicolumn{3}{|c|}{\textbf{Line A}} & \multicolumn{3}{c|}{\textbf{Line B}} \\
    \hline 
    \multicolumn{1}{c}{} \\[-0.9\normalbaselineskip]
    \cline{3-4} \cline{6-7}
        \multicolumn{1}{c}{} & & a  & b & & a  & b \\
    \cline{1-1} \cline{3-4} \cline{6-7}
        x & & d  & 2 & &  76  & 2 \\
    \cline{1-1} \cline{3-4} \cline{6-7}
        y & & 1 & 3 & & 74 & 3 \\
    \cline{1-1} \cline{3-4} \cline{6-7}
    \multicolumn{1}{c}{} \\[-0.9\normalbaselineskip]
    \hline
        z & \multicolumn{3}{|c|}{4a} & \multicolumn{3}{c|}{4a} \\
    \hline  
\end{tabular}

\caption{ll}
\label{table:senspec}   
\end{table}

\end{document}
share|improve this answer
    
Thanks so much. –  kevin Sep 28 '12 at 17:28
    
@kevin Glad I was able to help, but would suggest using David's solution with hhline. It is cleaner because it does not introduce unnecessary column like mine. I would have done it that way if I knew about the hhline package. –  mythealias Sep 28 '12 at 18:03

enter image description here

hhline can help here (unrelated but never use \bf in LaTeX they are not defined at all in the format and only included in the standard classes for compatibility with LaTeX 2.09)

\documentclass{article}
\usepackage{array,hhline}

\begin{document}

\begin{table}[t]\footnotesize\renewcommand{\arraystretch}{1.2}\addtolength{\tabcolsep}{-1pt}
\begin{center}
\begin{tabular}{| c || c | c || c | c |}
\hline
\multicolumn{1}{|c|}{\textbf{Types}} & \multicolumn{2}{c|}{\textbf{Line A}} & \multicolumn{2}{c|}{\textbf{Line B}} \\
\hline
\noalign{\vskip\doublerulesep}%
\hhline{~--||--}
\multicolumn{1}{c|}{} & a  & b & a  & b \\
\hhline{-||-|-||-|-|}
x & d  & 2 & 76  & 2 \\
\hhline{-||-|-||-|-|}
y & 1 & 3 & 74 & 3 \\
\hhline{:=:b:=:=:b:=:=:}
\multicolumn{1}{|c|}{z} & \multicolumn{2}{c|}{4a} &\multicolumn{2}{c|}{4a} \\
\hline  
\end{tabular}
\caption{ll}
\label{table:senspec}
\end{center}
\end{table}

\end{document}
share|improve this answer
    
While researching for an answer I stumbled upon some examples that use (your) hhline package but didn't quite get it right. Note to myself: Read at least the manual's first page. –  Qrrbrbirlbel Sep 27 '12 at 23:41
1  
The need to have the \noalign{\vskip is a defficiency in hhline, it doesn't currently allow you to just have the top half of a double rule across a column (t gives you the top section between ||) But otherwise hhline was more or less designed fro this kind of table (despite the general adherence to booktabs on this site:-) –  David Carlisle Sep 27 '12 at 23:44

This is not really an answer to your question, merely a suggestion and an answer:

Notes

  • \bf is a bad idea.
  • You can use \cline{2-3} to only draw a horizontal line from column 2 to 3.
  • The center environment adds vertical space (it's a list). If you don't want this use \centering (like I did in my examples).

What I would suggest

This takes advantages from the excellent booktabs package.
There are the following rules when it comes to this package (and others claim this a valid for good style, too):

  1. Don't use vertical lines.
  2. Obey rule 1.

Code

\documentclass{article}
\usepackage{booktabs}
\renewcommand{\cmidrulekern}{.25em}
\begin{document}
\begin{table}[t]%\footnotesize%\renewcommand{\arraystretch}{1.2}\addtolength{\tabcolsep}{-1pt}
\centering
\begin{tabular}{ccccc}
    \toprule
    \bfseries Types & \multicolumn{2}{c}{\bfseries Line A} & \multicolumn{2}{c}{\bfseries Line B} \\
    \cmidrule(r){2-3}\cmidrule(l){4-5}
                    & a &                b                 & a  &                b                \\
    \midrule
           x        & d &                2                 & 76 &                2                \\
           y        & 1 &                3                 & 74 &                3                \\
    \cmidrule(r){2-3}\cmidrule(l){4-5} % Maybe don't use these cmidrules.
           z        &        \multicolumn{2}{c}{4a}        &        \multicolumn{2}{c}{4a}        \\
    \bottomrule
\end{tabular}
\caption{second way}
\label{table:senspec}
\end{table}
\end{document}

Output

enter image description here

Kind of what you wanted

The vertical lines between "Types" and "Line A" as well as between "Line A" and "Line B" seem to need a little tweaking.
I'm sure someone with more (plain)TeX table experience can help us out here.

Code

\documentclass{article}
\begin{document}
\begin{table}[t]\footnotesize%\renewcommand{\arraystretch}{1.2}\addtolength{\tabcolsep}{-1pt}
\centering
\begin{tabular}{|c|@{\extracolsep{2pt}}c@{\extracolsep{-2pt}}|c|@{\extracolsep{2pt}}c@{\extracolsep{-2pt}}|c|}
\hline
    \multicolumn{1}{|c|}{\bfseries Types} & \multicolumn{2}{c}{\bfseries Line A} & \multicolumn{2}{|c|}{\bfseries Line B} \\
\hline\noalign{\smallskip}
\cline{2-3}\cline{4-5}
            \multicolumn{1}{c}{}          & \multicolumn{1}{|c|}{a} &                b                 & \multicolumn{1}{|c|}{a}  &                 b                 \\
\cline{1-1}\cline{2-3}\cline{4-5}
                      x                   & \multicolumn{1}{|c|}{d} &                2                 & \multicolumn{1}{|c|}{76} &                 2                 \\
\cline{1-1}\cline{2-3}\cline{4-5}
                      y                   & \multicolumn{1}{|c|}{1} &                3                 & \multicolumn{1}{|c|}{74} &                 3                 \\
\cline{1-1}\cline{2-3}\cline{4-5}
\noalign{\smallskip}
\hline
                      z                   &        \multicolumn{2}{c}{4a}        & \multicolumn{2}{|c|}{4a}               \\
\hline
\end{tabular}
\caption{Prediction Accuracy of the Enose System in Lung Cancer Detection}
\label{table:senspec2}
\end{table}
\end{document}

Output

enter image description here

share|improve this answer

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.