2

I am trying to get ltablex to work using the following code based on this answer:

\documentclass{article}
\usepackage{ltablex}
\usepackage{blindtext}
\keepXColumns

\newenvironment{test}
    {%
     \tabularx{\textwidth}{lX}
    }
    {%
     \endtabularx
    }

\begin{document}

\begin{test}
    row 1 & \blindtext[2] \\
    row 2 & \blindtext \\
    row 3 & \blindtext[2] \\
\end{test}

\end{document}

Unfortunately the following error is thrown:

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

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

l.20 \end{test}

How can I fix this to be able to get a table spanning multiple pages and the X column type?

6

No idea why the test does not work (probably need @DavidCarlisle to explain it). Here is a working alternative using environ

\documentclass{article}
\usepackage{ltablex}
\usepackage{blindtext,environ}
\keepXColumns

% \newenvironment{test}
% {%
%   \begin{tabularx}{\textwidth}{lX}
%     }
%     {%
%   \end{tabularx}
% }


\NewEnviron{test}{
  \begin{tabularx}{\textwidth}{lX}
    \BODY
  \end{tabularx}
}



\begin{document}

\begin{test}
    row 1 & \blindtext[2] \\
    row 2 & \blindtext \\
    row 3 & \blindtext[2] \\
\end{test}

 \end{document}
0

You can try to reinsert the code that tabularx uses to test for such internal \endtabularx. It works for your example but I didn't try to really understand the code and so neither know if both patches are necessary (it works also only with the second) and if it breaks something else.

\documentclass{article}
\usepackage{ltablex}
\usepackage{blindtext}
\keepXColumns
\usepackage{etoolbox}
\makeatletter
\pretocmd\TX@endtabularx{%
     \expandafter\expandafter\expandafter
     \TX@find@endtabularxa\csname end\TX@\endcsname
     \endtabularx\TX@\endtabularx\TX@find@endtabularxa
     }{}{\fail}

\patchcmd\TX@endtabularx{\end{tabularx}}{%
   \expandafter\expandafter\expandafter
   \TX@find@endtabularxbb
    \expandafter\end\expandafter{\TX@}%
    \endtabularx\TX@\endtabularx\TX@find@endtabularxb
    }{}{\fail}

\makeatother     


\newenvironment{test}
    {%
     \tabularx{\textwidth}{lX}
    }
    {%
     \endtabularx
    }

\begin{document}

\begin{test}
    row 1 & \blindtext[2] \\
    row 2 & \blindtext \\
    row 3 & \blindtext[2] \\
\end{test}

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