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 want the text in each cell vertically centered.

\documentclass[12pt,a4paper,onesided]{report}
\usepackage{array}
\usepackage{booktabs}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}

\begin{table}[htbp]
\centering
\caption{Recognition with Haar Wavelet based image compression for $N=5$ features}
\label{tab: Recognition with Haar Wavelet based image compression for $N=5$ features}
\begin{tabular}{|C{1.8cm}||C{1.8cm}||C{2cm}||C{1.8cm}|C{1.8cm}||C{1.8cm}||C{2.5cm}|}

\toprule \bfseries Training folder 
&\bfseries Training Time 
&\bfseries Training Accuracy 
& \multicolumn{2}{c|}{\bfseries Number of images} 
&\bfseries Testing folder 
& \bfseries Recognition accuracy\\
 & & & \bfseries Training &\bfseries Testing & &\\ \midrule
    1     & 0.52  & 70.00 & 10    & 90    & 9     & 14.44 \\
    2     & 0.66  & 85.00 & 20    & 80    & 8     & 38.75 \\
    3     & 0.74  & 86.66 & 30    & 70    & 7     & 40.00 \\
    4     & 0.68  & 85.00 & 40    & 60    & 6     & 48.33 \\
    5     & 0.88  & 88.00 & 50    & 50    & 5     & 62.00 \\
\bottomrule

\end{tabular}
\end{table}

\end{document}
share|improve this question
2  
Could you be a bit more specific? Just providing a bunch of code is not really informative. –  Argo Mar 22 at 11:18
    
Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. –  karlkoeller Mar 22 at 11:21
    
I have edited the content, thanks :) –  Saket Mar 22 at 11:46
    
@Saket You didn't say what you want centered –  egreg Mar 22 at 11:59
    
I want the text in each cell vertically centered. –  Saket Mar 22 at 12:03

2 Answers 2

A solution without defining newcolumn types nor computing the column widths. Simply use themakecell, caption and multirowpackages, plus hhlineto improve the appearance of double lines. The table can fit into your margins in two way: reduce tabcolsep to 4pt and either use a smaller font size for the document (I give an example in 11pt) or reduce the font size locallyto 10 pt (\small command in 12pt will do).

Btw, don't use booktabs with vertical lines.

    \documentclass[11pt,a4paper,onesided]{report}
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage[showframe, nomarginpar, left=1.5in, right=1in, top=1.5in, bottom=1in,]{geometry}
    \usepackage{array}
    \usepackage[font = small]{caption}
    \usepackage{booktabs}
    \usepackage{multirow}
    \usepackage{makecell}
    \renewcommand{\theadfont}{\bfseries}
    \renewcommand{\theadalign}{cc}
    \setlength{\tabcolsep}{4pt}
    \usepackage{hhline}

    \begin{document}
    Table fontsize:  11 pt
    \begin{table}[htbp]
    \centering
    \caption{Recognition with Haar Wavelet based image compression for $N=5$ features}
    \label{tab: Recognition with Haar Wavelet based image compression for $N=5$ features}
    \begin{tabular}{*{3}{|c|}|cc|*{2}{|c|}}%{|C{1.8cm}||C{1.8cm}||C{2cm}||C{1.8cm}|C{1.8cm}||C{1.8cm}||C{2.5cm}|}
     \hhline{|-||-||-||--||-||-|}
    \multirowthead{2}{Training\\ Folder}
    &\multirowthead{2}{Training\\ Time}
    &\multirowthead{2}{Training\\ Accuracy}
    & \multicolumn{2}{c||}{\thead{Number of images}}
    &\multirowthead{2}{Testing\\ Folder}
    & \multirowthead{2}{Recognition\\ Accuracy}\\
     & & &  \thead*{Training} & \thead{Testing} & &\\
     \hhline{|-||-||-||--||-||-|}
        \Gape[6pt][0pt]{1}     & 0.52  & 70.00 & 10    & 90    & 9     & 14.44 \\
        2     & 0.66  & 85.00 & 20    & 80    & 8     & 38.75 \\
        3     & 0.74  & 86.66 & 30    & 70    & 7     & 40.00 \\
        4     & 0.68  & 85.00 & 40    & 60    & 6     & 48.33 \\
        \Gape[0pt][6pt]{5}     & 0.88  & 88.00 & 50    & 50    & 5     & 62.00 \\
     \hhline{|-||-||-||--||-||-|}

    \end{tabular}
    \end{table}

    \fontsize{12}{14}\selectfont
    Table font size:  10 point
    \begin{table}[htbp]
    \small
    \centering
    \caption{Recognition with Haar Wavelet based image compression for $N=5$ features}
    %\small
    \label{tab: Recognition with Haar Wavelet based image compression for $N=5$ features}
    \begin{tabular}{*{3}{|c|}|cc|*{2}{|c|}}%{|C{1.8cm}||C{1.8cm}||C{2cm}||C{1.8cm}|C{1.8cm}||C{1.8cm}||C{2.5cm}|}
     \hhline{|-||-||-||--||-||-|}
    \multirowthead{2}{Training\\ Folder}
    &\multirowthead{2}{Training\\ Time}
    &\multirowthead{2}{Training\\ Accuracy}
    & \multicolumn{2}{c||}{\thead{Number of images}}
    &\multirowthead{2}{Testing\\ Folder}
    & \multirowthead{2}{Recognition\\ Accuracy}\\
     & & &  \thead*{Training} & \thead{Testing} & &\\
     \hhline{|-||-||-||--||-||-|}
        \Gape[6pt][0pt]{1}     & 0.52  & 70.00 & 10    & 90    & 9     & 14.44 \\
        2     & 0.66  & 85.00 & 20    & 80    & 8     & 38.75 \\
        3     & 0.74  & 86.66 & 30    & 70    & 7     & 40.00 \\
        4     & 0.68  & 85.00 & 40    & 60    & 6     & 48.33 \\
        \Gape[0pt][6pt]{5}     & 0.88  & 88.00 & 50    & 50    & 5     & 62.00 \\
     \hhline{|-||-||-||--||-||-|}

    \end{tabular}
    \end{table}
    \end{document} 

