我有一个用\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}}但这不起作用,因为我不能在环境的末尾使用参数。我该如何解决这个问题?
发布于 2009-09-07 18:34:59
您需要存储标题和标签参数,并在以后使用它们。(此外,\标签应出现在\标题之后。)
像这样的东西应该是有效的:
\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}%
}使用这样的环境:
\begin{mytable}{tab:example}{This is the caption for my example table.}{cc}
Row 1 & First \\
Row 2 & Second \\
Row 3 & Third \\
\end{mytable}我还没有测试过这段代码。
发布于 2009-09-07 18:12:26
使用剪切和粘贴而不是新环境?我很确定\newenv。不应该以这种方式使用。这有什么意义呢?不要每次都把它都打出来?
https://stackoverflow.com/questions/1390064
复制相似问题