4

I want to define a wrapper environment for a table where one of the columns stretches to fill the available width. I'm using the tabularx package and its X column for that. As mentioned here and in the tabularx documentation, I need to use \tabularx\endtabularx rather than \begin{tabularx}\end{tabularx} in the definition of my environment, because \tabularx runs to the end of the enclosing environment.

So far so good, now I'd like to add horizontal rules at the top and bottom. When I comment out the bottom \hline in the environment definition, I get an error

! Misplaced \noalign.
\hline ->\noalign 
                  {\ifnum 0=`}\fi \hrule \@height \arrayrulewidth \futurelet...
l.28 \end{mytable}

MWE (with the desired but problematic \hline commented out):

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\newenvironment{mytable}{%
  \tabularx{\textwidth}{lX}
    \hline%
    Name & Description \\
    \hline%
}{%
  %\hline%
  \endtabularx%
}

\begin{document}
Manually (working fine): \\
\begin{tabularx}{\textwidth}{lX}
  \hline
  Name & Description \\
  \hline
  foo & This description is long. It is very long. It is more than one line long. It is automatically broken up into lines. \verb`\verb` works. \\
  \hline
\end{tabularx}

\medskip
Wrapper environment (error with the bottom \verb`\hline`): \\
\begin{mytable}
  foo & This description is long. It is very long. It is more than one line long. It is automatically broken up into lines. \verb`\verb` works. \\
\end{mytable}
\end{document}

How can I make my environment automatically include a horizontal rule at the bottom of the table?

I don't mind switching from tabularx to something else. I'll want to use the environment both inline and in a float. It would be very annoying if \verb didn't work. At the moment, it's fine if page breaks inside the table are impossible but I'm likely to have to tackle at some point that as my tables get longer.

Bonus question: why is there so little space between each horizontal line and the text below, and how do I make the space decent — \strut

  • 2
    for the bonus question \setlength\extrarowheight{2pt} – David Carlisle Nov 26 '13 at 17:35
6

As it says in the tabularx doc, the principal restriction on the \tabularx \endtabularx form is that \endtabularx is the first token in the end code. So the behaviour you see is documented feature, not a bug.

However you could do:

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\makeatletter

\newenvironment{mytable}{%
  \let\savedtx\TX@endtabularx
  \def\TX@endtabularx{\toks@\expandafter{\the\toks@\\\hline}\savedtx}%
  \tabularx{\textwidth}{lX}
    \hline%
    Name & Description \\
    \hline%
}{%
  %\hline%
  \endtabularx%
}
\makeatother

\begin{document}
Manually (working fine): \\
\begin{tabularx}{\textwidth}{lX}
  \hline
  Name & Description \\
  \hline
  foo & This description is long. It is very long. It is more than one line long. It is automatically broken up into lines. \verb`\verb` works. \\
  \hline
\end{tabularx}

\medskip
Wrapper environment (error with the bottom \verb`\hline`): \\
\begin{mytable}
  foo & This description is long. It is very long. It is more than one line long. It is automatically broken up into lines. \verb`\verb` works.
\end{mytable}
\end{document}

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.