Search code examples
latexcentering

centre a table in latex with headings above cells


I have a table I wish to fit in the margins. I also wish to add both EPS and PPS above the middle of the cells with these two in. I am not sure why adding a caption is not working either. Here is the code:

  \begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}|p{3cm}|  }
   \multicolumn{5}{c}{} \\
 \hline
&EPS:Pre &EPS:Post &PPS:Pre & PPS:Post \\
\hline
 Species tested:&13 &15& 43& 43\\
 Compounds tested:& 745 & 745& 310& 361 \\
Unique tests:& 193& 193& 406& 407\\
  Total experiments:&17,811 &17,929& 107,470& 130,926\\ 
   \hline
   \end{tabular}
   \label{tab}

Thank you.


Solution

  • There where several problems.

    1. Your table was too large, with the margins and intercolumn spacing. I made a macro to fine adjust column width.

    2. To redefine a column (for instance to center a header), you can use `\multicolumn{1}{c}{header}

    3. \label assigns a number that is used in a numbered environment. It is associated with the table environmenent, not with tabular. The label appears within a caption. Also, the table is centered within the table environment.

    \documentclass{article}
    
    \begin{document}
    \newcommand{\colwidth}{0.17\textwidth}
    \newcommand{\centercolumn}[1]{\multicolumn{1}{c|}{#1}}
    \begin{table}
      \centering
      \begin{tabular}{ |p{3cm}||p{\colwidth}|p{\colwidth}|p{\colwidth}|p{\colwidth}|  }
        % \multicolumn{5}{c}{} \\ useless, I think
        \hline
        &\centercolumn{EPS:Pre} &\centercolumn{EPS:Post} &\centercolumn{PPS:Pre} & \centercolumn{PPS:Post} \\
        \hline
        Species tested:&13 &15& 43& 43\\
        Compounds tested:& 745 & 745& 310& 361 \\
        Unique tests:& 193& 193& 406& 407\\
        Total experiments:&17,811 &17,929& 107,470& 130,926\\ 
        \hline
      \end{tabular}
      \caption{My table}
      \label{tab:1}
    \end{table}
    \end{document}
    

    enter image description here