14

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?

lockstep
  • 250,273
Rene
  • 183
  • 2
    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

2 Answers2

8

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}
Peter Grill
  • 223,288
4

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}%
 \addtolength{\tabmargin}{-\the\tabwidth}\setcapmargin{0.5\tabmargin}%
 \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.

Rene
  • 183