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} 
  • Can you fill in more of the ... to show the desired table. I have a suspicion (given the comments on my answer) that I can't guess what you intend. – David Carlisle Oct 10 '14 at 15:23
  • @DavidCarlisle already edited to show intent better. – dustin Oct 10 '14 at 15:25
  • It's still very unclear (I'd never have guessed you meant the layout Werner showed:-) – David Carlisle Oct 10 '14 at 15:26
  • @DavidCarlisle: It's that crystal ball I carry around... ;) – Werner Oct 10 '14 at 15:29
  • @DavidCarlisle what book would you recommend to read to grasp this part of LaTeX better? – dustin Oct 10 '14 at 16:05
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.

  • Oh well OK if that's what he meant you get +1 and a crystal ball and I'll delete my answer:-) – David Carlisle Oct 10 '14 at 15:24
  • as promised :-) – David Carlisle Oct 10 '14 at 15:27
  • I have a follow question on automating the entries since they will go something like (1, 1) & (0, 2) & (0, 3) & ... & (0, 6) for row 2 as an example. This entries are built on a rule though. I cant seem to determine how to execute it from what I learned from this post though. Should I include it here or make a new question? – dustin Oct 10 '14 at 15:51
  • 1
    Could you describe the rule? I assume this doesn't only hold for row 2, but for other rows as well. You may be interested in looking at Counters for use in array/tabular cells. – Werner Oct 10 '14 at 15:54
  • The rule is the larger number is chosen from red or blue. Then X is 0 for blue chosen and 1 for red. Y is the number chosen. – dustin Oct 10 '14 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.