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:
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:
\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.
\relax
in front of\ifodd
: TeX tries to expand the first token in a cell looking for\omit
, so the conditional is evaluated before the counters are stepped.