23

I have a table and I want to make the font color red for just one row (not the background). How do I do that? Here's my table:

  \begin{tabular}{ l | l l l l }
        & 1 & 2 & 3 & 4 \\ 
    \hline 
    1   & A & B & C & D \\ 
    2   & A & B & C & D \\ 
    3   & A & B & C & D \\ 
    4   & A & B & C & D \\ 
  \end{tabular}
1

3 Answers 3

18

You could use the tabu environment of the tabu package with the command \rowfont, for example:

\documentclass{article}
\usepackage{tabu}
\usepackage{xcolor}
\begin{document}
  \begin{tabu}{ l | l l l l }
  \rowfont{\color{red}}
        & 1 & 2 & 3 & 4 \\ 
    \hline 
    1   & A & B & C & D \\ 
    2   & A & B & C & D \\ 
    3   & A & B & C & D \\ 
    4   & A & B & C & D \\ 
  \end{tabu}
\end{document}

enter image description here

8
  • 1
    Much better than my idea of a per-column \color{red}.
    – Werner
    Commented Aug 23, 2011 at 17:52
  • 1
    @doncherry: unaltered it behaves very similarly. Differences are explained in the manual, there's a section dedicated to it.
    – Stefan Kottwitz
    Commented Aug 23, 2011 at 19:10
  • 1
    @Eddy: don't worry, installation is not hard, and by trying that you will get further knowledge. You can download it from CTAN, and see How do I install a dtx file, Placing sty files and if you use TeX Live (not Xubuntu repositories) Installing tabu.sty.
    – Stefan Kottwitz
    Commented Aug 23, 2011 at 20:24
  • 2
    Figured out how to do it: 1. Download tabu.dtx and tabu.ins 2. run latex tabu.ins 3. move tabu.sty to $HOME/texmf/tex/latex/commonfiles 4. run texhash $HOME/texmf
    – Eddy
    Commented Aug 28, 2011 at 11:49
  • 2
    Unfortunately, the tabu package appears to be broken and unmaintained these days.
    – mat
    Commented May 13, 2021 at 11:21
38

If you don't want to use the tabu package you can define a \rowstyle command together with supporting column types + and = as follows:

\documentclass{article}

\usepackage{array}
\usepackage{xcolor}

\makeatletter

\newcommand*{\@rowstyle}{}

\newcommand*{\rowstyle}[1]{% sets the style of the next row
  \gdef\@rowstyle{#1}%
  \@rowstyle\ignorespaces%
}

\newcolumntype{=}{% resets the row style
  >{\gdef\@rowstyle{}}%
}

\newcolumntype{+}{% adds the current row style to the next column
  >{\@rowstyle}%
}

\makeatother

\begin{document}

\begin{tabular}{ =l | +l +l +l +l }
  \rowstyle{\color{red}}
      & 1 & 2 & 3 & 4 \\
  \hline
  1   & A & B & C & D \\
  2   & A & B & C & D \\
  3   & A & B & C & D \\
  4   & A & B & C & D \\
\end{tabular}

\end{document}
1
  • 2
    Excellent answer. I hate the tabu package and its documentation. Your solution works with tabular* too! Commented May 31, 2018 at 15:17
4

In {NiceTabular} of nicematrix, you have a command \RowStyle to specify formatting instructions for the current row.

\documentclass{article}

\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{l|llll}
\RowStyle{\color{red}}
    & 1 & 2 & 3 & 4 \\ 
\hline 
1   & A & B & C & D \\ 
2   & A & B & C & D \\ 
3   & A & B & C & D \\ 
4   & A & B & C & D \\ 
\end{NiceTabular}

\end{document}

Output of the first code

There is also a key color for that command \RowStyle. color=red will insert \leavevmode\color{red} with the \leavevmode which is necessary in columns of type p, m, b and X (otherwise, there is an extra vertical space).

\documentclass{article}

\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{l|p{1cm}p{1cm}p{1cm}p{1cm}}
\RowStyle[color=red]{}
    & 1 & 2 & 3 & 4 \\ 
\hline 
1   & A & B & C & D \\ 
2   & A & B & C & D \\ 
3   & A & B & C & D \\ 
4   & A & B & C & D \\ 
\end{NiceTabular}

\end{document}

Output of the second code

3
  • Clean solution, but doesn't play well with beamer for some reason. The presentation compiles, but \uncover commands are suddenly ignored. I ended up using tabu. Commented Apr 11 at 14:43
  • 1
    @Anthony Labarre: You should post another question with a MWE illustrating that problem... By the way, don't forget the tabu package is bugged and no longer maintained : see that question . Commented Apr 11 at 16:53
  • thanks, I was not aware of that. I'll post another question if the need arises, but so far so good. Commented Apr 12 at 12:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .