如何使用 xcolor 为列文本着色,但不改变行之间的间距?

如何使用 xcolor 为列文本着色,但不改变行之间的间距?

我有一个为列着色的代码示例,我在这里显示未着色的列的行间距较小。如何在不改变行间距的情况下为列文本着色?

\documentclass{article}
\usepackage{array}
\usepackage{xcolor}
\begin{document}
\begin{tabular}{>{\color{red}}p{2cm}>{\color{red}}p{2cm}}
one & two \\
three & four \\
five & six\\
seven & eight \\
nine & ten
\end{tabular}

\begin{tabular}{p{2cm}p{2cm}}
one & two \\
three & four \\
five & six\\
seven & eight \\
nine & ten
\end{tabular}
\end{document}

答案1

添加\leavevmode

\documentclass{article}
\usepackage{array}
\usepackage{xcolor}
\begin{document}
\begin{tabular}{>{\leavevmode\color{red}}p{2cm}>{\leavevmode\color{red}}p{2cm}}
one & two \\
three & four \\
five & six\\
seven & eight \\
nine & ten
\end{tabular}

\begin{tabular}{p{2cm}p{2cm}}
one & two \\
three & four \\
five & six\\
seven & eight \\
nine & ten
\end{tabular}
\end{document}

在此处输入图片描述

答案2

它与列类型配合良好l,因此我们可以定义一个新的列类型,,M{}将 放入minipage类型为 的列中l

\documentclass{article}
\usepackage{array}
\usepackage[table]{xcolor}
\newcolumntype{M}[1]{>{\begin{minipage}{#1}\arraybackslash}l<{\end{minipage}}}
\begin{document}
\begin{tabular}{>{\color{red}}M{2cm}>{\color{red}}M{2cm}}
  one & two \\
  three & four \\
  five & six\\
  seven & eight \\
  nine & ten
\end{tabular}
\begin{tabular}{>{\color{blue}}M{20mm}>{\color{green}}M{20mm}}
  one & two \\
  three & four \\
  five & six\\
  seven & eight \\
  nine & ten
\end{tabular}

\begin{tabular}{p{2cm}p{2cm}}
  one & two \\
  three & four \\
  five & six\\
  seven & eight \\
  nine & ten
\end{tabular}
\end{document}

无空格的彩色表格列

答案3

parbox如果我们用替换它,效果会很好mbox。你可以阅读了解其中的差异。

\documentclass{article}
\usepackage{array}
\usepackage{xcolor}

\begin{document}

    \begin{tabular}{>{\color{red} } m{2cm} > {\color{blue} } m{2cm} }
        one & two \\
        three & four \\
        five & six\\
        seven & eight \\
        nine & ten
    \end{tabular}   
    \begin{tabular}{p{2cm}p{2cm}}
        one & two \\
        three & four \\
        five & six\\
        seven & eight \\
        nine & ten
    \end{tabular}
\end{document}

输出:

在此处输入图片描述

正如我在评论中提到的,如果您需要让完整的表格文本采用单一颜色,则只需用\color{red} \begin{tabular} ... \end{tabular} }或包装表格即可\textcolor{red}{ \begin{tabular} ... \end{tabular} }

\textcolor{red}{
    \begin{tabular}{p{2cm} p{2cm}}
        \hline 
        one & two \\
        \hline 
        three & four \\
        five & six\\
        seven & eight \\
        nine & ten
    \end{tabular}   }

警告:

这种方法使整个表格(包括 s)变成红色\hline

在此处输入图片描述

相关内容