latexlongtable

Environment inside a longtable with LaTeX


I would like to create a new environment to print a header and a footer between sections of a table.

I did this:

\documentclass{article}

\usepackage{longtable}

\newenvironment{env}{Heading&&& \\}{\hline \\}

\begin{document}

\begin{longtable}{p{7cm}lrr}
    \begin{env}
        Content&b&c&d
    \end{env}
\end{longtable}

\end{document}

but I get insulted by the compiler. See here for the complete output.

Does someone see the problem?


Solution

  • There are two problems here. First, you need an \\ at the end of the "Content&b&c&d" line. Second, environments don't work inside tabular/longtable — that's where most of your error messages are coming from. It may be possible to diddle them into working, but it's way beyond my TeX-fu. This is the best I can come up with:

    \documentclass{article}
    \usepackage{longtable}
    \newcommand{\startenv}{Heading\tabularnewline}
    \newcommand{\stopenv}{\hline\tabularnewline}
    \begin{document}
    
    \begin{longtable}{p{7cm}lrr}
    \startenv
        Content&b&c&d \\
    \stopenv
    \end{longtable}
    

    (It is not strictly necessary to use \tabularnewline instead of \\, but it will avoid headaches if you ever mix this with other environments that use \\ for their own purposes.)