# newcolumntype leads to error with afterpage

when I'm defining a new columntype in an afterpage environment, LaTeX fail with an error:

Latex Error: ./chapters/chapter3.tex:149 Illegal parameter number in definition of \AP@.


When removing the nowcolumntype declaration and replacing "C" with "p" it works, so it seems to be a problem with afterpage.

I'm using the pdflscape package

\afterpage{

\begin{landscape}
\begin{table}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\begin{center}
\begin{tabular}{|C{2.0cm}|C{2.0cm}}
\hline
1  1    &   1  2\\
2  1    &   2  2\\
\hline
\end{tabular}
\end{center}
\end{table}

\end{landscape}
}

-

As a general principle, \newcolumntype declarations shouldn't be used outside a document's preamble. (The fact that using them outside the preamble doesn't result in a crash when not used in conjunction with the afterpage environment doesn't contradict this point...)

The following MWE, which is based on your code, compiles without errors. Note that I've replaced the \begin{center} ... \end{center} instructions with a simple \centering instruction and have loaded the rotating package to make use of its sidewaystable environment.

\documentclass{article}
\usepackage{afterpage,array,rotating}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\afterpage{%
\begin{sidewaystable}
\centering
\begin{tabular}{|C{2.0cm}|C{2.0cm}|}
\hline
1  1    &   1  2\\
2  1    &   2  2\\
\hline
\end{tabular}
\end{sidewaystable}
}
\end{document}

-
There's usually no problem in locally setting a \newcolumntype in a table environment. In this case I suspect a timing problem due to the fact that \afterpage plays with the output routine. – egreg May 29 '12 at 12:20
@egreg: I would still maintain that \newcolumntype should only be executed in the preamble. :-) However, I'll soften the phrasing in the answer to be less apodictic about this issue. – Mico May 29 '12 at 12:25

Afterpage tokens are put through a \def (of a temporary macro who's name is shown in the error message) So as usual with a def you have to double the #:

\documentclass{article}
\usepackage{afterpage,array,rotating}

\begin{document}
\afterpage{%
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{##1}}
\begin{sidewaystable}
\centering
\begin{tabular}{|C{2.0cm}|C{2.0cm}|}
\hline
1  1    &   1  2\\
2  1    &   2  2\\
\hline
\end{tabular}
\end{sidewaystable}
}
\end{document}


It would have been possible to define afterpage in a way that avoided the need to double the # (by cycling the tokens through a token register) but I didn't think anyone would ever use the package let alone need macro parameter tokens within its argument:-)

-