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

发布于 2024-08-04 15:02:00 字数 488 浏览 3 评论 0原文

我有一个用 \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}}

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

折戟 2024-08-11 15:02:00

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

类似这样的东西应该有效:

\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}

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

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.

屌丝范 2024-08-11 15:02:00

使用剪切和粘贴而不是新环境?我非常确定\newenv。并不意味着以这种方式使用。这有什么意义呢?不每次都把它全部输入吗?

use cut and paste instead of a new environment? i am fairy certain the \newenv. is not meant to be used in that manner. what is the point of this? to not type it all out every time?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文