3

I am trying to insert my table to the left column of a two columns paper. I want my table looks like this format:

The following is my command, the table I create looks ugly! Can anyone give me a help? Thank you!

\begin{table}[h]
\begin{center} 
\parbox{0.5\textwidth}{\caption{when the title is too long it will not in the center of the table}}\vspace{10pt}
\label{tab:table}
  \begin{tabular}{r|ccccc|c}  
    &  \multicolumn{5}{c}{Gene Selection Method} &  \\ 
 Cross-Validation & A  & B & C & \\  \hline
 fold 1 & 0 & 0 & 0  \\
 fold 2 & 0 & 0 & 0  \\ \hline 
 fold 3 & 0 & 0 & 0  \\
 fold 4 & 0 & 0 & 0  \\ \hline
 fold 5 & 0 & 0 & 0  \\
 fold 6 & 0 & 0 & 0  \\ \hline
 fold 7 & 0 & 0 & 0  \\ 
 fold 8 & 0 & 0 & 0  \\ \hline
 fold 9 & 0 & 0 & 0  \\  
 fold 10 & 0 & 0 & 0 \\ \hline
 Average & 0 & 0 & 0 \\ 
  \end{tabular}
\end{center}
\end{table}

This is my ugly table:

5
  • (1) Left column of a twocolumn paper. What \documentclass are you using? (2) Inside twocolumn mode, float placements are difficult. Do you want to force it to be where you put it... in the left column? Or float around? (3) What is "ugly" about the table... that's a very subjective viewpoint. (4) Don't use the center environment, rather use \centering. See Should I use \center or \centering for figures and tables?
    – Werner
    May 20, 2015 at 16:36
  • I want to force it to be where I want in the left column. May 20, 2015 at 16:40
  • @Werner, I edited it. This is my ugly table, I want it to look like the first one. May 20, 2015 at 16:43
  • Why are you including multiple horizontal lines (hline) and vertical lines (|) in your table if you don't want them? Just remove them, and your table will have the same format as the table you give as the "perfect" table.
    – Sverre
    May 20, 2015 at 16:46
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – Sverre
    May 20, 2015 at 16:48

2 Answers 2

3

Forcing a float in-place can be obtained using float's [H] float specification (see How to influence the position of float environments like figure and table?).

In order to replicate the style of the given table, I'd suggest using a combination of array (for column definition/alignment), tabularx (for ease of specifying a stretchable column) and booktabs (for visual candy).

enter image description here

\documentclass[twocolumn]{article}
\usepackage{float,array,booktabs,tabularx,caption}
\captionsetup[table]{
  justification=centering,
  labelfont=sc,
  labelsep=newline}
\usepackage{lipsum}% Just for this example
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}

\lipsum[1]

\begin{table}[H]
  \caption{when the title is too long it will not in the center of the table}
  \begin{tabularx}{\columnwidth}{ X *{3}{C{.2\columnwidth}} }
    \toprule
    \multicolumn{1}{c}{Cross-}     & \multicolumn{3}{c}{Gene selection method} \\
    \multicolumn{1}{c}{validation} & A & B & C \\
    \midrule
    fold 1 & 0 & 0 & 0  \\
    fold 2 & 0 & 0 & 0  \\ \midrule 
    fold 3 & 0 & 0 & 0  \\
    fold 4 & 0 & 0 & 0  \\ \midrule
    fold 5 & 0 & 0 & 0  \\
    fold 6 & 0 & 0 & 0  \\ \midrule
    fold 7 & 0 & 0 & 0  \\ 
    fold 8 & 0 & 0 & 0  \\ \midrule
    fold 9 & 0 & 0 & 0  \\  
    fold 10 & 0 & 0 & 0 \\ \midrule
    Average & 0 & 0 & 0 \\
    \bottomrule
  \end{tabularx}
\end{table}

\lipsum[2]

\end{document}

caption allows one to style the caption layout very easily. I've done so explicitly for a table float above.

Depending on the contents of columns A, B and C, you may consider using siunitx for additional alignment(s).

