如何修改table命令使其完美

如何修改table命令使其完美

我正在尝试将表格插入到两列纸张的左列。我希望我的表格看起来像这种格式:

以下是我的命令,我创建的表看起来很丑!有人能帮我吗?谢谢!

\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}

这是我的丑陋的桌子:

答案1

可以使用以下方法强制浮动float[H]浮点规范(见figure如何影响浮动环境(如和)的位置table)。

为了复制给定表格的样式,我建议使用以下组合array(用于列定义/对齐),tabularx(为了便于指定可伸缩列)和booktabs(为了视觉享受)。

在此处输入图片描述

\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可以非常轻松地设置标题布局的样式。我已明确地对table上面的浮动进行了设置。

根据 A、B 和 C 列的内容,您可以考虑使用siunitx进行额外的对齐。

答案2

这是我的解决方案...

\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}

输出预览:

在此处输入图片描述

希望这能有所帮助!干杯!

相关内容