表格行和列的背景颜色

表格行和列的背景颜色

我在更改表格单元格/行的背景时遇到问题。

这是我使用的代码:

\documentclass{article}

\usepackage[skip=0.333\baselineskip]{caption}
\usepackage{booktabs,tabularx,ragged2e}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage[table]{xcolor}

\begin{document}
\renewcommand{\arraystretch}{1.3}
\newcolumntype{Y}{>{\hsize=.7\hsize\RaggedRight\arraybackslash}X}
\newcolumntype{B}{X}

\begin{table}
\caption{Sources and targets}
\begin{tabularx}{\textwidth}{p{1cm}@{}*{4}{Y}@{}}
%% table header
\toprule
\rowcolor{lightgray}\multicolumn{2}{r@{}}{List targets} $\rightarrow$ & \multicolumn{3}{c@{}}{Targets} \\
\cmidrule(l){3-5}
\rowcolor{lightgray}\multicolumn{2}{l@{}}{\cellcolor{lightgray}$\downarrow$ Sources}  & Target 1 & Target 2 & Target 3 \\
\midrule
%% body of table
\multirow{3}{2cm}{\rotatebox{90}{\parbox{\linewidth}{sources}}} &\cellcolor{lightgray}Source 1 & Description 10 
         & Description 11 
         & Description 12 \\
\addlinespace % <-- use whitespace as a simple yet very effective divider
&\cellcolor{lightgray}Source 2 (additional info) 
         & Description 20 
         & Description 21
         & Description 22, \par
           Description 23, \par
           Description 24 \\
\addlinespace % ditto
&\cellcolor{lightgray}Source 3 & Description 30 
         & Description 31
         & Description 32, \par 
           Description 33, \par 
           Description 34 \\    
\bottomrule
\end{tabularx}
\end{table}


\end{document}

结果是这样的: 在此处输入图片描述

有人能帮我让它看起来更好吗?如果没有单元格之间的空白,并且这个垂直列(源)也应该具有相同的灰色背景。

答案1

