如何创建一个自定义的表环境,并使用LaTeX在表的末尾添加标题?

如何创建一个自定义的表环境,并使用LaTeX在表的末尾添加标题?,latex,tex,latex-environment,newenvironment,Latex,Tex,Latex Environment,Newenvironment,我有一个用\newenvironment定义的自定义表环境。我在这个环境中有一个标题,但我想在最后有它 我的环境看起来(有点简化)如下: \newenvironment{mytable}[2]{\begin{table}[hbtp]\caption{#1}\label{#1}\begin{center}\begin{tabular}{#2}}{\end{tabular}\end{center}\end{table}} \newenvironment{mytable}[2]{\begin{tab

我有一个用\newenvironment定义的自定义表环境。我在这个环境中有一个标题,但我想在最后有它

我的环境看起来(有点简化)如下:

\newenvironment{mytable}[2]{\begin{table}[hbtp]\caption{#1}\label{#1}\begin{center}\begin{tabular}{#2}}{\end{tabular}\end{center}\end{table}}
\newenvironment{mytable}[2]{\begin{table}[hbtp]\label{#1}\begin{center}\begin{tabular}{#2}}{\caption{#1}\end{tabular}\end{center}\end{table}}
\begin{mytable}{tab:example}{This is the caption for my example table.}{cc}
  Row 1 & First \\
  Row 2 & Second \\
  Row 3 & Third \\
\end{mytable}
我想把标题放在最后,像这样:

\newenvironment{mytable}[2]{\begin{table}[hbtp]\caption{#1}\label{#1}\begin{center}\begin{tabular}{#2}}{\end{tabular}\end{center}\end{table}}
\newenvironment{mytable}[2]{\begin{table}[hbtp]\label{#1}\begin{center}\begin{tabular}{#2}}{\caption{#1}\end{tabular}\end{center}\end{table}}
\begin{mytable}{tab:example}{This is the caption for my example table.}{cc}
  Row 1 & First \\
  Row 2 & Second \\
  Row 3 & Third \\
\end{mytable}

但这不起作用,因为我不能在环境的末尾使用参数。如何解决此问题?

使用剪切粘贴而不是新环境?我确信这是新的。不打算以这种方式使用。这有什么意义?要避免每次都键入所有内容?

您需要存储标题和标签参数,并在以后使用它们。(此外,\标签应出现在\标题之后。)

像这样的方法应该会奏效:

\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}%
}
使用如下环境:

\newenvironment{mytable}[2]{\begin{table}[hbtp]\caption{#1}\label{#1}\begin{center}\begin{tabular}{#2}}{\end{tabular}\end{center}\end{table}}
\newenvironment{mytable}[2]{\begin{table}[hbtp]\label{#1}\begin{center}\begin{tabular}{#2}}{\caption{#1}\end{tabular}\end{center}\end{table}}
\begin{mytable}{tab:example}{This is the caption for my example table.}{cc}
  Row 1 & First \\
  Row 2 & Second \\
  Row 3 & Third \\
\end{mytable}

我还没有测试过这段代码

不要一直把它打印出来。在一次运行中改变整本书的表格外观。在所有桌子上都有明显相同的样式。干燥的典型原因。您可以询问定义环境或新命令的意义何在。好吧,定义新环境的意义在于使其拥有自己的计数器,而不是将其计算为表格或图形。。。但这似乎毫无意义。非常感谢。但结果证明\gdef不起作用。相反,我使用了\renewcommand,所有操作都可以按我的需要进行。谢谢。啊,我搞砸了。gdef命令周围的大括号不应该在那里:\gdef\templabel{1}\gdef\tempcaption{2}对不起。这就是我不首先测试代码的原因。使用
\centering
而不是使用中心环境。后者增加了不必要的额外垂直空间。协议是什么?我应该编辑我的答案以使其正确(以便其他人更容易复制/粘贴),还是保持原样以便评论有意义?