separate style and content in table
1. All Cells:
If you want all the cells in a particular column to have the same formatting, you can use the collcell
package which allows you to pass the entry of each column to a command for further processing:
Alternatively, you could also use the array
package, and define:
\newcolumntype{R}{>{\cellcolor{red}}X<{}}
2. Select Cells:
If you only want a select number of cells to be colored then you can use additional commands such as \ActivateColorCell
to enable cell coloring and \DectivateColorCell
to disable the cell coloring as desired:
Notes:
- I used the
etoolbox
package to provide the toggle to test if coloring is enabled, but it should be easy to adapt this to not require that package if desired using solutions from LaTeX conditional expression.
Code: collcell
(all cells):
\documentclass{article}
\usepackage{collcell}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{tabu}
\newcommand{\ColorCellRed}[1]{\cellcolor{red}{#1}}
\newcolumntype{R}{>{\collectcell\ColorCellRed}X<{\endcollectcell}}
\begin{document}
\begin{center}
\begin{tabu}{XXRX}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 \\
9 & 10 & 11 & 12 \\
13 & 14 & 15 & 16 \\
17 & 18 & 19 & 20 \\
\end{tabu}
\end{center}
\end{document}
Code: array
(all cells):
\documentclass{article}
\usepackage{array}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{tabu}
\newcolumntype{R}{>{\cellcolor{red}}X<{}}
\begin{document}
\begin{center}
\begin{tabu}{XXRX}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 \\
9 & 10 & 11 & 12 \\
13 & 14 & 15 & 16 \\
17 & 18 & 19 & 20 \\
\end{tabu}
\end{center}
\end{document}
Code: collcell
(some cells):
\documentclass{article}
\usepackage{collcell}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{tabu}
\usepackage{etoolbox}
\newtoggle{EnableColorCell}
\togglefalse{EnableColorCell}
\newcommand*{\ActivateColorCell}{\global\toggletrue{EnableColorCell}}
\newcommand*{\DectivateColorCell}{\global\togglefalse{EnableColorCell}}
\newcommand{\ColorCellRed}[1]{%
\iftoggle{EnableColorCell}{%
\cellcolor{red}{#1}%
}{%
#1% no cell color as it is disabled
}%
}
\newcolumntype{R}{>{\collectcell\ColorCellRed}X<{\endcollectcell}}
\begin{document}
\begin{center}
\begin{tabu}{XXRX}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 \\\ActivateColorCell
9 & 10 & 11 & 12 \\
13 & 14 & 15 & 16 \\\DectivateColorCell
17 & 18 & 19 & 20 \\
\end{tabu}
\end{center}
\end{document}
Code: array
(some cells):
\documentclass{article}
\usepackage{array}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{tabu}
\usepackage{etoolbox}
\newtoggle{EnableColorCell}
\togglefalse{EnableColorCell}
\newcommand*{\ActivateColorCell}{\global\toggletrue{EnableColorCell}}
\newcommand*{\DectivateColorCell}{\global\togglefalse{EnableColorCell}}
\newcolumntype{R}{>{\iftoggle{EnableColorCell}{\cellcolor{red}}{}}X<{}}
\begin{document}
\begin{center}
\begin{tabu}{XXRX}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 \\\ActivateColorCell
9 & 10 & 11 & 12 \\
13 & 14 & 15 & 16 \\\DectivateColorCell
17 & 18 & 19 & 20 \\
\end{tabu}
\end{center}
\end{document}