2

I am learning LaTeX while working on my lab report, which might sound stupid but I have to start somewhere before my thesis. Anyhow, I am trying to code the following type of table into my LaTeX document:

A kind of nice looking table

   \begin{table}[htbp]
    \caption{Ct-value}
    \label{tab:Ct}
    \centering
    \begin{tabular}{C{1.75cm} *{8}{C{1 cm}} }
        \toprule
        \multicolumn{4}{p{4cm}}{DETECTION OF CT}
        \\
        \cmidrule(lr){3-6} 
        &   
        \multicolumn{3}{p{3cm}}{Matrix-PCR} &
        \multicolumn{2}{p{2cm}}{H5-PCR} &
        \multicolumn{2}{p{2cm}}{H7-PCR} & 
        \\
        \cmidrule(lr){2-4} \cmidrule(lr){5-6} \cmidrule(lr){7-8}
        Sample &
        G1 & G2 & G3 &
        G4 & G5 &
        G6 & G7 &
        \\
        \cmidrule(lr){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7} \cmidrule(lr){8-8} 
        Neg         &   -       &   -       &   -       &   -       &   -       &   -       &   -       \\        
        102685      &   26,25   &   26,56   &   25,93   &   -       &   -       &   -       &   -       \\        
        102420      &   -       &   -       &   -       &   -       &   -       &   -       &    27,72 \\         
        102696      &   23,93   &   25.93   &   23,54   &   -       &   -       &   23,25   &   -       \\        
        102721      &   21,48   &   21,71   &   -       &   24,40   &   22,78   &   -       &   -       \\        
        H5 Pos      &   19,02   &   -       &   18,79   &   21,92   &   20,35   &   -       &   -       \\        
        H5 1:10     &   23,98   &   -       &   22,84   &   27,38   &   23,79   &   -       &   -       \\        
        H5 1:100    &   27,80   &   -       &   27,17   &   -       &   -       &   -       &   -       \\
        H7 Pos      &   -       &   23,36   &   -       &   -       &   -       &   20,39   &   21,90   \\
        H7 1:10     &   -       &   27,87   &   -       &   -       &   -       &   24,97   &   28,35   \\
        H7 1:100    &   -       &   32,27   &   -       &   -       &   -       &   28,98   &   31,68   \\
\bottomrule
\end{tabular}
\end{table}


\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{natbib}
\usepackage{url}
\usepackage[utf8x]{inputenc}
\usepackage{dcolumn}
\usepackage{amsmath}
\usepackage{graphicx}
\graphicspath{{images/}}
\usepackage{parskip}
\usepackage{fancyhdr}
\usepackage{vmargin}
\usepackage{multirow,booktabs,colortbl,tabularx}
\definecolor{LightGray}{gray}{.8}

I am confident...that I am doing this the "wrong" and more difficult way, but I am so close to completing this table and it has been quite a nice learning process. However, I have some problems I can't solve listed below.

PROBLEMS: 1. The caption is not to the left as I want it to be. 2. I have difficulty with centering titles above their lines. 3. I don't know how to add more space between each title (latitude/height). 4. Looks very unorganized below 'Sample' column, I want everything to the left.
5. Top- and bottom rule ends 1-2 cm to0 late. Also, I would like to change their thickness.

  • 1
    Please make your code snippet compilable, for example where does columntype C comes from? – Mensch Jan 23 '18 at 23:38
1

Here's a solution which (a) tries to organize the header material more clearly and (b) uses extra whitespace between some of the data rows in order to provide more visual grouping and, hopefully, make the material more visually appealing. Observe that it's not necessary to specify any column widths -- the l and c column types are doing just fine.

enter image description here

\documentclass[12pt]{article}
% I simplified preamble to focus on the bare essentials
\usepackage{booktabs}
\usepackage[skip=0.333\baselineskip]{caption}

