5

I defined a new environment for some custom tables (to not have to repeat the header and column definitions etc. again and again).

\newenvironment{mytable}{
  \noindent\tabularx{\textwidth}{|c|c|X|} 
    \hline 
    \bf Col 1 & \bf Col 2 & \bf Col 3 \\ 
    \hline
}{%
  \endtabularx
}

It's based on tabularx (for the variable size X column) and should be able to span multiple pages. There's the package ltablex which can do this for a tabularx environment. (See the tabularx in my example below, you can comment out the mytable).

However, I am receiving the error:

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

I assume this comes from the redefinition of \TX@endtabularx in ltablex.sty where it has the following code snippet:

  \ifnum0=`{\fi}%
   \end{tabularx}

Is it possible to use a "longtable tabularx" as a new environment?

Example to try out:

\documentclass{article}
% filling table with for, relentlessy copied from 
% http://tex.stackexchange.com/a/183756/51583
% extremely shortened, see address for more explanations
\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}\def\tabledata{}
\newcount\n\n=1\loop\advance\n by1\addto\tabledata{Lorem&Ipsum&XCell\\}
  \ifnum\n<100\repeat

% package and new environment
\usepackage{ltablex}
\newenvironment{mytable}{%
  \noindent\tabularx{\textwidth}{|c|c|X|} 
    \hline 
    \bf Col 1 & \bf Col 2 & \bf Col 3 \\ 
    \hline
}{%
  \endtabularx
}

\begin{document}

% working tabularx
\begin{tabularx}{\textwidth}{|c|c|X|}
  \tabledata
\end{tabularx}

% not working mytable
\begin{mytable}
  \tabledata
  \hline
\end{mytable}

\end{document}
2

It is to do with the way tabularx deals with its argument. The package environ provides a work around for such situations. Instead of \newenvironment you say

\NewEnviron{myenv}{start code
   \BODY
   end code}

Then

\begin{myenv}
  mybody
\end{myenv}

will run the contents of myenv with \BODY replaced by mybody. In your case the relevant definition is

\usepackage{environ}
\NewEnviron{mytable}{%
  \noindent\begin{tabularx}{\textwidth}{|c|c|X|} 
    \hline 
    \bf Col 1 & \bf Col 2 & \bf Col 3 \\ 
    \hline
  \BODY
\end{tabularx}
}

And a complete file is

\documentclass{article}
% filling table with for, relentlessy copied from 
% http://tex.stackexchange.com/a/183756/51583
% extremely shortened, see address for more explanations
\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}\def\tabledata{}
\newcount\n\n=1\loop\advance\n by1\addto\tabledata{Lorem&Ipsum&XCell\\}
  \ifnum\n<100\repeat

% package and new environment
\usepackage{ltablex}
\usepackage{environ}
\NewEnviron{mytable}{%
  \noindent\begin{tabularx}{\textwidth}{|c|c|X|} 
    \hline 
    \bf Col 1 & \bf Col 2 & \bf Col 3 \\ 
    \hline
  \BODY
\end{tabularx}
}

\begin{document}

% working tabularx
\begin{tabularx}{\textwidth}{|c|c|X|}
  \tabledata
\end{tabularx}

% working mytable
\begin{mytable}
  \tabledata
  \hline
\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.