表格式:移位 \cmidrule 和列額外空間

表格式:移位 \cmidrule 和列額外空間

我有以下使用 IEEE 會議範本的腳本:https://www.ctan.org/tex-archive/macros/latex/contrib/IEEEtran/?lang=en

我發現格式化這個表很棘手。問題是:

1)最左邊一列不正確。我需要黑色水平線從行的開頭開始(請參閱每行左側的一些空格),並且在開始行文字之前需要一個空格(需要它看起來與最右邊的列類似)。

2) 列A必須像列B和 一樣加底線C。但是,這條線的位置不太好。它應該在垂直線之前結束。

這是腳本:

\documentclass[compsoc, conference, letterpaper, 10pt, times]{IEEEtran}
\ifCLASSOPTIONcompsoc
  % IEEE Computer Society needs nocompress option
  % requires cite.sty v4.0 or later (November 2003)
  \usepackage[nocompress]{cite}
\else
  % normal IEEE
  \usepackage{cite}
\fi
\ifCLASSINFOpdf
\else
\fi
\usepackage{graphicx}

\usepackage{colortbl}

\usepackage{array}
\usepackage{booktabs}
\usepackage{multirow}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}}
\newcommand{\normal}[1]{\multicolumn{1}{l}{#1}}
\usepackage{amssymb}% http://ctan.org/pkg/amssymb
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%
\newcommand*\rot{\rotatebox{90}}
\usepackage{tabularx}

\begin{document}
\title{Test Document}

% make the title area
\maketitle

\section{Introduction}
This is an introduction.

\begin{table*}[tb!] 
\caption{A Table}

\centering
\resizebox{\textwidth}{!} {
\begin{tabular} {@{} lc*{16}l @{}}
\hline
\multicolumn{1}{c}{A} 
&&\multicolumn{3}{c}{B}    
&&\multicolumn{3}{c}{C}  
&&\multicolumn{3}{c}{D}  
&&\multicolumn{2}{c}{E}  

\\
\cmidrule(lr){0-1} 
\cmidrule(lr){3-5} 
\cmidrule(lr){7-9}
\cmidrule(lr){11-13}
\cmidrule(lr){15-16}
\\
& \vline &\rot{Col. 1} & \rot{Col. 2} & \rot{Col. 3} 
& \vline & \rot{Col. 1} & \rot{Col. 2} & \rot{Col. 3} 
& \vline & \rot{Col. 1} & \rot{Col. 2} &\rot{Col. 3} 
& \vline & \rot{Col. 1} & \rot{Col. 2} 

\\ \hline
\rowcolor[gray]{.9} 
Text1 &\vline  & \cmark & {}     & {}     & \vline  & {}     & \cmark  & {}      & \vline  & {} &\cmark  & & \vline &    & \cmark \\\hline
Text2 &\vline  & {}     & \cmark & {}     & \vline  & {}     & \cmark   & {}      & \vline & {} & \cmark & & \vline & {} & \cmark   \\\hline
\rowcolor[gray]{.9}
Text3 & \vline & \cmark & {}     & {}     & \vline  & {}     & \cmark   & {}      & \vline & {} &\cmark  & &\vline & {} & \cmark    \\
\hline
Text4 & \vline & \cmark & {}    & {}     & \vline   & {}     & \cmark   & {}      & \vline & {} &\cmark  & &\vline & {} & \cmark   \\
 \hline
\rowcolor[gray]{.9} 
Text5 & \vline & {}     & \cmark & {}    & \vline   & \cmark  & {}      & {}      & \vline & \cmark & {} & &\vline & {} & \cmark    \\
\hline
Text6 & \vline & \cmark & {}    & {}     & \vline   & \cmark  & {}      & {}      & \vline & {}     & \cmark & & \vline & {} & \cmark   \\
\hline
\rowcolor[gray]{.9}
Text7 & \vline & \cmark & {}    & {}     & \vline   & \cmark  & {}      & {}       & \vline & \cmark & {}    & & \vline & \cmark & {}  \\
\hline
Text8 & \vline & \cmark & {}   & {}     & \vline    & {}     & \cmark   & {}      & \vline  & {}     &\cmark & & \vline & {} & \cmark  \\
\hline
\rowcolor[gray]{.9}
Text9 & \vline & {}     & {}     & \cmark & \vline & \cmark  & \cmark  & {}       & \vline  & {}     &\cmark & & \vline & {} & \cmark \\\hline
Text10 & \vline & {}     & {}     & \cmark & \vline & {}      & {}      & \cmark   & \vline  & {}     &\cmark & &\vline & {} & \cmark   \\
\hline
\end{tabular}
}
\end{table*}
\end{document}

在此輸入影像描述

答案1

您的問題是由在列定義@{}中使用引起的。tabular解決方法在colortbl套件的文檔中進行了解釋,因此它與使用的文檔類別無關。

編輯: 如果您考慮使用tabularx而不是tabular,您的問題的解決方案是:

  • 將列數減少為包含內容的列數 (12)
  • 將列定義變更為(不含垂直線)

\begin{tabularx}{\linewidth}{@{}>{\columncolor{white}[0pt][\tabcolsep]}l
                                                                 *{10}{C}
                                >{\columncolor{white}[\tabcolsep][0pt]}C
                            @{}}

或(有垂直線)

\begin{tabularx}{\linewidth}{@{}>{\columncolor{white}[0pt][\tabcolsep]}l  |
                                                                  *{3}{CCC|}
                                                                       C 
                                >{\columncolor{white}[\tabcolsep][0pt]}C
                            @{}}

無關: 我擅自更改了您的表格,以便它可以適合列寬。為此,我使用tabularx表格環境,將字體大小減小到\small,刪除垂直線,對於水平線,使用以下規則booktabs(以獲得更專業的表格外觀):

在此輸入影像描述

正如您所看到的,這張表沒有您的問題。也許你喜歡它:)。 MWE 為:

\documentclass[twocolumn]{article}
\usepackage{amssymb}% http://ctan.org/pkg/amssymb
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%

\usepackage[table]{xcolor}% instead of colortbl
\usepackage{booktabs, makecell, multirow, tabularx}
\usepackage{rotating}% added
\newcolumntype{C}{>{\centering\arraybackslash}X}

%-------------------------------------- only for show page layout
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}
\begin{document}
\lipsum[1]
    \begin{table}[ht]