\begin{document}
\begin{table}[htbp!]
\centering
\caption{Ct-value}
\label{tab:Ct}
\begin{tabular}{@{} l *{7}{c} @{}}
\toprule
Sample & \multicolumn{7}{c@{}}{Detection of CT}\\
\cmidrule(l){2-8} 
& \multicolumn{3}{c}{Matrix-PCR} 
& \multicolumn{2}{c}{H5-PCR} 
& \multicolumn{2}{c@{}}{H7-PCR} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-6} \cmidrule(l){7-8}
& G1 & G2 & G3 & G4 & G5 & G6 & G7 \\
\midrule
Neg      & -- & -- & -- & -- & -- & -- & -- \\
102685   & 26,25 & 26,56 & 25,93 & -- & -- & -- & -- \\
102420   & -- & -- & -- & -- & -- & -- &  27,72 \\ 
102696   & 23,93 & 25.93 & 23,54 & -- & -- & 23,25 & -- \\
102721   & 21,48 & 21,71 & -- & 24,40 & 22,78 & -- & -- \\
\addlinespace
H5 Pos   & 19,02 & -- & 18,79 & 21,92 & 20,35 & -- & -- \\
H5 1:10  & 23,98 & -- & 22,84 & 27,38 & 23,79 & -- & -- \\
H5 1:100 & 27,80 & -- & 27,17 & -- & -- & -- & -- \\
\addlinespace
H7 Pos   & -- & 23,36 & -- & -- & -- & 20,39 & 21,90 \\
H7 1:10  & -- & 27,87 & -- & -- & -- & 24,97 & 28,35 \\
H7 1:100 & -- & 32,27 & -- & -- & -- & 28,98 & 31,68 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
  • Note that I don't think it's a good idea to left-align the caption for this table. – Mico Jan 24 '18 at 1:05
2

1) Are you sure that do you want a left aligned short caption with a centered table?

2) Why do you want specify the width of columns when all rows fit perfectly into the text width?

BTW, to fit in long cells in several rows you can use p{<length>} or m{<length>} (with the array package) or C and others with tabulary package, but not C{<length>} unless defined elsewhere (e.g. \newcolumntype{C}{>{\hfil}m} in the preamble). That is why why need a MWE. Otherwise we can only guess what the hell does such a command as \toprule (it is not standard LaTEX, and maybe the people do not know the booktabs package) or a "C" column with length.

3) Use the en or em dash instead of a hyphen for not available data (or better, just left the cell empty).

MWE

\documentclass{article}
\usepackage{booktabs}
\usepackage{caption}
\captionsetup[table]{singlelinecheck=off}
\begin{document}
\begin{table}[htbp]
  \caption{Ct-value}
  \label{tab:Ct}
  \centering
  \begin{tabular}{lccccccc}
    \toprule
        & \multicolumn{7}{c}{DETECTION OF CT}\\
    \cmidrule(lr){2-8} 
    & \multicolumn{3}{c}{Matrix-PCR} &
    \multicolumn{2}{c}{H5-PCR} &
    \multicolumn{2}{c}{H7-PCR} \\
    \cmidrule(lr){2-4} \cmidrule(lr){5-6} \cmidrule(lr){7-8}
    Sample &
    G1 & G2 & G3 &
    G4 & G5 &
    G6 & G7 \\
    \cmidrule(lr){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7} \cmidrule(lr){8-8} 
    Neg     &  ---    &  ---    &  ---    &  ---    &  ---    &  ---    &  ---    \\    
    102685   &  26,25  &  26,56  &  25,93  &  ---    &  ---    &  ---    &  ---    \\    
    102420   &  ---    &  ---    &  ---    &  ---    &  ---    &  ---    &  27,72 \\     
    102696   &  23,93  &  25.93  &  23,54  &  ---    &  ---    &  23,25  &  ---    \\    
    102721   &  21,48  &  21,71  &  ---    &  24,40  &  22,78  &  ---    &  ---    \\    
    H5 Pos   &  19,02  &  ---    &  18,79  &  21,92  &  20,35  &  ---    &  ---    \\    
    H5 1:10   &  23,98  &  ---    &  22,84  &  27,38  &  23,79  &  ---    &  ---    \\    
    H5 1:100  &  27,80  &  ---    &  27,17  &  ---    &  ---    &  ---    &  ---    \\
    H7 Pos   &  ---    &  23,36  &  ---    &  ---    &  ---    &  20,39  &  21,90  \\
    H7 1:10   &  ---    &  27,87  &  ---    &  ---    &  ---    &  24,97  &  28,35  \\
    H7 1:100  &  ---    &  32,27  &  ---    &  ---    &  ---    &  28,98  &  31,68  \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
