1

How does one automatically add a \bottomrule at the end of a tabularx environment?

\documentclass{scrartcl}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{etoolbox}

\newenvironment{mytabular}[1]{%
  \expanded{\noexpand\begin{tabular}{#1}}%
  \toprule%
}{%
  \bottomrule%
  \end{tabular}%
}%

\newenvironment{mytabularx}[1]{%
  %\apptocmd\tabularx{\toprule}{}{\errmessage{Failed to patch tabularx}}
  % => Package array Error:  Illegal pream-token (\toprule): `c' used.

  \expanded{\noexpand\tabularx{\textwidth}{#1}}%
  \toprule
  % => works
}{%
  \endtabularx%
}%

\newenvironment{mytabularX}[1]{%
  %\pretocmd\TX@endtabularx{\bottomrule}{}{\errmessage{Failed to patch tabularx}}
  % => ./main.tex:59: Misplaced \noalign.
  % => \bottomrule ->\noalign
  % =>                        {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global...
  % => l.59 \begin{mytabularX}{Xcrl}
  %\preto\TX@endtabularx{\bottomrule}%
  % => ./main.tex:59: Misplaced \noalign. (same as above)

  \expanded{\noexpand\tabularx{\textwidth}{#1}}%
}{%
  %\bottomrule
  % => ./main.tex:61: Misplaced \noalign.
  % => \bottomrule ->\noalign
  % =>                        {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global...
  % => l.61 \end{mytabularX} (same error, different line)
  \endtabularx%
}%

\begin{document}

\begin{mytabular}{lcrl}
  this & is & my & table \\
\end{mytabular}

\vspace{2cm}

\begin{mytabularx}{lcrX}
  this & is & my & table \\
\end{mytabularx}

\vspace{2cm}

\begin{mytabularX}{Xcrl}
  this & is & my & table \\
\end{mytabularX}

\end{document}

The most frustrating part about this is that it works for adding at the beginning of the environment.

1 Answer 1

0

You could use \preto\endarray{\bottomrule}, which is a hook from the array package for tables in general. Scope appears to be limited to the current environment.

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.