使用 collcell 进行行/列索引

使用 collcell 进行行/列索引

我正在尝试复制用于数组/表格单元格的计数器使用collcell。然而,收集和打印机制(以及任何必要的调节)变得不同步。

在此处输入图片描述

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

我该如何调整collcell才能使右矩阵与左矩阵相匹配?

答案1

您必须先设置列计数器,然后再执行\collcell。接下来,您必须在处理完行之后延迟计数器的(重新)设置。

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

必须对负责管理可选参数的宏进行类似的修补\\

相关内容