我使用来自以下 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
和\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
: