# How to include tabularx/y in a new environment?

I am looking for a way to define a customizable environment for tables in my latex documents. This means:

• caption and label
• width of table
• customized column format (tabular preamble)

While this seems to be easy for figures, I am desparately looking for a solution for tables. Here is my minimal code example:

\documentclass[a4paper,fontsize=14pt,DIV=calc]{scrbook}
\usepackage{tabularx,tabulary}

\newenvironment{tabfig}[4]
{\begin{table}
\captionabove{#2}\label{tab:#1}
\begin{tabularx}{#3}{#4}}
{\end{tabularx}\end{table}}

\begin{document}
\begin{tabfig}{MyLabel}{MyCaption}{0.8\textwidth}{l >{\centering}X >{\centering}X}
A & B & C \tabularnewline
x & y & z \tabularnewline
\end{tabfig}
\end{document}


I have done thorough research for several days by now and also tried \bgroup, \begingroup, \begintabularx, the cprotect package and various other things. Though, I could not come up with something useful in this general case. Folks, I cannot believe I am the first to write a customizable table environment. Is there anybody to light the light?

-
The problem here is that tabularx is not actually an environment, it only looks like that. It is a command which reads the whole command in one go, which doesn't work if you wrap it in another environment. (But the folk at tex.stackexchange certainly have some solution.) –  Paŭlo Ebermann Aug 15 '11 at 19:29

## migrated from stackoverflow.comAug 15 '11 at 19:37

This question came from our site for professional and enthusiast programmers.

Try this:

\documentclass[a4paper,fontsize=14pt,DIV=calc]{scrbook}
\usepackage{tabularx,tabulary}

\newenvironment{tabfig}[4]{%
\table\centering%
\captionabove{#2}\label{tab:#1}%
\tabularx{#3}{#4}%
}{%
\endtabularx%
\endtable%
}%

\begin{document}
\begin{table}
\captionabove{MyCaption}\label{tab:MyLabel}
\begin{tabularx}{0.8\textwidth}{l >{\centering}X >{\centering}X}
A & B & C \tabularnewline
x & y & z \tabularnewline
\end{tabularx}\end{table}

\begin{tabfig}{MyLabel}{MyCaption}{0.8\textwidth}{l >{\centering}X >{\centering}X}
A & B & C \tabularnewline
x & y & z \tabularnewline
\end{tabfig}
\end{document}

-
Nice way to environment-ize the "macro". –  Werner Aug 15 '11 at 19:59
@Werner: it's in the documentation for tabularx. –  egreg Aug 15 '11 at 20:41
@Werner Nice hint, \table helped a lot! But I cannot add anything in the end part of the environment definition (e.g. \bottomrule from booktabs). Second, is there a way to center the whole table horizontally on the page? \begin{center} does not work anymore... @egreg \endtabularx is in the documentation (hidden in the macro part), \table is not. –  Rene Aug 15 '11 at 21:21
@Rene: Updated to center horizontally. See this link about \bottomrule in a self made environment –  Peter Grill Aug 16 '11 at 3:38
@Peter: You solved a question that cost me a lot of time, indeed! Thanks for your help! –  Rene Aug 16 '11 at 17:09

Since the solution is not completely trivial, I would like to summarize the answers to the question in a complete code example to use in your documents:

\documentclass[a4paper,fontsize=14pt,DIV=calc]{scrbook}
\usepackage{tabularx,tabulary,booktabs,environ}

\newlength{\tabmargin}% necessary if you want the caption to be aligned with the table
\newlength{\tabwidth}%

\NewEnviron{tabfigx}[4]{%
\setlength{\tabwidth}{#3}\setlength{\tabmargin}{\linewidth}%
\table[!htb]%
\captionabove{#2}\label{tab:#1}%
\centering%
\tabularx{#3}{#4}%
\toprule%
\BODY%
\bottomrule% or what else you would like to do after the table has been done...
}[\endtabularx\endtable]%

\begin{document}
\begin{tabfigx}{MyLabel}{MyCaption}{0.8\textwidth}{l >{\centering}X >{\centering}X}
A & B & C \tabularnewline
x & y & z \tabularnewline
\end{tabfigx}
\end{document}


If you want to use the tabulary package, just define a similar macro tabfigy and change the lines containing \tabularx and \endtabularx accordingly. An alternative way is to use the floatrow package. Personally, I will stay with KOMA script plus some extending macros.

-