Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a selfdefined table-environment with the caption at the end of the table with LaTeX?

I have a custom table-environment defined with \newenvironment. I have a caption in this environment, but I want to have it at the end.

My environment looks (a little simplified) like this:

\newenvironment{mytable}[2]{\begin{table}[hbtp]\caption{#1}\label{#1}\begin{center}\begin{tabular}{#2}}{\end{tabular}\end{center}\end{table}}

I want to put the caption at the end, like this:

\newenvironment{mytable}[2]{\begin{table}[hbtp]\label{#1}\begin{center}\begin{tabular}{#2}}{\caption{#1}\end{tabular}\end{center}\end{table}}

But that doesn't work, because I cannot use the parameters in the end of the environment. How can I solve this problem?

like image 266
Mnementh Avatar asked Sep 07 '09 16:09

Mnementh


1 Answers

You'll want to store the caption and label parameters and use them later. (Also, the \label should appear after the \caption.)

Something like this should work:

\newcommand{\templabel}{}% stores the label
\newcommand{\tempcaption}{}% stores the caption

\newenvironment{mytable}[3]{%
  \gdef\templabel{#1}% store the label so we can use it later
  \gdef\tempcaption{#2}% store the caption so we can use it later
  \begin{table}[hbtp]% 
    \begin{center}%
      \begin{tabular}{#3}%
}{%
        \caption{\tempcaption}% use the stored caption
        \label{\templabel}% use the stored label (*after* the caption)
      \end{tabular}%
    \end{center}%
  \end{table}%
}

Use the environment like this:

\begin{mytable}{tab:example}{This is the caption for my example table.}{cc}
  Row 1 & First \\
  Row 2 & Second \\
  Row 3 & Third \\
\end{mytable}

I haven't tested this code.

like image 85
godbyk Avatar answered Oct 25 '22 03:10

godbyk