10

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.

1
  • 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
    Commented Aug 2, 2012 at 22:28

2 Answers 2

8

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}​
1

The environment {NiceArray} of nicematrix is similar to the classical {array} (of the package array) but has (among other features) built-in LaTeX counters iRow and jCol for the rows and the columns.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\[
  \begin{NiceArray}{ccc}
    (\arabic{iRow},\arabic{jCol}) & (\arabic{iRow},\arabic{jCol}) & (\arabic{iRow},\arabic{jCol}) \\
    (\arabic{iRow},\arabic{jCol}) & (\arabic{iRow},\arabic{jCol}) & (\arabic{iRow},\arabic{jCol}) \\
    (\arabic{iRow},\arabic{jCol}) & (\arabic{iRow},\arabic{jCol}) & (\arabic{iRow},\arabic{jCol})
  \end{NiceArray}
\]

\end{document}​

Output of the first code

In fact, nicematrix has also a command \AutoNiceMatrix to construct a matrix from a pattern.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\[\AutoNiceMatrix{6-6}{(\arabic{iRow},\arabic{jCol})}\]
\end{document}​

Output of the second code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .