5

Possible Duplicate:
bottomrule not working in a self-made environment

I have this environment for consistent tables in my latex documents:

\newenvironment{defaultTable}[2] {
    \@float{table}[h]
    \noindent
    \tabularx{\textwidth}{#1}
    \specialrule{0.5pt}{10pt}{0pt}
    \rowcolor[gray]{.9}
    \gdef\mycaption{#2}
} {
    \bottomrule
    \endtabularx
    \emph{\caption{\mycaption}}
    \end@float
}

Unfortunately, the line with \bottomrule leads to this error, which i don't understand:

! Misplaced \noalign. \bottomrule ->\noalign {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global... l.27 \end{defaultTable}

When I use it inside the environment it works though

\begin{defaultTable}{X X}{Blablah Table}
(..)
\bottomrule
\end{defaultTable}

What am I doing wrong?

Schtibe
  • 215
  • I assume the case where it works has {defaultTable} defined not to have \bottomrule, but otherwise be the same. – Charles Stewart Dec 07 '10 at 14:04
  • 1
    It would have been helpful to have posted a complete working example, including the packages you were assuming to have this code work. – Alan Munn Apr 04 '11 at 20:41

1 Answers1

3

The problem is caused by the \noalign in the definition of \bottomrule. You can define a new version of \bottomrule for use within the environment and then things should work. (You shouldn't redefine \bottomrule in case you use it outside of the environment.) The definition is taken from the booktabs source.

\documentclass{article}
\usepackage{tabularx,booktabs,colortbl}
\makeatletter
\def\envbottomrule{{\ifnum0=`}\fi
  \@aboverulesep=\aboverulesep
  \global\@belowrulesep=\belowbottomsep
  \global\@thisruleclass=\@ne
  \@ifnextchar[{\@BTrule}{\@BTrule[\heavyrulewidth]}}
\newenvironment{defaultTable}[2] {
    \@float{table}[h]
    \noindent
    \tabularx{\textwidth}{#1}
    \specialrule{0.5pt}{10pt}{0pt}
    \rowcolor[gray]{.9}
    \gdef\mycaption{#2}
} {%
    \envbottomrule
    \endtabularx
    \emph{\caption{\mycaption}}
    \end@float
}

\makeatother
\begin{document}
\begin{defaultTable}{XX}{A caption}%
foo & bar\\
foo & bar
\end{defaultTable}

\end{document}
David Carlisle
  • 757,742
Alan Munn
  • 218,180