改编:

  • 删除addlinespacetoprulemidrulebottomrule删除空格(并使用hline
  • 更正标题:...{List targets $\rightarrow$}
  • 使用以下选项为整个列选择背景颜色\newcolumntype{a}{>{\columncolor{lightgray}}Y}(从如何为表格列添加颜色?
  • 对于多行的第一列,你必须\multirow{-3}{...}{...}在三行的最后一行写入,否则背景灰色将覆盖文本(参见texblog:在 LaTeX 中为多行表格着色

代码:

\documentclass{article}

\usepackage[skip=0.333\baselineskip]{caption}
\usepackage{booktabs,tabularx,ragged2e}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage[table]{xcolor}

\begin{document}
\renewcommand{\arraystretch}{1.3}
\newcolumntype{Y}{>{\hsize=.7\hsize\RaggedRight\arraybackslash}X}
%\newcolumntype{B}{X}
\newcolumntype{a}{>{\columncolor{lightgray}}Y}
\newcolumntype{C}[1]{>{\columncolor{lightgray}}p{#1}} % zentriert mit

\begin{table}
    \caption{Sources and targets}
    \begin{tabularx}{\textwidth}{C{5mm}@{}a*{3}{Y}@{}}
        %% table header
        \hline
        \rowcolor{lightgray}\multicolumn{2}{r@{}}{List targets $\rightarrow$} & \multicolumn{3}{c@{}}{Targets} \\
        \rowcolor{lightgray}\multicolumn{2}{l@{}}{$\downarrow$ Sources}  & Target 1 & Target 2 & Target 3 \\ \hline
        %% body of table
        & Source 1 & Description 10 
                 & Description 11 
                 & Description 12 \\
        &Source 2 (additional info) 
                 & Description 20 
                 & Description 21
                 & Description 22, \par
                   Description 23, \par
                   Description 24 \\
        \multirow{-3}{12mm}{\rotatebox{90}{\parbox{\linewidth}{sources}}} &Source 3 & Description 30 
                 & Description 31
                 & Description 32, \par 
                   Description 33, \par 
                   Description 34 \\ \hline
    \end{tabularx}
\end{table}

\end{document}

结果:

在此处输入图片描述

答案2

我宁愿考虑如何设计漂亮的桌子并将重新设计您的表格如下:

在此处输入图片描述

姆韦

\documentclass{article}
\usepackage[skip=0.33\baselineskip]{caption}
\usepackage{ragged2e}
\usepackage{booktabs, multirow, tabularx}
\newcolumntype{L}{>{\RaggedRight}X}
\usepackage{graphicx}

\begin{document}

\begin{table}
    \caption{Sources and targets}
    \begin{tabularx}{\textwidth}{*{4}{L} }
        \toprule
    \multicolumn{1}{c}{} & \multicolumn{3}{c}{Targets} \\
    \cmidrule{2-4}
    sources     & Target 1
                & Target 2
                & Target 3          \\
        \midrule
    %      
    Source 1
                & Description 10
                & Description 11
                & Description 12    \\
        \addlinespace
    Source 2\par  (additional info)
                & Description 20
                & Description 21
                & Description 22,  \par
                  Description 23,  \par
                  Description 24   \\
        \addlinespace
     Source 3   & Description 30
                & Description 31
                & Description 32,   \par
                  Description 33,   \par
                  Description 34    \\
        \bottomrule
    \end{tabularx}
\end{table}
\end{document}

答案3

这是由于 booktabs 规则的垂直填充所致。解决方案是中和此填充,并通过增加 来补偿\arraystretch。对于特定的两个\addlinespace,我将其替换为\\[\adddefaultspace]。最后,我将 替换\cmidrule\hhline,代价是增加一个补充(空)列。

>{\hsize=coeff\hsize}另一点是,您误解了for columns的目的和语法X:系数的总和必须等于列的总数。如果您想减少相等的 X 列的大小,只需减少表的总大小即可。

    \documentclass{article}

    \usepackage[skip=0.333\baselineskip]{caption}
    \usepackage{booktabs,tabularx,ragged2e}
    \usepackage{multirow}
    \usepackage{graphicx, hhline}
    \usepackage[table]{xcolor}

    \begin{document}
    \renewcommand{\arraystretch}{1.4}
    \newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}%\hsize=.7\hsize
    \newcolumntype{B}{X}

    \begin{table}[!htb]
    \setlength{\aboverulesep}{0pt}
    \setlength{\belowrulesep}{0pt}
    \caption{Sources and targets}
    \begin{tabularx}{\textwidth}{p{1cm}@{}*{4}{Y}@{} >{}c}
    %% table header
    \cmidrule[\heavyrulewidth](r{-\tabcolsep}){1-5}
    \rowcolor{lightgray}\multicolumn{2}{r@{}}{List targets $\rightarrow$ }& \multicolumn{3}{c@{}}{Targets}& \\
   \hhline{>{\arrayrulecolor{lightgray}}-->{\arrayrulecolor{black}}--->{\arrayrulecolor{lightgray}}->{\arrayrulecolor{black}}}
    \rowcolor{lightgray}\multicolumn{2}{l@{}}{\cellcolor{lightgray}$\downarrow$ Sources} & Target 1 & Target 2 & Target 3 \\ \cmidrule[\lightrulewidth](r{-\tabcolsep}){1-5}
    %% body of table
    \multirow{7}{*}[\defaultaddspace]{\rotatebox[origin =tr]{90}{sources}} &\cellcolor{lightgray}Source 1 & Description 10
             & Description 11
             & Description 12 \\[2\defaultaddspace]
    &\cellcolor{lightgray}Source 2 (additional info)
             & Description 20
             & Description 21
             & Description 22, \par
               Description 23, \par
               Description 24 \\[\defaultaddspace]
    &\cellcolor{lightgray}Source 3 & Description 30
             & Description 31
             & Description 32, \par
               Description 33, \par
               Description 34 \\
    \cmidrule[\heavyrulewidth](r{-\tabcolsep}){1-5}
    \end{tabularx}
    \end{table}

    \end{document} 

在此处输入图片描述

相关内容