具有文本换行和表格行颜色的多列单元格

具有文本换行和表格行颜色的多列单元格

看似简单的事情却变得如此繁琐。我想要做的就是有一个 3 行 10 列的基本表格。第一行应突出显示为浅灰色,第三行应突出显示为浅灰色(定义为 lightlightgray)。同样在第 1 行中,第 1 列和第 2 列应该合并,第 3 列和第 4 列合并,第 5 列和第 6 列合并,第 7 列和第 8 列合并,第 9 列和第 10 列合并。最后,每个合并单元格中的文本都应居中并在必要时换行,因为表格太宽,无法在页面上容纳。我在 excel 中添加了一个等效表格的屏幕截图,然后是在 latex 中添加了它。问题是所有单元格都不会填充颜色,列不会保持相同的大小,并且表格线会随机消失。

在此处输入图片描述

在此处输入图片描述

我的代码如下

\documentclass[pdftex,12pt,a4paper]{report}

% SET UP THE DOCUMENT
\usepackage[lmargin=3cm,rmargin=2cm,tmargin=2cm,bmargin=2cm]{geometry}
\usepackage{setspace}
\usepackage{framed}
\usepackage[usenames,table,dvipsnames]{xcolor}
\definecolor{lightlightgray}{gray}{0.95}
\usepackage{fancybox}
\usepackage{caption}
\usepackage[hidelinks]{hyper ref} %Allows for clickable references
\setcounter{secnumdepth}{5} %Allow for levels of section eg:5.2.1.1
\usepackage[none]{hyphenat}

%USE THESE TO CONTROL TABLES
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}
\usepackage[english]{babel}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage {rotating}

%THESE CONTROL THE IMAGES/PICTURES PACKAGE
\usepackage{float} %Allows for control of float 

%THIS LOADS THE BIBLIOGRAPHY PACKAGE
\usepackage{apacite} %change this to IEEE if you want

\begin{document}

\begin{table}[H]
    \centering
    \caption[2018 SECaT Scores]{Please HELLPPP}.
    \label{tab:secat}
    \begin{tabular}{|p{1.1cm}|p{1.1cm}|p{1.2cm}|p{1.2cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|p{1.1cm}|} \hline
    \rowcolor{lightgray}
    \multicolumn{2}{C{1.1cm}}{\textbf{Strongly Agree}} & \multicolumn{2}{|c|}{\textbf{Agree}} & \multicolumn{2}{C{1.4cm}}{\textbf{Neither Agree/Disagree}} & \multicolumn{2}{|c|}{\textbf{Disagree}} & \multicolumn{2}{C{1.1cm}}{\cellcolor{lightgray}\textbf{Strongly Disagree}} \\ \hline
    \% & No. & \% & No. & \% & No. & \% & No. & \% & No.\\ \hline
    \rowcolor{lightlightgray}
        17\% & 25 & 38\% & 57 & 27\% & 40 & 10\% & 15 & 9\% & 13 \\ \hline
    \end{tabular}
\end{table}

\end{document}

答案1

stackengine以下是用于列头的解决方案tabularx

\documentclass[pdftex, 12pt, a4paper]{report}

% SET UP THE DOCUMENT
\usepackage[lmargin=3cm,rmargin=2cm,tmargin=2cm,bmargin=2cm, showframe]{geometry}
\usepackage{setspace}
\usepackage{framed}
\usepackage[usenames,table,dvipsnames]{xcolor}
\definecolor{lightlightgray}{gray}{0.95}
\usepackage{fancybox}
\usepackage{caption}
\usepackage[hidelinks]{hyper ref} %Allows for clickable references
\setcounter{secnumdepth}{5} %Allow for levels of section eg:5.2.1.1
\usepackage[none]{hyphenat}

%USE THESE TO CONTROL TABLES
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}
\usepackage[english]{babel}
\usepackage{array}
\usepackage {rotating}
\usepackage{tabularx}
%THESE CONTROL THE IMAGES/PICTURES PACKAGE
\usepackage{float} %Allows for control of float
\usepackage[usestackEOL]{stackengine}
%THIS LOADS THE BIBLIOGRAPHY PACKAGE
\usepackage{apacite} %change this to IEEE if you want

\begin{document}

\begin{table}[H]
    \centering\setstackgap{L}{2.5ex}
    \setlength{\extrarowheight}{3pt}
    \setlength{\tabcolsep}{2pt}
    \caption[2018 SECaT Scores]{Please HELLPPP}
    \label{tab:secat}
    \begin{tabularx}{\linewidth}{|*{10}{>{\centering\arraybackslash}X|}}
     \hline
  \rowcolor{lightgray}[\tabcolsep]
 \multicolumn{2}{|>{\bfseries}c|}{\Centerstack{Strongly\\ Agree}} & \multicolumn{2}{c|}{\textbf{Agree}} & \multicolumn{2}{>{\small\bfseries}c|}{\Centerstack{Neither\\ Agree/Disagree}} & \multicolumn{2}{c|}{\textbf{Disagree}} & \multicolumn{2}{>{\bfseries}c|}{\Centerstack{Strongly\\ Disagree}} \\ \hline
    \% & No. & \% & No. & \% & No. & \% & No. & \% & No.\\ \hline
    \rowcolor{lightlightgray}
        17\% & 25 & 38\% & 57 & 27\% & 40 & 10\% & 15 & 9\% & 13 \\ \hline
    \end{tabularx}
\end{table}

\end{document} 

在此处输入图片描述

答案2

替代:

\documentclass[12pt,a4paper]{report}
% SET UP THE DOCUMENT
\usepackage[hmargin={3cm,2cm},vmargin=2cm]{geometry}
\usepackage[usenames,dvipsnames,table]{xcolor}
\definecolor{lightlightgray}{gray}{0.95}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newlength{\twocolwidth}
\setlength\twocolwidth{\dimexpr0.2\hsize+\tabcolsep+\arrayrulewidth}

\begin{document}
    \begin{table}[ht]
    \centering
    \setlength\tabcolsep{.5\tabcolsep}
    \setlength\extrarowheight{1pt}
    \renewcommand\tabularxcolumn[1]{m{#1}}
\caption[2018 SECaT Scores]{Please HELLPPP}.
\label{tab:secat}
\begin{tabularx}{\linewidth}{ |*{10}{C|} }
    \hline
\rowcolor{lightgray}
\multicolumn{2}{|>{\hsize=\twocolwidth}C|}{\textbf{Strongly Agree}} 
& \multicolumn{2}{>{\hsize=\twocolwidth}C|}{\textbf{Agree}} 
& \multicolumn{2}{>{\hsize=\twocolwidth}C|}{\textbf{Neither Agree or Disagree}} 
& \multicolumn{2}{>{\hsize=\twocolwidth}C|}{\textbf{Disagree}} 
& \multicolumn{2}{>{\hsize=\twocolwidth}C|}{\textbf{Strongly Disagree}} \\ \hline
\%   & No. & \%   & No. & \%   & No. & \%   & No. & \%  & No.\\ \hline
\rowcolor{lightlightgray}
17\% & 25  & 38\% & 57  & 27\% & 40  & 10\% & 15  & 9\% & 13 \\ \hline
\end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

相关内容