4

I am evaluating toks registers for various uses - they are still fairly new to me. I'm using them to construct a table column-wise. During this, I have come across an issue I have failed to find a solution for. Consider the following example:

\documentclass{article}
\usepackage{array}
\usepackage{longtable}

\newtoks\toksFormat
\newtoks\toksLine

\toksFormat = {lcr}
\toksLine   = {first & second & third}

\begin{document}

  % this works
  \newcolumntype{X}{clr}
  \begin{longtable}{|X|}
    \hline
    \the\toksLine \tabularnewline
    \hline
  \end{longtable}

  % this doesn't
  \newcolumntype{X}{\the\toksFormat}
  \begin{longtable}{|X|}
    \hline
    \the\toksLine \tabularnewline
    \hline
  \end{longtable}

\end{document}

As the documentation of the package array states, great care is taken not to evaluate the preamble. I suppose that is what's causing the error message in the second table:

! Package array Error:  Illegal pream-token (\the): `c' used.

I'm not sure whether this is an array issue at all, it might just be down to some plain TeX magic I do not (yet) know of. Is it somehow possible to force the expansion of \the\toksFormat inside the call to \newcolumntype?

  • In the future please don't use backticks ` in the title because it will not be rendered by design. – kiss my armpit Nov 9 '13 at 0:06
5
\edef\tmp{\noexpand\newcolumntype{X}{\the\toksFormat}}
\tmp

But there really is no advantage to using the register here.

| improve this answer | |
3

You have to expand, yes. Here are a couple of tricks (I learned the #{ one from Heiko Oberdiek).

\documentclass{article}
\usepackage{array}

\makeatletter
\def\enewcolumntype#1#2#{\@enewcolumntype{#1}{#2}}
\def\@enewcolumntype#1#2#3{%
  \begingroup\edef\x{\endgroup
    \unexpanded{\newcolumntype{#1}#2}{#3}}\x
}
\makeatother

\newtoks\toksFormat
\newtoks\toksLine

\toksFormat = {lcr}
\toksLine   = {first & second & third}

\begin{document}

\enewcolumntype{X}{\the\toksFormat}
\begin{tabular}{|X|}
\hline
\the\toksLine \tabularnewline
\hline
\end{tabular}

\toksFormat = {lp{#1}r}

\enewcolumntype{X}[1]{\the\toksFormat}
\begin{tabular}{|X{5cm}|}
\hline
\the\toksLine \tabularnewline
\hline
\end{tabular}

\end{document}

enter image description here

| improve this answer | |

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.