首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用LaTeX创建一个在表格末尾带有标题的自定义表格环境?

如何用LaTeX创建一个在表格末尾带有标题的自定义表格环境?
EN

Stack Overflow用户
提问于 2009-09-07 16:19:24
回答 2查看 2.7K关注 0票数 6

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

我的环境看起来(有点简化)是这样的:

代码语言:javascript
复制
\newenvironment{mytable}[2]{\begin{table}[hbtp]\caption{#1}\label{#1}\begin{center}\begin{tabular}{#2}}{\end{tabular}\end{center}\end{table}}

我想把标题放在最后,就像这样:

代码语言:javascript
复制
\newenvironment{mytable}[2]{\begin{table}[hbtp]\label{#1}\begin{center}\begin{tabular}{#2}}{\caption{#1}\end{tabular}\end{center}\end{table}}

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-07 18:34:59

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

像这样的东西应该是有效的:

代码语言:javascript
复制
\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}%
}

使用这样的环境:

代码语言:javascript
复制
\begin{mytable}{tab:example}{This is the caption for my example table.}{cc}
  Row 1 & First \\
  Row 2 & Second \\
  Row 3 & Third \\
\end{mytable}

我还没有测试过这段代码。

票数 4
EN

Stack Overflow用户

发布于 2009-09-07 18:12:26

使用剪切和粘贴而不是新环境?我很确定\newenv。不应该以这种方式使用。这有什么意义呢?不要每次都把它都打出来?

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1390064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档