简体   繁体   中英

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?

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM