单独为 Latex 表的单元格着色时出现填充问题

单独为 Latex 表的单元格着色时出现填充问题

我以 booktabs 样式创建了以下 latex 表格,并为每个单元格涂上不同的颜色。但最后,我看到一些填充没有着色,因为单元格内容的大小不同。我该如何解决这个问题?

我的彩色桌子

但我只是想创建下表:

实际的表格

这是 latex 表的代码。它有点长,但有助于理解问题。

\usepackage{colortbl}
\usepackage{booktabs}
\definecolor{gray1}{RGB}{238,236,225}
\definecolor{gray2}{RGB}{224,224,224}
\definecolor{oran1}{RGB}{255,192,0}
\definecolor{yelw1}{RGB}{255,227,111}
\definecolor{blue1}{RGB}{197,217,241}
\definecolor{gree1}{RGB}{196,215,155}
\definecolor{crea1}{RGB}{252,245,238}
\definecolor{pink1}{RGB}{218,150,148}

\begin{table}[]
\centering
\caption{Raylı Sistemlerde Maliyet Optimizasyonu}
\label{tab:optOneri}
\resizebox{\columnwidth}{!}{%
\begin{tabular}{@{}cccccccccc@{}}
\rowcolor{gray1}
\multicolumn{10}{c}{Maliyet Optimizasyonu}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    \\
                                                                & \multicolumn{4}{c}{\cellcolor{gray2}İlk Yapım Maliyeti}                                                                                                                                                                                                                                     & \multicolumn{5}{c}{\cellcolor{pink1}İşletme Maliyeti}                                                                                                                                                                                                                                                                                                                                                                                                            \\
\begin{tabular}[c]{@{\cellcolor{yelw1}}c@{}}Dizideki Araç \\ Sayısı\end{tabular} & \begin{tabular}[c]{@{\cellcolor{blue1}}c@{}}Toplam İstasyon\\ Maliyeti\end{tabular} & \begin{tabular}[c]{@{\cellcolor{blue1}}c@{}}Toplam Hatyapım\\ Maliyeti\end{tabular} & \begin{tabular}[c]{@{\cellcolor{blue1}}c@{}}Toplam Araç\\ Maliyeti\end{tabular} & \begin{tabular}[c]{@{\cellcolor{oran1}}c@{}}Toplam Yapım\\ Maliyeti\end{tabular} & \begin{tabular}[c]{@{\cellcolor{yelw1}}c@{}}Toplam\\ İstasyon\\ İşletme\\ Maliyeti\\ (yıllık)\end{tabular} & \begin{tabular}[c]{@{\cellcolor{yelw1}}c@{}}Toplam\\ Hat\\ İşletme\\ Maliyeti\\ (yıllık)\end{tabular} & \begin{tabular}[c]{@{\cellcolor{yelw1}}c@{}}Toplam\\ Sistem\\ İşletme\\ Maliyeti\\ (yıllık)\end{tabular} & \begin{tabular}[c]{@{\cellcolor{pink1}}c@{}}Toplam\\ İşletme\\ Maliyeti\\ (yıllık)\end{tabular} & \begin{tabular}[c]{@{\cellcolor{pink1}}c@{}}Toplam\\ İşletme\\ Maliyeti\\ (20 yıllık)\end{tabular} \\
\rowcolor{crea1}4                                              &                                                                    &                                                                    &                                                                &                                                                 &                                                                                           &                                                                                      &                                                                                         &                                                                                &                                                                                   \\
\rowcolor{crea1}5                                              &                                                                    &                                                                    &                                                                &                                                                 &                                                                                           &                                                                                      &                                                                                         &                                                                                &                                                                                   \\
\rowcolor{crea1}6                                              &                                                                    &                                                                    &                                                                &                                                                 &                                                                                           &                                                                                      &                                                                                         &                                                                                &                                                                                   \\
\rowcolor{crea1}8                                              &                                                                    &                                                                    &                                                                &                                                                 &                                                                                           &                                                                                      &                                                                                         &                                                                                &                                                                                  

\end{tabular}
}
\end{table}

谢谢。

答案1

这是一个使用\makecell命令替换嵌套tabular环境的简单解决方案。该makecell包允许在单元格内换行。此外,我不使用\resizebox,而是用较小的fontsize和替换它\tabcolsep,并加载caption以在标题和表格之间获得正确的垂直间距。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
\usepackage{colortbl}
\usepackage{array, makecell, caption}
\definecolor{gray1}{RGB}{238,236,225}
\definecolor{gray2}{RGB}{224,224,224}
\definecolor{oran1}{RGB}{255,192,0}
\definecolor{yelw1}{RGB}{255,227,111}
\definecolor{blue1}{RGB}{197,217,241}
\definecolor{gree1}{RGB}{196,215,155}
\definecolor{crea1}{RGB}{252,245,238}
\definecolor{pink1}{RGB}{218,150,148}

\begin{document}
\begin{table}[]
  \centering\footnotesize
  \setlength\tabcolsep{5pt}
  \caption{Raylı Sistemlerde Maliyet Optimizasyonu}
  \label{tab:optOneri}
    \begin{tabular}{@{}*{10}{c}@{}}
      \rowcolor{gray1}
      \multicolumn{10}{@{}c@{}}{Maliyet Optimizasyonu} \\
      & \multicolumn{4}{c}{\cellcolor{gray2}İlk Yapım Maliyeti} & \multicolumn{5}{c@{}}{\cellcolor{pink1}İşletme Maliyeti} \\
      \cellcolor{yelw1}\makecell{Dizideki \\Araç \\ Sayısı} &
      \cellcolor{blue1}\makecell{Toplam\\ İstasyon\\ Maliyeti} & \cellcolor{blue1}\makecell{Toplam\\ Hatyapım\\ Maliyeti} & \cellcolor{blue1}\makecell{Toplam\\ Araç\\ Maliyeti} & \cellcolor{oran1}\makecell{Toplam\\ Yapım\\ Maliyeti} & \cellcolor{yelw1}\makecell{Toplam\\ İstasyon\\ İşletme\\ Maliyeti\\ (yıllık)} & \cellcolor{yelw1}\makecell{Toplam\\ Hat\\ İşletme\\ Maliyeti\\ (yıllık)} & \cellcolor{yelw1}\makecell{Toplam\\ Sistem\\ İşletme\\ Maliyeti\\ (yıllık)} & \cellcolor{pink1}\makecell{Toplam\\ İşletme\\ Maliyeti\\ (yıllık)} & \cellcolor{pink1}\makecell{Toplam\\ İşletme\\ Maliyeti\\ (20 yıllık)} \\
      \rowcolor{crea1}4 & & & & & & & & & \\
      \rowcolor{crea1}5 & & & & & & & & & \\
      \rowcolor{crea1}6 & & & & & & & & & \\
      \rowcolor{crea1}8 & & & & & & & & &
    \end{tabular}
\end{table}

\end{document} 

在此处输入图片描述

相关内容