\caption{A Table}
    \centering
\setlength\tabcolsep{3pt}
    \settowidth\rotheadsize{Col. 3}
\begin{tabularx}{\linewidth}{@{}>{\columncolor{white}[0pt][\tabcolsep]}l
                                                                 *{10}{C} 
                                >{\columncolor{white}[\tabcolsep][0pt]}C
                            @{}}
    \toprule
\multicolumn{1}{c}{A}       
        &   \multicolumn{3}{c}{B}   
            &   \multicolumn{3}{c}{C}
                &   \multicolumn{3}{c}{D}
                    &   \multicolumn{2}{c}{E}           \\
\cmidrule(lr){1-1}
\cmidrule(lr){2-4}\cmidrule(lr){5-7}\cmidrule(lr){8-10}
\cmidrule(lr){11-12}
        & \rothead{Col. 1} & \rothead{Col. 2} & \rothead{Col. 3}
        & \rothead{Col. 1} & \rothead{Col. 2} & \rothead{Col. 3}
        & \rothead{Col. 1} & \rothead{Col. 2} & \rothead{Col. 3}
        & \rothead{Col. 1} & \rothead{Col. 2}                   \\ 
    \hline
\rowcolor[gray]{.9}
Text1   & \cmark        &               & 
        &               & \cmark        & 
        &               & \cmark        &
        &               & \cmark                        \\
Text2   &               & \cmark        & 
        &               & \cmark        & 
        & \cmark        &               & \cmark        \\
\rowcolor[gray]{.9}
Text3   & \cmark        &               &
        &               & \cmark        &  
        &               & \cmark        &
        &               & \cmark                        \\
Text4   & \cmark        &               & 
        &               & \cmark        & 
        &               & \cmark        & 
        &               & \cmark                        \\
\rowcolor[gray]{.9}
Text5   &               & \cmark        &
        & \cmark        &               & 
        & \cmark        &               &
        &               & \cmark                        \\
\hline
Text6   & \cmark        &               & 
        & \cmark        &               &
        &               & \cmark        & 
        &               & \cmark                        \\
