# A command to move forward to a specific (numbered) column?

Suppose I want a \gotocolumn command, with the following behavior: in a tabular with n or more columns, \gotocolumn{n} would expand to the shortest combination of \\ and & necessary to bring us forward into the n'th column. So for instance...

• In the first column of a table, \gotocolumn{3} would expand to & &.
• In the second column of a table, \gotocolumn{3} would expand to &.
• In the third column of a table, \gotocolumn{3} would expand to \relax.
• In the fourth or subsequent column of a table, \gotocolumn{3} would expand to \\ & &.

Has this been done? Could it be done? Thoughts on how?

-

You can do some bookkeeping when new columns and rows are started. If you really want to you can redefine & and \\ I chose to introduce some new macros that just "call" the normal ones and do some additional work. It looks like this:

\documentclass{article}
\newcounter{currCol}
\setcounter{currCol}{1}
\def\amp{\stepcounter{currCol}&}
\def\nl{\\\setcounter{currCol}{1}}
\newcounter{numCols}
\setcounter{numCols}{3}
\def\gotocolumn#1{%
\ifnum\the\value{currCol}=#1%done
\else%
\ifnum\the\value{currCol}<#1% before same row
\amp\gotocolumn{#1}%
\else% after, next row
\amp\gotocolumn{#1}%
\else%
\nl\gotocolumn{#1}%
\fi%
\fi%
\fi%
}
\begin{document}
\begin{tabular}{lcr}
test \amp test \amp test\nl
more \amp test \amp stuff\nl
\gotocolumn{2}test \amp test\nl
\gotocolumn{3}last\nl
first \amp \gotocolumn{1}first
\end{tabular}
\end{document}


Which looks like this.

It's not tested very well, so there might be some issues in border cases.

-
Does this mean that in a normal LaTeX tabular -- without redefining the tab/newline commands -- there's no way for a macro to "find out" where in the table it's being evaluated? –  Dan Velleman Dec 21 '11 at 18:04
@DanVelleman: You could use the collcell package to process each cell and keep track of the columns. That way the table would look like a normal table. This recent answer question on customize the cell environment in a tabular shows an example usage of collcell. –  Peter Grill Dec 22 '11 at 5:52
@DanVelleman not as far as I know. Depending on what you mean when you say "redefining the tab/newline commands". Because I'm pretty sure that's exactly what collcell does as well. You can of course redefine the ampersand and \ to make it look like any other table, while performing the same actions as \amp and \nl. –  Roelof Spijker Dec 22 '11 at 12:28
Makes sense. Thanks. –  Dan Velleman Dec 24 '11 at 21:31