2

A solution with siunitx, caption and threeparttable. I also replced the many hyphens in cells with an endash.

Unrelated: note the vmargin package shouldn't be used, as it it has some unwanted side-effects. Replace it with geometry:

\documentclass[12pt, english]{article}
\usepackage{babel}
\usepackage{natbib}
\usepackage{url}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\graphicspath{{images/}}
\usepackage{parskip}
\usepackage{fancyhdr}
\usepackage{multirow,booktabs,colortbl,tabularx}
\usepackage{caption, makecell, siunitx, threeparttable}
\definecolor{LightGray}{gray}{.8}

\begin{document}

 \begin{table}[!htbp]
    \centering
 \begin{threeparttable}
 \captionsetup{singlelinecheck = off}
 \sisetup{table-format = 2.2, table-number-alignment = center, copy-decimal-marker}
 \setlength{\extrarowheight}{2pt}
    \caption{Ct-value}
    \label{tab:Ct}
    \begin{tabular}{p{1.75cm} *{7}{S}}%
        \toprule
        \multicolumn{4}{p{4cm}}{DETECTION OF CT}
        \\
        \cmidrule(lr){3-6}
        &
        \multicolumn{3}{c}{Matrix-PCR} &
        \multicolumn{2}{c}{H5-PCR} &
        \multicolumn{2}{c}{H7-PCR}
        \\
        \cmidrule(lr){2-4} \cmidrule(lr){5-6} \cmidrule(lr){7-8}
        \makecell{Sample} &
        {G1} & {G2} & {G3} &
        {G4} & {G5} &
        {G6} & {G7}
        \\
        \cmidrule(lr){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7} \cmidrule(lr){8-8}
        Neg & {–} & {–} & {–} & {–} & {–} & {–} & {–}\\%
        102685 & 26,25 & 26,56 & 25,93 & {–} & {–} & {–} & {–} \\%\\
        102420 & {–} & {–} & {–} & {–} & {–} & {–} & 27,72 \\
        102696 & 23,93 & 25.93 & 23,54 & {–} & {–} & 23,25 & {–} \\
        102721 & 21,48 & 21,71 & {–} & 24,40 & 22,78 & {–} & {–} \\
        H5 Pos & 19,02 & {–} & 18,79 & 21,92 & 20,35 & {–} & {–} \\
        H5 1:10 & 23,98 & {–} & 22,84 & 27,38 & 23,79 & {–} & {–} \\
        H5 1:100 & 27,80 & {–} & 27,17 & {–} & {–} & {–} & {–} \\
        H7 Pos & {–} & 23,36 & {–} & {–} & {–} & 20,39 & 21,90 \\
        H7 1:10 & {–} & 27,87 & {–} & {–} & {–} & 24,97 & 28,35 \\
        H7 1:100 & {–} & 32,27 & {–} & {–} & {–} & 28,98 & 31,68 \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}

\end{document} 

enter image description here

  • I suspect that "DETECTION OF CT" is supposed to be centered above rows 2 thru 8. – Mico Jan 24 '18 at 0:59
  • @Mico: I wondered – as the caption itself has to be left-aligned… I also couldn't understand why the first horizontal rule encompasses these columns and no other. – Bernard Jan 24 '18 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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