\rowcolor[gray]{.9}
Text7   & \cmark        &               & 
        & \cmark        &               & 
        & \cmark        &               & 
        & \cmark        &                               \\
Text8   & \cmark        &               & 
        &               & \cmark        & 
        &               & \cmark        & 
        &               & \cmark                        \\
\rowcolor[gray]{.9}
Text9   &               &               & \cmark 
        & \cmark        & \cmark        &
        &               & \cmark        & 
        &               & \cmark                        \\
Text10  &               &               & \cmark 
        &               &               & \cmark   
        &               & \cmark        & 
        &               & \cmark                        \\
    \bottomrule
\end{tabularx}
\end{table}
\lipsum
\end{document}

上面的 MWe 也應該適用於IEEEtran文檔類別(現在我還沒有安裝)。

如果您堅持使用兩列並帶有垂直線的表格,那麼請考慮替換上面的 MWE,而不是現有以下行:

...
    \begin{table*}
\caption{A Table}
    \centering
\setlength\tabcolsep{3pt}
    \settowidth\rotheadsize{Col. 3}
    \begin{tabularx}{\linewidth}{@{}>{\columncolor{white}[0pt][\tabcolsep]}l  |
                                                                      *{3}{CCC|}
                                                                           C
                                    >{\columncolor{white}[\tabcolsep][0pt]}C
                                @{}}
    \toprule
\multicolumn{1}{c}{A}       
        &   \multicolumn{3}{c}{B}   
            &   \multicolumn{3}{c}{C}
                &   \multicolumn{3}{c}{D}
                    &   \multicolumn{2}{c}{E}           \\
\cmidrule(lr){1-1}
\cmidrule(lr){2-4}\cmidrule(lr){5-7}\cmidrule(lr){8-10}
\cmidrule(lr){11-12}
        & {Col. 1} & {Col. 2} & {Col. 3}
        & {Col. 1} & {Col. 2} & {Col. 3}
        & {Col. 1} & {Col. 2} & {Col. 3}
        & {Col. 1} & {Col. 2}                   \\ 
    \hline
    ...

您將獲得:

在此輸入影像描述

Edid (2):並使用問題中程式碼中的所有垂直線和垂直列標題:

在此輸入影像描述

最後一張圖像的完整 MWE 為:

\documentclass[twocolumn]{article}
\usepackage{amssymb}% http://ctan.org/pkg/amssymb
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%

\usepackage[table]{xcolor}% instead of colortbl
\usepackage{booktabs, makecell, multirow, tabularx}
\usepackage{rotating}% added
\newcolumntype{C}{>{\centering\arraybackslash}X}
%-------------------------------------- only for show page layout
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}
\begin{document}
\lipsum[1]
    \begin{table*}
\caption{A Table}
    \centering
\setlength\tabcolsep{3pt}
    \settowidth\rotheadsize{Col. 3}
    \begin{tabularx}{\linewidth}{@{}>{\columncolor{white}[0pt][\tabcolsep]}l|
                                                                      *{3}{CCC|}
                                                                           C
                                    >{\columncolor{white}[\tabcolsep][0pt]}C
                                @{}}
    \toprule
\multicolumn{1}{c}{A}       
        &   \multicolumn{3}{c}{B}   
            &   \multicolumn{3}{c}{C}
                &   \multicolumn{3}{c}{D}
                    &   \multicolumn{2}{c}{E}           \\
\cmidrule(lr){1-1}
\cmidrule(lr){2-4}\cmidrule(lr){5-7}\cmidrule(lr){8-10}
\cmidrule(lr){11-12}
        & \rothead{Col. 1} & \rothead{Col. 2} & \rothead{Col. 3}
        & \rothead{Col. 1} & \rothead{Col. 2} & \rothead{Col. 3}
        & \rothead{Col. 1} & \rothead{Col. 2} & \rothead{Col. 3}
        & \rothead{Col. 1} & \rothead{Col. 2}                   \\ 
    \hline
\rowcolor[gray]{.9}
Text1   & \cmark        &               & 
        &               & \cmark        & 
        &               & \cmark        &
        &               & \cmark                        \\
Text2   &               & \cmark        & 
        &               & \cmark        & 
        & \cmark        &               & \cmark        \\
