3

I'm trying to replicate the row/column indexing behaviour presented in Counters for use in array/tabular cells using collcell. However, the collection and printing mechanism (and therefore any conditioning, if necessary) becomes out-of-sync.

enter image description here

\documentclass{article}

\usepackage{collcell}

\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}

\newcommand{\tabrowcol}[1]{(\thetabrow,\thetabcol)}
\newcolumntype{C}{ >{\collectcell\tabrowcol}c<{\endcollectcell} }

\begin{document}

\[
  % Correct row/column indexing
  \begin{array}{ c c c }
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol)
  \end{array}
  \qquad
  % Incorrect row/column indexing
  \begin{array}{ C C C }
    x & x & x \\
    x & x & x \\
    x & x & x
  \end{array}
\]

\end{document}​

How can I adapt collcell so that the right matrix matches the left matrix?

Werner
  • 603,163
  • You are forgetting \stepcounter{tabcol} in the definition of the C column type. However, the last column is processed too late and you get 0. You need a “phantom column” at the end; but I remember something about the last column in the manual of collcell. – egreg Oct 26 '16 at 07:30

1 Answers1

3

You have to step the counter of columns before doing \collcell. Next you have to delay the (re)setting of counters after the row has been processed.

\documentclass{article}

\usepackage{collcell}
\usepackage{xpatch}

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

\xpatchcmd{\@xarraycr}
  {\cr}
  {\cr\noalign{\global\c@tabcol\z@\global\advance\c@tabrow\@ne}}{}{}
\xpatchcmd{\@xargarraycr}
  {\cr}
  {\cr\noalign{\global\c@tabcol\z@\global\advance\c@tabrow\@ne}}{}{}
\xpatchcmd{\@yargarraycr}
  {\cr}
  {\cr\noalign{\global\c@tabcol\z@\global\advance\c@tabrow\@ne}}{}{}

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

\newcommand{\tabrowcol}[1]{(\thetabrow,\thetabcol)}
\newcolumntype{C}{ >{\stepcounter{tabcol}\collectcell\tabrowcol}c<{\endcollectcell} }

\begin{document}

\[
  % Correct row/column indexing
  \begin{array}{ c c c }
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol)
  \end{array}
  \qquad
  % Incorrect row/column indexing
  \begin{array}{ C C C }
    x & x & x \\
    x & x & x \\
    x & x & x
  \end{array}
\]

\end{document}

A similar patch has to be done to the macro responsible for managing the optional argument to \\.

egreg
  • 1,121,712