Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Add \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. –  egreg Aug 2 '12 at 22:28
    
@egreg: Interesting... thanks! –  Werner Aug 2 '12 at 22:44

1 Answer 1

up vote 3 down vote accepted

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}​
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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