enter image description here

share|improve this answer

Is this what you were asking for? Here the solution defines a \mc macro taking the text as an argument for the multirow structure that will wrap the text into two lines, left aligned.

\newcommand\mc[1]{\multicolumn{1}{l||}{\multirow{2}{0.9cm}{#1}}}

enter image description here

Code

\documentclass[12pt,a4paper]{report}
\usepackage{array,graphicx}
\usepackage[margin=1cm]{geometry}
\usepackage{booktabs,dcolumn,multirow} 
\newcolumntype{C}[1]{>{\centering \arraybackslash\hspace{0pt}}m{#1}} 
\newcommand\mc[1]{\multicolumn{1}{l||}{\multirow{2}{0.9cm}{#1}}}
\begin{document}

Before:

\begin{table}[htbp]
\centering
\caption{Recognition with Haar Wavelet based image compression for $N=5$ features}
\label{tab: Recognition with Haar Wavelet based image compression for $N=5$ features}
\begin{tabular}{|C{1.8cm}||C{1.8cm}||C{2cm}||C{1.8cm}|C{1.8cm}||C{1.8cm}||C{2.5cm}|}

\toprule \bfseries Training folder 
&\bfseries Training Time 
&\bfseries Training Accuracy 
& \multicolumn{2}{c|}{\bfseries Number of images} 
&\bfseries Testing folder 
& \bfseries Recognition accuracy\\
& & & \bfseries Training &\bfseries Testing & &\\ \midrule
    1     & 0.52  & 70.00 & 10    & 90    & 9     & 14.44 \\
    2     & 0.66  & 85.00 & 20    & 80    & 8     & 38.75 \\
    3     & 0.74  & 86.66 & 30    & 70    & 7     & 40.00 \\
    4     & 0.68  & 85.00 & 40    & 60    & 6     & 48.33 \\
    5     & 0.88  & 88.00 & 50    & 50    & 5     & 62.00 \\
\bottomrule

\end{tabular}
\end{table}

After:
\begin{table}[htbp] 
\centering
\resizebox{\textwidth}{!}{    % if resizebox is needed
\begin{minipage}{\textwidth}  % if resizebox is needed
\caption{Recognition with Haar Wavelet based image compression for $N=5$ features} 
\label{tab: Recognition with Haar Wavelet based image compression for $N=5$ features} 
\begin{tabular}{|C{1.8cm}||C{1.8cm}||C{2cm}||C{1.8cm}|C{1.8cm}||C{1.8cm}||C{2.5cm}|}
\toprule 
\multicolumn{1}{|l||}{\multirow{2}{0.9cm}{\bfseries Training  folder}}
&\mc{\bfseries Training Time} 
&\mc{\bfseries Training Accuracy} 
&\multicolumn{2}{c||}{\bfseries Number of images} 
&\mc{\bfseries Testing folder} 
&\multicolumn{1}{l|}{\multirow{2}{0.9cm}{\bfseries Recognition accuracy}} \\
& & & \bfseries Training &\bfseries Testing & &\\ 
\midrule 
1 & 0.52 & 70.00 & 10 & 90 & 9 & 14.44 \\
2 & 0.66 & 85.00 & 20 & 80 & 8 & 38.75 \\
3 & 0.74 & 86.66 & 30 & 70 & 7 & 40.00 \\
4 & 0.68 & 85.00 & 40 & 60 & 6 & 48.33 \\
5 & 0.88 & 88.00 & 50 & 50 & 5 & 62.00 \\
\bottomrule
\end{tabular}
\end{minipage}         % if resizebox is needed
}                      % if resizebox is needed
\end{table}

\end{document}
share|improve this answer
    
Many many thanks :) Jesse :) :) –  Saket Mar 22 at 13:58
    
Jesse, i want one more favour... Can we make the same table with less width? It is looking odd in my report. i have these margins: \usepackage[left=1.5in, right=1in, top=1.5in, bottom=1in, includefoot, headheight=10pt]{geometry} –  Saket Mar 22 at 14:09
    
There is a way using resizebox from graphicx package, like \resizebox{\textwidth}{!}{ \begin{minipage}{\textwidth} tabular content including caption and label \end{minipage}}. –  Jesse Mar 22 at 14:32
    
Please see my update for clarity. My test shows 0.8\textwidth is fair. –  Jesse Mar 22 at 14:39

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.