在 LaTeX 中使用自定义 Float 的 \newcommand 中的标签不起作用

Label not working inside a \newcommand with a custom Float in LaTeX

我声明了一个包含 \caption\label 的新环境,因此我可以引用它。

在我的 header:

\DeclareFloatingEnvironment[name=Tableau]{tableau}
\newenvironment{ptab}{\captionsetup{type=tableau}}{}

在我的 .tex 文档中:

\begin{ptab}
    \caption{A caption for my table}
    \label{ptab:myTab}
\end{ptab}

Some text with a reference (Tableau~\ref{ptab:myTab}) % Works fine !

问题:我想通过声明一个 \newcommand 可以为我写这篇文章来赢得一些时间。但是文本中的引用不再起作用了!

添加到我的header:

\newcommand{\tabref}[2]{%
    \begin{ptab} 
        \label{#1} 
        \caption{#2} 
    \end{ptab}}

在 .tex 文件中:

\tabref{ptab:myTab}{A caption for my table}

Some text with a reference (Tableau~\ref{ptab:myTab}) % Not working "(Tableau ??)"

我知道之前已经有人问过类似的问题,但它与新环境无关。 How to reference a label within a newcommand in LATEX?

我发现 \label{}\caption{} 的顺序颠倒了。 这很重要,因为 LaTeX 需要在 before 创建一个标题,它可以用标签引用。 工作代码:

\newcommand{\tabref}[2]{%
    \begin{ptab}
        \caption{#2} 
        \label{#1}  
    \end{ptab}}

\tabref{ptab:myTab}{A caption for my table}

Some text with a reference (Tableau~\ref{ptab:myTab}) % Now working !