3

Is it possible to renew the tabularx environment so that it will take the tabularx style parameters? Or the other way around?

\begin{tabularx}{\textwidth}{ ... }
% ...
\end{tabularx}

\begin{tabu} to \linewidth { ... }
% ...
\end{tabu}

something like:

\renewenvironment{tabularx}{\begin{tabu} to #1 ...}{\end{tabu}}

or

\renewenvironment{tabu}{\begin{tabularx}{#1} ...}{\end{tabularx}}

This would help me to find out if the tabularx package can be used for tex4ht and the tabu package for pdflatex. I'm aware that tabu seems to be more powerful, but it appears from a first look that most of the tabu parameters could be definded so they will be ignored when producing html tables.

Koni
  • 226
  • the fact that tabu uses the tex primitive ... to syntax is one of the many deficiencies in the syntax of that package (perhaps the main one that it breaks the > syntax >{a}>{b}c in tabularx (or array etc) has to be coded in teh reverse order as >{b}>{a}c in tabu. If You are just useing X and not tabus extedned X[c,m] columns then just using tabularx would seem to be simpler, and if you are using the extended features just handling to will not help. – David Carlisle Jan 10 '15 at 22:07
  • Anyway I'm curious how to renew an environment that takes parameters, could you please show me how this would be done? – Koni Jan 11 '15 at 13:07
  • 1
    latex has no supported way to define commands or environments taking primitive box attributes such as to so you'd have to drop down to primitive tex commands. – David Carlisle Jan 11 '15 at 13:22
  • Could you please point me to an example to learn this? I just tried:

    \let\tabuo\tabu

    \renewenvironment{tabu}[2]{\begin{tabuo} to #1 {#2}}{\end{tabuo}}

    Which gives me the error: \begin{tabu} on input line ... ended by \end{document}

    – Koni Jan 11 '15 at 13:31
  • I tried http://tex.stackexchange.com/questions/47351/can-i-redefine-a-command-to-contain-itself and http://tex.stackexchange.com/questions/116670/duplicating-environments , however I do not understand why the following works: \let\tabuo\tabu \let\endtabuo\endtabu \renewenvironment{tabu}[2]{\tabuo to #1 {#2}}{\endtabuo} Is \endtabu defined? Is this a naming convention for environments or implied by TeX? – Koni Jan 11 '15 at 13:51

2 Answers2

4

If you want to change \begin{tabu} to <dimen> into \begin{tabularx}{<dimen}, while keeping normal tabu environments you can do

\documentclass{article}
\usepackage{tabularx,tabu}

\makeatletter
\let\originaltabu\tabu
\def\tabu#1#{%
  \setbox\z@=\hbox#1{}%
  \ifdim\wd\z@=\z@
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\originaltabu}%
  {\let\endtabu\endtabularx\tabularx{\wd\z@}}%
}
\makeatother

\begin{document}

\begin{tabu}{|c|c|c|}
aa & bbbb & ccc \\
1 & 0 & 2
\end{tabu}

\bigskip

\begin{tabu} to 8cm{|X|X|X|}
aa & bbbb & ccc \\
1 & 0 & 2
\end{tabu}

\end{document}

I asked the author of tabu to avoid the syntax to <dimen>, he didn't want to accept the advice. However the trick of absorbing all the tokens up to the first brace allows us to use (abuse, perhaps) the fact that \hbox accepts such a syntax.

So the first tabu executes \setbox0=\hbox{} (\z@ is inner for zero as number or length) and the width of the box is zero. Thus the “true” part is followed and \originaltabu is executed.

The second tabu, instead, does \setbox0=\hbox to 8cm{} and the box becomes 8cm wide, so the “false” part is followed, leading to a (local) redefinition of \endtabu to \endtabularx and executing \tabularx{8cm}, after which the column specifiers are absorbed.

I added vertical rules just for better showing the result, not because I recommend their usage (I don't, actually).

enter image description here

egreg
  • 1,121,712
0

I don't understand why but this seems to work:

  \let\tabuo\tabu
  \let\endtabuo\endtabu
  \renewenvironment{tabu}[2]{\tabuo to #1 {#2}}{\endtabuo}

Would it be safer to use \LetLtxMacro instead of \let ?

And how would it work the other way around (i.e. to define a tabu environment that maps to tabularx syntax)?

Edit: Maybe I also solved it the other way around but only for the case when it's separated by one space and the to is really always there.

\def\tabu #1  to  #2#3{\tabularx{#2}{#3}}
\let\endtabu\endtabularx

or

\def\tabu #1#2  #3#4{\tabularx{#3}{#4}}
\let\endtabu\endtabularx

Can someone tell me what is the magic behind the double spaces after \def in TeX please?

Koni
  • 226
  • I think this would be better as an edit to your question or maybe a new question. Your follow-up questions are unlikely to get much attention here. For a start, there is nowhere to answer except in a comment. (Answers are supposed to address the question - not other answers!) – cfr Apr 11 '15 at 23:56