\rowcolor[gray]{.9}
Text3   & \cmark        &               &
        &               & \cmark        &  
        &               & \cmark        &
        &               & \cmark                        \\
Text4   & \cmark        &               & 
        &               & \cmark        & 
        &               & \cmark        & 
        &               & \cmark                        \\
\rowcolor[gray]{.9}
Text5   &               & \cmark        &
        & \cmark        &               & 
        & \cmark        &               &
        &               & \cmark                        \\
Text6   & \cmark        &               & 
        & \cmark        &               &
        &               & \cmark        & 
        &               & \cmark                        \\
\rowcolor[gray]{.9}
Text7   & \cmark        &               & 
        & \cmark        &               & 
        & \cmark        &               & 
        & \cmark        &                               \\
Text8   & \cmark        &               & 
        &               & \cmark        & 
        &               & \cmark        & 
        &               & \cmark                        \\
\rowcolor[gray]{.9}
Text9   &               &               & \cmark 
        & \cmark        & \cmark        &
        &               & \cmark        & 
        &               & \cmark                        \\
Text10  &               &               & \cmark 
        &               &               & \cmark   
        &               & \cmark        & 
        &               & \cmark                        \\
    \bottomrule
\end{tabularx}
\end{table*}
\lipsum
\end{document}

編輯(3): 為了樂趣和完整性無關建議另一種解決方案,沒有(黑色)垂直線,沒有旋轉列標題,並且所有列具有相同的寬度:

在此輸入影像描述

\documentclass[twocolumn]{article}
\usepackage{amssymb}
\usepackage{pifont}
\newcommand{\cmark}{\ding{51}}%

\usepackage[table]{xcolor}% instead of colortbl
\usepackage{booktabs, tabularx}
\usepackage[skip=1ex]{caption}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\usepackage{lipsum}
\begin{document}
\lipsum[1]
    \begin{table*}
\caption{A Table}
    \centering
\setlength\tabcolsep{3pt}
    \begin{tabularx}{\linewidth}{@{}>{\columncolor{white}[0pt][\tabcolsep]}X   @{\quad}
                                                                      *{3}{CCC @{\quad}}
                                                                           C
                                    >{\columncolor{white}[\tabcolsep][0pt]}C
                                @{}}
    \toprule
\multicolumn{1}{c}{A}
        &   \multicolumn{3}{c}{B}
            &   \multicolumn{3}{c}{C}
                &   \multicolumn{3}{c}{D}
                    &   \multicolumn{2}{c}{E}           \\
\cmidrule(r){1-1}
\cmidrule(r){2-4}\cmidrule(r){5-7}\cmidrule(r){8-10}
\cmidrule{11-12}
        & {Col. 1} & {Col. 2} & {Col. 3}
        & {Col. 1} & {Col. 2} & {Col. 3}
        & {Col. 1} & {Col. 2} & {Col. 3}
        & {Col. 1} & {Col. 2}                   \\
    \hline
\rowcolor[gray]{.9}
Text1   & \cmark        &               &
        &               & \cmark        &
        &               & \cmark        &
        &               & \cmark                        \\
Text2   &               & \cmark        &
        &               & \cmark        &
        & \cmark        &               & \cmark        \\
\rowcolor[gray]{.9}
Text3   & \cmark        &               &
        &               & \cmark        &
        &               & \cmark        &
        &               & \cmark                        \\
Text4   & \cmark        &               &
        &               & \cmark        &
        &               & \cmark        &
        &               & \cmark                        \\
\rowcolor[gray]{.9}
Text5   &               & \cmark        &
        & \cmark        &               &
        & \cmark        &               &
        &               & \cmark                        \\
Text6   & \cmark        &               &
        & \cmark        &               &
        &               & \cmark        &
        &               & \cmark                        \\
\rowcolor[gray]{.9}
Text7   & \cmark        &               &
        & \cmark        &               &
        & \cmark        &               &
        & \cmark        &                               \\
Text8   & \cmark        &               &
        &               & \cmark        &
        &               & \cmark        &
        &               & \cmark                        \\
\rowcolor[gray]{.9}
Text9   &               &               & \cmark
        & \cmark        & \cmark        &
        &               & \cmark        &
        &               & \cmark                        \\
