4

This produces a 2x2 table whose first row is blue with white text and whose second row is white with black text. Although it works fine it does repeat \textcolor{white} in each cell of the row where the cells are to have white text. Is there some way of specifying that the text in the first row should be white without having to repeatedly specify it over again for each cell of that row? Its not so bad in this small example but I am concerned that it will get tiresome on larger tables.

\documentclass{article}
\usepackage{tabularx}
\usepackage{ragged2e}        
\usepackage{colortbl}
\newcolumntype{L}{>{\raggedright\arraybackslash}X} 
\begin{document}
\begin{center}\Large
\noindent\begin{tabularx}{1\textwidth}{|L|L|}
\hline
% NEXT LINE REPEATS WHITE FOR EACH CELL
\rowcolor{blue}\textcolor{white}{Jan}&\textcolor{white}{Feb}\\ 
Mar&Apr\\ 
\hline
\end{tabularx}
\end{center}
\end{document}

EDIT: This question has already been answered and accepted with an answer directly addressing the tabularx environment in the question's example code but since then I found that the tabu package can do this.

As this involves using a different package than that presented in the original question I am not going to put this as an answer but am just adding the following example of how to implement this using tabu in case anyone is interested in another approach.

\documentclass{article}
\usepackage{tabu}
\usepackage{colortbl}
\begin{document}
\begin{center}\Large
\noindent\begin{tabu} to \textwidth{|X[l,m]|X[l,m]|}
\tabucline-
\rowfont{\color{white}}\rowcolor{blue}Jan&Feb\\ 
Mar&Apr\\ 
\tabucline-
\end{tabu}
\end{center}
\end{document}
user1189687
  • 1,953

1 Answers1

4

\noalign is not a supported latex command, but...

\documentclass{article}
\usepackage{tabularx}
\usepackage{ragged2e}        
\usepackage{colortbl}
%\newcolumntype{L}{>{\rtcolor\raggedright\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash\leavevmode\rtcolor\ignorespaces}X}
\def\rowtextcolor#1{\noalign{\gdef\rtcolor{\color{#1}}}}
\global\let\rtcolor\relax
\begin{document}
\begin{center}\Large
\noindent
\begin{tabularx}{1\textwidth}{|L|L|}
\hline
% NEXT LINE REPEATS WHITE FOR EACH CELL
\rowcolor{blue}\rowtextcolor{white}
Jan&Feb\\ 
\rowtextcolor{black}
Mar&Apr\\ 
\hline
\end{tabularx}
\end{center}
\end{document}

Note:

The first (commented out) version puts \color at the start of the implicit parbox of the X column. As noted in the color package documentation this can have some unwanted effects on spacing (but is the best thing to do if the entries start with vertical material (eg vspace or display lists).

The second version is better if the entries are just text, it starts the paragraph with \leavevmode before inserting the color command so spacing is not affected.

These spacing issues are intrinsic to the way LaTeX does colour and not really related to the tabular code.

David Carlisle
  • 757,742
  • That does appear to eliminate the need to repeat the command but it seems to cause the cells to now be two lines high with the text put on the second line whereas the original code had one line per cell. Do you know what is causing that and more importantly how to get rid of that effect? – user1189687 Jul 07 '12 at 00:23
  • Try now, added second version and some notes. – David Carlisle Jul 07 '12 at 00:32
  • Thanks. That does work. Do you know where I can find out more about those commands. My Kopke and Daly has limited discussion of TeX. Also I was hoping the solution would be generalizable to any sort of markup for all the cells in a row and I would like to be able to understand what I am doing. – user1189687 Jul 07 '12 at 00:46
  • 1
    \noalign and gdef are tex primitives so the texbook is the ultimate source but I would guess tex by topic (now free) would have pretty full description (or search this site:-) It is pretty general it doesn't have to be color. The > inserts a command \rtcolor here into every cell and then at the beginning of each row you could define it to be anything. – David Carlisle Jul 07 '12 at 00:50
  • 1
    Based on that explanation it seems we can generalize rtcolor to rtattr giving this \newcolumntype{L}{>{\raggedright\arraybackslash\leavevmode\rtattr\ignorespaces}X} \def\rowtext#1{\noalign{\gdef\rtattr{#1}}} \global\let\rtattr\relax which is called like this \rowtext{\color{blue}} so that the attribute can be color or anything else we wish to apply to factor out from the row cells. – user1189687 Jul 07 '12 at 03:09
  • I think I understand how it works now. It puts \rtcolor in every cell by assuming that every column will include rtcolor in its column spec. Then its a matter of repeatedly redefining rtcolor at the start of each row so the latest definition is substituted in. Its very clever & I have certainly learned a lot trying to understand it, in particular, the limitations of the environments for tables. I am going to accept it since its likely the best that can be expected given these limitations but the interplay between columns & rows seems complex so I am not sure I will use it in practice. – user1189687 Jul 07 '12 at 04:21