4

I have an example of some code that colors a column, and I show here that the uncolored column has less spacing between rows. How do I color my column text without changing the spacing between rows?

\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}
User0
  • 163
  • One solution is to just use \textcolor{mytext} rather than {\color{}mytext} in the actual table, but quite laborious to put that around each entry. – User0 Jun 20 '18 at 01:01
  • If you need the complete table text should be in red, you can try {\color{red} \begin{tabular} ... \end{tabular} } – David Jun 20 '18 at 06:11

3 Answers3

1

It works OK with the l column type, so we can define a new column type, M{}, which puts a minipage in a column of type 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}

coloured tabular columns without space

cfr
  • 198,882
1

Add \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}

enter image description here

Ulrike Fischer
  • 327,261
0

It works fine if we replace parbox with mbox. You can read this to understand the difference.

\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}

Output:

enter image description here

As I have mentioned in the comment, if you need the complete table text to be in a single color, you can just wrap tabular with \color{red} \begin{tabular} ... \end{tabular} } or \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}   }

Caution:

This approach make the complete tabular to be red in colour including the \hlines.

enter image description here

David
  • 1,964