8
  • Perhaps placing the \midrule every five rows (in this case) would increase the readability (except the last one), so basically once between fold 5 and 6.
    – Alenanno
    May 20, 2015 at 17:00
  • @Alenanno: True. It depends on what fold X really mean.
    – Werner
    May 20, 2015 at 17:04
  • @Werner, there is one problem, the table title is not all in the middle. May 20, 2015 at 17:17
  • @user3761566: The table title is wider than \columnwidth, so it's set in paragraph style. I guess you want a \centering view?
    – Werner
    May 20, 2015 at 17:17
  • @Werner, yes, like the example I post in my question May 20, 2015 at 17:21
0

Here's my solution ...

\documentclass[11pt,a4paper,twocolumn]{article}

\usepackage{caption}                   % controls how captions are typeset
\usepackage{lipsum}                    % for typesetting dummy text
\usepackage{booktabs}                  % for "fancy" tables!
\usepackage{multirow}                  % text that spans across multiple rows

\renewcommand*\thetable{\Roman{table}}
% to get the table label number in roman capitals, as per your requirement!

\captionsetup{%
        font={rm,up,normalsize},
        labelfont={bf},
        tablename=TABLE,
        % to get the table name ("Table") in Uppercase, as per your requirement!
        singlelinecheck=true,
        justification=centering,
        labelsep=newline, 
        position=bottom,
        margin=10pt     
        }%                              % global setup for caption package!

% To add forced linebreak inside a table cell
% How to use :- % Foo bar & \specialcell{Foo\\bar} & Foo bar \\ % vertically centered
% Foo bar & \specialcell[t]{Foo\\bar} & Foo bar \\              % aligned with top rule
% Foo bar & \specialcell[b]{Foo\\bar} & Foo bar \\              % aligned with bottom rule
\newcommand{\specialcell}[2][c]{%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
  % To typeset text across rows with a line-break at desired position

\begin{document}
\lipsum[1]                              % for typesetting dummy text

{                                       % Begin group --- % To keep the effect local!
\vspace*{-2.5ex}                        % reduces surrounding vertical space
\renewcommand*{\arraystretch}{1.15}     % to increase space between rows!

  \begin{table}[h]
   \centering                               % better than using the \begin{center} environment
   \belowcaptionskip=8pt                    % controls the skip amount below caption

    \caption{when the title is too long it will not in the center of the table\label{tab:table}}

      \begin{tabular}{r@{\hskip 6.5ex}c@{\hskip 10ex}c@{\hskip -4ex}c@{\hskip -12ex}}  
       \toprule
       & \multicolumn{3}{c}{{\hskip -1ex}Gene Selection Method} \\
       \cmidrule(l{-1ex}){2-4} 
       \multirow{-2}[1]*{\specialcell{Cross-\\[-2pt]Validation}} & A & B & C \\
       \midrule
       \fbox{fold 1} & 0 & 0 & 0  \\    % Use of \fbox{} to get a frame around text, as per your requirement!
        fold 2 & 0 & 0 & 0  \\  
        fold 3 & 0 & 0 & 0  \\
        fold 4 & 0 & 0 & 0  \\ 
        fold 5 & 0 & 0 & 0  \\
        fold 6 & 0 & 0 & 0  \\ 
        fold 7 & 0 & 0 & 0  \\ 
        \fbox{fold 8} & 0 & 0 & 0  \\   % Use of \fbox{} to get a frame around text, as per your requirement! 
        fold 9 & 0 & 0 & 0  \\  
        fold 10 & 0 & 0 & 0 \\ 
        \midrule
        Average & 0 & 0 & 0 \\ 
        \midrule
      \end{tabular}
  \end{table}
\vspace*{-1.75ex}                       % reduces surrounding vertical space
}                                       % End group --- % To keep the effect local! 

\lipsum[4-6]                            % for typesetting dummy text
\cleardoublepage

\end{document}

Preview of the output:

enter image description here

Hope this helps! Cheers!

You must log in to answer this question.

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