4

I wanted to use a tabularx (ltablex package) inside an environment, but there is an error (but it still builds okay).

I'm using ltablex, because I want a longtable spanning several pages with the behaviour of tabularx (having one or more columns auto-size via "X").

The example is a stripped down minimal version to show the error (for this simple example I wouldn't bother to create a new environment, the real thing has more formatting).

\documentclass[a4paper]{scrartcl}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{ltablex} % includes tabularx, but combined with longtables (spanning multiple pages)

\newenvironment{deftable}{
   \tabularx{\textwidth}{ |p{5cm}|X| }
}{
   \endtabularx
}

\begin{document}

\begin{deftable}
  cell & anothercell \\ \hline
  more cells & Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren \\
\end{deftable}

\end{document}

The error I'm getting is:

! LaTeX Error: \begin{deftable} on input line 13 ended by \end{tabularx}.

I think the tabularx processor does not consume the \end{tabularx} that is put on the stack by \endtabularx ... but seriously, I know LaTeX not good enough to figure out some solution.

  • I get no error if I replace \end{tabularx} in the definition of \TX@endtabularx with \endtabularx\endgroup, but I'm not sure if this really solves the problem. – egreg Aug 19 '14 at 10:47
  • thx for cleaning my question, I'll remember this formatting for future questions. :) @cgnieder – Faldrian Aug 19 '14 at 14:23
  • @Faldrian no worries: you'll get used to this :) we all did – clemens Aug 19 '14 at 14:28
3

I get no error if I replace \end{tabularx} (which is probably wrong in the first place) with \endtabularx\endgroup. No guarantee this won't break anything else.

\documentclass[a4paper]{scrartcl}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{ltablex} % includes tabularx, but combined with longtables (spanning multiple pages)
\usepackage{etoolbox}
\makeatletter
% get rid of \par (there are two empty lines in the definition by ltablex)
\patchcmd{\TX@endtabularx}{\par}{}{}{}
\patchcmd{\TX@endtabularx}{\par}{}{}{}

\patchcmd{\TX@endtabularx}
  {\end{tabularx}}
  {\endtabularx\endgroup}
  {}{}
\makeatother

\newenvironment{deftable}{%
   \tabularx{\textwidth}{ |p{5cm}|X| }%
}{%
   \endtabularx
}
  • Thank you very much! The last patchcmd fixes it, probably by using endgroup to tell the parser this tabularx is finished now. :) – Faldrian Aug 19 '14 at 14:20
4

Here is a try with environ package. But I am not sure if it works for your complex cases. It is worth giving a try. The following code works.

\documentclass[a4paper]{scrartcl}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{ltablex} % includes tabularx, but combined with longtables (spanning multiple pages)
\usepackage{environ}

\NewEnviron{deftable}{%
   \begin{tabularx}{\textwidth}{ |p{5cm}|X| }%
    \BODY
   \end{tabularx}
}

\begin{document}

\begin{deftable}
  cell & anothercell \\ \hline
  more cells & Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren \\
\end{deftable}

\end{document}

enter image description here

  • Nice, the environ package produces very readable code. :) – Faldrian Aug 20 '14 at 15:44

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.