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.

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?

share|improve this question
    
In the future please don't use backticks ` in the title because it will not be rendered by design. –  Please don't touch Nov 9 '13 at 0:06

2 Answers 2

up vote 4 down vote accepted
\edef\tmp{\noexpand\newcolumntype{X}{\the\toksFormat}}
\tmp

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

share|improve this answer

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

share|improve this answer

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.