Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Aug 15 '11 at 19:37

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

1  
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
add comment

2 Answers

up vote 4 down vote accepted

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}
share|improve this answer
2  
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
1  
@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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.