2

From an earlier post, tabular environment: incrementing the headings, I learned some of the basics of incrementing a counter in an array. However, I don't know enough yet to set up a counter for the following prescribed rule filling in the table from the linked post. The rule is x is either 0 if the blue die is chosen or 1 if the red die and y is the number of dots on the chosen die. The die with greater value is chosen and the tie goes to red.

The code Werner supplied from the previous post is:

\documentclass{article}
\usepackage{array}

\newcounter{bluecol}\newcounter{redrow}
\newcommand{\insertblue}{%
  \relax\ifnum\value{bluecol}>0 blue${}=\number\numexpr7-
  \value{bluecol}$\addtocounter{bluecol}{-1}\fi}
\newcommand{\insertred}{%
  \stepcounter{redrow}red${}= \theredrow$}

\begin{document}

\setcounter{redrow}{0}
\setcounter{bluecol}{6}
\begin{tabular}{|>{\insertred}c*{6}{|>{\insertblue}c}|}
  \hline
  \multicolumn{1}{|c|}{} &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  \hline
\end{tabular}

\end{document}

enter image description here

dustin
  • 18,617
  • 23
  • 99
  • 204

1 Answers1

2

Here is an update to my answer using ideas from Counters for use in array/tabular cells:

enter image description here

\documentclass{article}
\usepackage{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}

\newcounter{bluecol}\newcounter{redrow}
\newcommand{\insertblue}{%
  \relax\ifnum\value{bluecol}>0 blue${}= \number\numexpr7-\value{bluecol}$\addtocounter{bluecol}{-1}%
  \else
    \ifnum\value{tabrow}>\value{tabcol}%
      $(1,\number\numexpr\value{tabrow}-1)$%
    \else
      $(0,\number\numexpr\value{tabcol})$%
    \fi%
  \fi}
\newcommand{\insertred}{%
  \stepcounter{redrow}red = $\theredrow$}

\begin{document}

\setcounter{redrow}{0}
\setcounter{bluecol}{6}
\begin{tabular}{|>{\insertred}c*{6}{|>{\insertblue}c}|}
  \hline
  \multicolumn{1}{|c|}{} &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  &&&&&& \\
  \hline
\end{tabular}

\end{document}

tabrow and tabcol keep track of the table row and column (both starting from 1). Within the inner part of the table, \insertblue conditions on whether red < blue and sets the element accordingly.

\insertblue inserts the blue header only within the header.

Werner
  • 603,163