Problem with newenvironment
The trick storing the second parameter as macro and using it in the "end" part of the environment:
\newenvironment{ctable}[2]%
{%
\def\ctablecaption{#2}%
\begin{center}%
\begin{tabular}{#1}%
}%
{%
\end{tabular}%
\captionof{table}{\ctablecaption}%
\end{center}%
}
Using the environ
package you can avoid the standard trick illustrated in Heiko's answer:
\documentclass{article}
\usepackage{capt-of}
\usepackage{environ}
\NewEnviron{ctable}[2]%
{%
\begin{center}%
\begin{tabular}{#1}%
\BODY
\end{tabular}%
\captionof{table}{#2}%
\end{center}%
}
\begin{document}
\begin{ctable}{cc}{test}
test & test
\end{ctable}
\end{document}
You're trying to avoid floating tables, which is quite dangerous. Moreover you lose flexibility. However, if you really want to shoot yourself in the foot ;-)
, here's a possibility: the package xparse
.
\documentclass{article}
\usepackage{xparse,capt-of}
\NewDocumentEnvironment{ctable}{mm} % two arguments in braces
{% begin part
\begin{center}
\begin{tabular}{#1}
}
{% end part
\end{tabular}
\captionof{table}{#2}
\end{center}
}
\begin{document}
\begin{ctable}{ll}{A meaningful caption\label{tab:foo}}
A & B \\
C & D
\end{ctable}
\end{document}