15

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

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

  • 1
    Much better than my idea of a per-column \color{red}. – Werner Aug 23 '11 at 17:52
  • Is an unaltered tabu environment equivalent to an unaltered tabular environment? – doncherry Aug 23 '11 at 17:58
  • 1
    @doncherry: unaltered it behaves very similarly. Differences are explained in the manual, there's a section dedicated to it. – Stefan Kottwitz Aug 23 '11 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 Aug 23 '11 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 Aug 28 '11 at 11:49
21

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
    Excellent answer. I hate the tabu package and its documentation. Your solution works with tabular* too! – Krishna May 31 '18 at 15:17
  • You can even put a \rowcolor{<colour>} in front of the row and have colourised background and text. However, I wish someone "lifts" the \rowfont command from tabu and rewrite it for use with tabular & friends. – Sveinung Jul 5 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.