Generaltabularx inside a newenvironment with hline

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
jekylls
Posts: 2
Joined: Sun Jul 05, 2009 12:20 am

tabularx inside a newenvironment with hline

Post by jekylls »

I'd like to make a custom environment that will replicate the following code without the \hline and the tabularx:

Code: Select all

\begin{tabularx}{|rX|} 
  \hline
   stuff...\\ \hline
\end{tabularx}
Suppose the environment is called testbox. Then the code above could be written as:

Code: Select all

\begin{testbox}
  stuff...
\end{testbox}
This is what I've come up with for the newenvironment code:

Code: Select all

\newenvironment{testbox}[1][rX]{%
   \tabularx{\columnwidth}{|#1|} \hline}{ \hline \endtabularx }  
However, the \hline in the end declaration causes a "misplaced noalign" error. If the \hline is removed the end definition, the code compiles with no errors (except no bottom line).

Here is the smallest code that I can give that causes the error:

Code: Select all

\documentclass{article}
\usepackage{tabularx}

\newenvironment{testbox}[1][rX]{%
  \tabularx{\columnwidth}{|#1|} \hline}{ \hline \endtabularx }  

\begin{document}
\begin{testbox}
  3 & c \\
  4 & c \\
\end{testbox}
\end{document}
Why does that \hline cause a misplaced noalign error? How can I fix it?

-jekyll

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

tabularx inside a newenvironment with hline

Post by Juanjo »

jekylls wrote: Why does that \hline cause a misplaced noalign error?
It is explained in the first paragraphs of section 5 in the manual of tabularx. The \end part of any environment based on tabularx should begin with \endtabularx.
jekylls wrote: Why does that \hline cause a misplaced noalign error? How can I fix it?
It suffices to redefine the internal command \TX@find@end in order to add the stuff that comes at the end of the table. Look at the following code, which is self-explanatory:

Code: Select all

\documentclass{article}
\usepackage{tabularx}

\newenvironment{testbox}[1][rX]{%
  \tabularx{\columnwidth}{|#1|}\hline}{\endtabularx}
  
\makeatletter
\def\TX@find@end#1{%
   \def\@tempa{#1}%
   \ifx\@tempa\TX@%
      \toks@\expandafter{\the\toks@\AddBeforeEndtabularx}%
      \expandafter\TX@endtabularx
   \else\toks@\expandafter
      {\the\toks@\end{#1}}\expandafter\TX@get@body\fi}
\makeatother

\def\AddBeforeEndtabularx{\\ \hline}

\begin{document}
\parindent=0pt

\begin{testbox}
  3 & c \\
  4 & c
\end{testbox}

\bigskip

\begin{testbox}[Xc]
  3 & c \\
  4 & c
\end{testbox}

\bigskip

\def\AddBeforeEndtabularx{\\ \hline\hline\multicolumn{3}{|c|}{Bottom}\\ 
   \hline\hline}

\begin{testbox}[XlX]
  3 & c & Bla \\
  4 & c & Bla
\end{testbox}

\bigskip

\begin{testbox}[lXr]
  3 & c & Bla \\
  4 & c & Bla
\end{testbox}

\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
jekylls
Posts: 2
Joined: Sun Jul 05, 2009 12:20 am

tabularx inside a newenvironment with hline

Post by jekylls »

Thank you very much for the help. I now have the environment that I wanted/needed. Could you help me understand the "self-explanatory" code that I left down below? I don't understand it, and I would very much like to. I don't seem to know how to program in Latex as good as I thought I could. Is there a advanced guide to latex programming?

-jekylls
Juanjo wrote: It suffices to redefine the internal command \TX@find@end in order to add the stuff that comes at the end of the table. Look at the following code, which is self-explanatory:

Code: Select all

\makeatletter
\def\TX@find@end#1{%
   \def\@tempa{#1}%
   \ifx\@tempa\TX@%
      \toks@\expandafter{\the\toks@\AddBeforeEndtabularx}%
      \expandafter\TX@endtabularx
   \else\toks@\expandafter
      {\the\toks@\end{#1}}\expandafter\TX@get@body\fi}
\makeatother

\def\AddBeforeEndtabularx{\\ \hline}
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

tabularx inside a newenvironment with hline

Post by Juanjo »

Well, when I said that the code was self-explanatory, I wanted to express that, by loooking at the examples, it was easy to see how to define and use your environment and to understand the effect of the new macro \AddBeforeEndtabularx. Of course, the piece of code you quoted is not obvious, even for me. To fully understand it, it is necessary to know some low level TeX commands and concepts, such as token lists. See these threads (this one and this other), for example, for useful references and advice.

Anyway, the macro \TX@find@end is defined in tabularx. Focusing on your environment, LaTeX starts scanning the contents of testbox and collects everything it finds into a token list (a "stream" of macros and characters) that, by now, remains unprocessed. Every time it finds an \end command, LaTeX stops scanning to check if its argument is "testbox". This check is done precisely through the following lines in the definition of \TX@find@end:

Code: Select all

\def\@tempa{#1}%
   \ifx\@tempa\TX@%
since \@tempa is the argument of the \end that has been found and \TX@ is a macro previously defined as "testbox". If true, LaTeX adds to the token list the stuff in \AddBeforeEndtabularx (this is my modification of \TX@find@end)

Code: Select all

\toks@\expandafter{\the\toks@\AddBeforeEndtabularx}%
and starts processing the token list (so really beginning to write the table):

Code: Select all

\expandafter\TX@endtabularx
If false, LaTeX has found the end of an environment embedded in the table (such as a minipage or an itemize environments). In this case, LaTeX adds the command "\end{found environment}" to the token list

Code: Select all

\toks@\expandafter {\the\toks@\end{#1}}
and continues scanning the contents of testbox

Code: Select all

\expandafter\TX@get@body
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
jaykemper
Posts: 22
Joined: Wed Apr 15, 2009 12:11 am

Re: tabularx inside a newenvironment with hline

Post by jaykemper »

So it sounds like the only command that can be in the newenvironment command is "\endtabularx"

Is there (or how different from AddBeforeEndtabularx) a AddAfterEndxtabularx function?
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

tabularx inside a newenvironment with hline

Post by Juanjo »

jaykemper wrote:So it sounds like the only command that can be in the newenvironment command is "\endtabularx"
No. You can add commands after \endtabularx
jaykemper wrote: Is there (or how different from AddBeforeEndtabularx) a AddAfterEndxtabularx function?
It would be convenient that you explain concisely the problem you are facing and what you expect to obtain, adding some compilable code, if possible.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
zer0nimo
Posts: 12
Joined: Wed Jul 29, 2009 3:20 am

tabularx inside a newenvironment with hline

Post by zer0nimo »

HI!

Perhaps somebody can help me with a similar problem.

I encountered exactly the same problems and found the fix with "\tabularx" and "\endtabularx".

But there is another error-msg:

Code: Select all

"\begin{textbox}" on inputline ... ended by "\end{tabularx}"
Does anybody now why this error occures?!?

I used exactly the example from above. My version of tabularx is "[1999/01/07 v2.07 `tabularx' package (DPC)]". Additionally I use the packages "\RequirePackage{ltablex,ragged2e}".

Thanks for any help!

Regards, Erik
zer0nimo
Posts: 12
Joined: Wed Jul 29, 2009 3:20 am

Re: tabularx inside a newenvironment with hline

Post by zer0nimo »

Sorry ... but by this post I realized, that exactly the additonally used package "ltablex" may be the problem AND IT IS!

Does somebody know why? May it use an "old" version of tabularx not including the bugfix?

And does somebody know how to handle the probelm? (Updating ltablex, using a different package for tables longer than one page or sth. else?)

Thank you,
Erik
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

tabularx inside a newenvironment with hline

Post by Juanjo »

It seems that ltablex.sty has not been updated for a long time. There is a line almost at the end of the file that causes the problem. This line is inserted in a long macro definition. So, I think it is simpler to make a copy of the package and modify it. I've done so. Download the attached file ltablexmod.sty and use it instead of ltablex.

Since ltablex redefines the tabularx environment to internally construct tables with longtable, one can use features of this latter package to add things at the end of the table. Hence, the example given in a previous post could be rewritten as follows:

Code: Select all

\documentclass{article}
\usepackage{ltablexmod}

\newenvironment{testbox}[1][rX]{%
  \tabularx{\columnwidth}{|#1|}\AddBeforeEndtabularx\endfoot\hline}{\endtabularx}

\begin{document}
\parindent=0pt

\keepXColumns

\def\AddBeforeEndtabularx{\hline}
\begin{testbox}
  3 & c \\
  4 & c
\end{testbox}

\bigskip

\begin{testbox}[Xc]
  3 & c \\
  4 & c
\end{testbox}

\bigskip

\def\AddBeforeEndtabularx{\hline\hline\multicolumn{3}{|c|}{Bottom}\\ 
   \hline\hline}

\begin{testbox}[XlX]
  3 & c & Bla \\
  4 & c & Bla
\end{testbox}

\bigskip

\begin{testbox}[lXr]
  3 & c & Bla \\
  4 & c & Bla
\end{testbox}

\end{document}
A final comment. Perhaps you are not aware that ltablex defines two commands, \keepXColumns and \convertXColumns, to control whether or not X columns behave as in the original tabularx package or they are converted to l columns when their contents are short (this is the default). To see the difference, just comment out \keepXColumns in the above code. Also remember that you need to compile several times until the tables have the right width.
Attachments
ltablexmod.sty
(6.43 KiB) Downloaded 569 times
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
zer0nimo
Posts: 12
Joined: Wed Jul 29, 2009 3:20 am

Re: tabularx inside a newenvironment with hline

Post by zer0nimo »

GREAT! :o

This is exactly what I need.

And you were right, I wasn't aware off the additional commands. This is very helpful, too.

Thank you very much!!!
Post Reply