2

Is it possible to let LaTeX increment the headings in a table environment? If so, how could I do this?

For example, I would like to have 7 columns and 7 rows where the columns except the first are blue = $1$ & blue = $2$ & ... and similarly the rows except the first are red = $1$ & ....

Is there a way to have LaTeX do this? In TikZ, I could use a foreach loop.

\documentclass{article}
\begin{document}
\begin{table}
  \begin{tabular}{|c|c|c|c|c|c|c|}
  \hline
    & blue = $1$ & blue = $2$ & blue = $3$ & ... \\
    red = $1$ & & ... \\
    red = $2$ & & ... \\
    red = $3$ & & ... \\
    ...
  \end{tabular}
\end{table}
\end{document} 
Werner
  • 603,163
dustin
  • 18,617
  • 23
  • 99
  • 204

1 Answers1

4

Here's a way, all supported by array for insertion of content at the start of a cell:

enter image description here

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

The first \multicolumn is to prevent red from being set in the first row.

Werner
  • 603,163