Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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
      \ifnum\the\value{currCol}<\the\value{numCols}% need additional columns
        \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.

gotocolumn results

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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