[Tex/LaTex] Counters for use in array/tabular cells

arraysconditionalscounterstables

The following practical example

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\newcounter{tabcol}\newcounter{tabrow}
\newcolumntype{C}{>{\stepcounter{tabcol}}c}
\begin{document}
\[
  \begin{array}{>{\stepcounter{tabrow}\setcounter{tabcol}{1}}CCC}
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol)
  \end{array}
\]
\end{document}​

outputs row and column indices:

enter image description here

Somewhat inspired by the construction of Auto generate table with tons of + and –, what would be the most flexible way to implement a row/column indexing system like above that would fit within the standard tabular and array environments without having to intervene with the column specification?

For example, mixing l, c and r (and X and other) column types requires one to define a new column type to step the appropriate column for every such column type, which isn't very convenient.

As mentioned, I am interested in a technique of incorporating these counters with the traditional tabular and array environments (and therefore not something like a tikz matrix).

The end-game would be to use the row/column indices to condition on the cell contents (perhaps colouring, perhaps some output or whatever); similar in style to the following elementary example:

enter image description here

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\newcounter{tabcol}\newcounter{tabrow}
\newcolumntype{C}{>{\stepcounter{tabcol}}c}
\newcommand{\oddornot}{\relax\ifodd\numexpr\value{tabrow}+\value{tabcol}\relax odd\else even\fi}
\begin{document}
\begin{tabular}{>{\stepcounter{tabrow}\setcounter{tabcol}{1}}CCC}
  \oddornot & \oddornot & \oddornot \\
  \oddornot & \oddornot & \oddornot \\
  \oddornot & \oddornot & \oddornot
\end{tabular}
\end{document}​

I'm assuming a solution approach would entail a redefinition of & and \\ for counter stepping and renewal. However, I'm not comfortable doing that without (perhaps) breaking something else along the way.

Best Answer

Something not unlike this (although to do a full job you'd have to worry more about nested tables (which needs a stack to reset and restore the global counter) and \multicolumn (which needs to advance the column counter by the appropriate amount)

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array

\makeatletter
\def\insert@column{%
   \the@toks \the \@tempcnta
   \global\advance\c@tabcol\@ne
   \ignorespaces \@sharp \unskip
   \the@toks \the \count@ \relax}

\let\old@arraycr\@arraycr
\def\@arraycr{\global\c@tabcol\z@\global\advance\c@tabrow\@ne\old@arraycr}

\let\old@tabarray\@tabarray
\def\@tabarray{\global\c@tabrow\@ne\global\c@tabcol\z@\old@tabarray}

\makeatother
\newcounter{tabcol}\newcounter{tabrow}
\begin{document}
\[
  \begin{array}{ccc}
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol)
  \end{array}
\]
\end{document}​