Text10  &               &               & \cmark
        &               &               & \cmark
        &               & \cmark        &
        &               & \cmark                        \\
    \bottomrule
\end{tabularx}
\end{table*}
\lipsum
\end{document}

所有上述偏離主題的建議都考慮了您對行著色的願望。我個人不會對行進行著色。為了幫助讀者追蹤行,我最終會在每四行之後添加一些垂直空間,以嘗試將表格放入一列中。然而,不同的人,不同的口味,...

答案2

我建議使用tabularx and no \resizebox。垂直線作為列之間的分隔符號放置,因此我只有 12 列。另外,我冒昧地改變了旋轉柱頭的外觀:旋轉 40°,因此它們的高度更小:

\documentclass[compsoc, conference, letterpaper, 10pt, times]{IEEEtran}
\ifCLASSOPTIONcompsoc
  % IEEE Computer Society needs nocompress option
  % requires cite.sty v4.0 or later (November 2003)
  \usepackage[nocompress]{cite}
\else
  % normal IEEE
  \usepackage{cite}
\fi
\ifCLASSINFOpdf
\else
\fi

\usepackage{graphicx}

\usepackage[table, svgnames]{xcolor}

\usepackage{array}
\usepackage{booktabs}
\usepackage{multirow}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}}
\newcommand{\normal}[1]{\multicolumn{1}{l}{#1}}
\usepackage{amssymb}% http://ctan.org/pkg/amssymb
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%
\newcommand*\rot[1][90]{\rotatebox{90}}
\newcommand*\myrot[1]{\rotatebox[origin=l]{40}{#1}}
\usepackage{tabularx}
\newcolumntype{C}{ >{\centering\arraybackslash}X}

\begin{document}
\title{Test Document}

% make the title area
\maketitle

\section{Introduction}
This is an introduction.


\begin{table*}[!htb]
  \setlength\extrarowheight{2pt}\centering
  \caption{A Table}
  \begin{tabularx}{0.8\textwidth} { l|*{3}{CCC|}*{2}{C}|@{}}
    \hline
    \multicolumn{1}{c}{A}
           & \multicolumn{3}{c}{B}
           & \multicolumn{3}{c}{C}
           & \multicolumn{3}{c}{D}
           & \multicolumn{2}{c}{E}
    \\
    \cmidrule(lr){1-1}
    \cmidrule(lr){2-4}
    \cmidrule(lr){5-7}
    \cmidrule(lr){8-10}
    \cmidrule(lr){11-12}
           & \myrot{Col. 1} & \myrot{Col. 2} & \myrot{Col. 3}
           & \myrot{Col. 1} & \myrot{Col. 2} & \myrot{Col. 3}
           & \myrot{Col. 1} & \myrot{Col. 2} & \myrot{Col. 3}
           & \myrot{Col. 1} & \myrot{Col. 2}
    \\ \hline
    \rowcolor[gray]{.9}Text1
           & \cmark & & & & \cmark & & & \cmark & & & \cmark \\\hline
    Text2 & & \cmark & & & \cmark & & & \cmark & & & \cmark \\\hline
    \rowcolor[gray]{.9}
    Text3 & \cmark & & & & \cmark & & & \cmark & & & \cmark \\
    \hline
    Text4 & \cmark & & & & \cmark & & & \cmark & & & \cmark \\
    \hline
    \rowcolor[gray]{.9}
    Text5 & {} & \cmark & & \cmark & & & \cmark & & & & \cmark \\
    \hline
    Text6 & \cmark & & & \cmark & & & & \cmark & & & \cmark \\
    \hline
    \rowcolor[gray]{.9}
    Text7 & \cmark & & & \cmark & & & \cmark & & & \cmark & \\
    \hline
    Text8 & \cmark & & & & \cmark & & & \cmark & & & \cmark \\
    \hline
    \rowcolor[gray]{.9}
    Text9 & & & \cmark & \cmark & \cmark & & & \cmark & & & \cmark \\
    \hline
    Text10 & & \cmark & & & & \cmark & & \cmark & & & \cmark \\
    \hline
  \end{tabularx}
\end{table*}

\end{document}

在此輸入影像描述

如果您想要旋轉 90°,請將表體替換\myrot\rot

在此輸入影像描述

相關內容