10

I am trying to assemble a table that occurs multiple times throughout a document. It always consists of some fixed columns, followed by a set of columns that appear in one table but are not needed in another table. I was told it would be a good idea to use toks registers for this. Never having heard of these, I tried to figure out how I could put them to use in my case. (BTW, is there a good online documentation of this stuff available?) From various examples, I've pieced together this code:

\documentclass{article}

\newtoks\toksA
\newtoks\toksB
\newtoks\toksC
\newtoks\toksD

\newcommand*{\leadingcolumns}{
\toksA={ foo & bar }
\toksB={ Foo & Bar }
\toksC={ fOo & bAr }
\toksD={ foO & baR }
}

\newcommand*{\numbercolumn}{
\toksA=\expandafter{\the\toksA & 1 }
\toksB=\expandafter{\the\toksB & 2 }
\toksC=\expandafter{\the\toksC & 3 }
\toksD=\expandafter{\the\toksD & 4 }
}

\newcommand*{\charactercolumn}{
\toksA=\expandafter{\the\toksA & W }
\toksB=\expandafter{\the\toksB & X }
\toksC=\expandafter{\the\toksC & Y }
\toksD=\expandafter{\the\toksD & Z }
}

\newcommand{\printtable}{
\the\toksA \tabularnewline
\the\toksB \tabularnewline
\the\toksC \tabularnewline
\the\toksD \tabularnewline
}

\begin{document}

all columns:

\begin{tabular}{llll}
  \leadingcolumns
  \charactercolumn
  \numbercolumn
  \printtable
\end{tabular}

characters only:

\begin{tabular}{lll}
  \leadingcolumns
  \charactercolumn
  \printtable
\end{tabular}

numbers only:

\begin{tabular}{lll}
  \leadingcolumns
  \numbercolumn
  \printtable
\end{tabular}

\end{document}

While this basically does what I want it to do, it only prints out the first line of each table. What am I doing wrong?

  • as a side remark you have many space tokens in your macros and this will show as extra blanks in the tabular; it would be safer to add suitable % to avoid them; you can also prefix all your assignments with \global, for example \toksA=\expandafter{\the\toksA & 1 } becomes \global\toksA\expandafter{\the\toksA & 1 }. The = sign is optional and can be removed. With the \global it works. – user4686 Nov 8 '13 at 23:04
10

You are assigning the registers in the first cell of the table which is a local group. Using registers here doesn't really gain you anything over using macros, but if you want to do it you need to assign them before the table (just as you would if you used macros):

\documentclass{article}

\newtoks\toksA
\newtoks\toksB
\newtoks\toksC
\newtoks\toksD

\newcommand*{\leadingcolumns}{
\toksA={ foo & bar }
\toksB={ Foo & Bar }
\toksC={ fOo & bAr }
\toksD={ foO & baR }
}

\newcommand*{\numbercolumn}{
\toksA=\expandafter{\the\toksA & 1 }
\toksB=\expandafter{\the\toksB & 2 }
\toksC=\expandafter{\the\toksC & 3 }
\toksD=\expandafter{\the\toksD & 4 }
}

\newcommand*{\charactercolumn}{
\toksA=\expandafter{\the\toksA & W }
\toksB=\expandafter{\the\toksB & X }
\toksC=\expandafter{\the\toksC & Y }
\toksD=\expandafter{\the\toksD & Z }
}

\newcommand{\printtable}{
\the\toksA \tabularnewline
\the\toksB \tabularnewline
\the\toksC \tabularnewline
\the\toksD \tabularnewline
}

\begin{document}

all columns:


  \leadingcolumns
  \charactercolumn
  \numbercolumn
\begin{tabular}{llll}
  \printtable
\end{tabular}

characters only:

  \leadingcolumns
  \charactercolumn
\begin{tabular}{lll}
  \printtable
\end{tabular}

numbers only:

  \leadingcolumns
  \numbercolumn
\begin{tabular}{lll}
  \printtable
\end{tabular}

\end{document}
  • @DavudCarlisle: I suggested to build the tabular with toks because of his questions with the colored cells - columns which are excluded already in the building process can't give such problems. Also from his questions on the german mailing list I had the impression that his tabular is much more complicated. (But I did not suggest to use one toks register for each row - I would use a loop and if-then-else switches.) – Ulrike Fischer Nov 8 '13 at 14:44
  • @UlrikeFischer: Please allow me to rephrase that: You mentioned toks, and I since I have never used them before, I tried to use them in an extremely simplified example - and failed. I want to understand why I failed and how to use these registers correctly. Whether I actually use this method is still open for discussion. – vwegert Nov 8 '13 at 16:48

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.